1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
| public class FileUpload {
private static final String OSS_DIR = "dir/";
private static final String BUCKET = "bucket";
private static final String CDN_PRE = "https://cdn.com/" + OSS_DIR;
private static final String END_POINT = "endpoint";
private static final String KEY_ID = "key_id";
private static final String KEY = "key";
public static void main(String[] args) {
if(args.length < 1){ printUsage(); return; } String path = args[0];
if(isBlank(path)){ printUsage(); return; }
File file = new File(path); if(!file.exists()){ printErr(path + " not found."); return; }
try{ upload(file); }catch(Exception e){ e.printStackTrace(); }
}
public static String writeToOss(String fileName, String suffixName,byte[] data) {
if(isBlank(fileName) || isBlank(suffixName) || data == null || data.length == 0 ){ throw new IllegalArgumentException("file name or data is empty"); }
suffixName = suffixName.trim();
String newName = fileName + suffixName; String ossFilePath = OSS_DIR + newName;
OSS oss = null; try { oss = new OSSClientBuilder().build(END_POINT,KEY_ID,KEY); PutObjectResult putObjectResult = oss.putObject(BUCKET, ossFilePath, new ByteArrayInputStream(data)); } catch (Exception e) { String errMsg = "put oss object error,fileName:" + ",data size:" + data.length; throw new IllegalStateException(errMsg); } finally { try { if (oss != null) { oss.shutdown(); } } catch (Exception e) { } }
String cdnPath = CDN_PRE + newName; return cdnPath; }
public static final void upload(File file){
if(file.isFile()){ upload0(file); }else if(file.isDirectory()){ File[] files = file.listFiles(); for (File f : files) { if(f.isFile()){ upload0(f); } } } }
public static void upload0(File file){
try { String name = file.getName(); String suffix = getSuffix(name); byte[] bytes = Files.readAllBytes(file.toPath()); String md5 = md5(bytes);
String cdnPath = writeToOss(md5,suffix,bytes);
System.out.println("file : " + file.getPath()); System.out.println("md5 : " + md5); System.out.println("url : " + cdnPath); System.out.println("===upload success==="); } catch (IOException e) { throw new IllegalStateException(e); } }
private static void printUsage(){
System.out.println("==========usage=========="); System.out.println("java -jar xxx.jar [file/dir]"); }
private static void printErr(String error){ System.err.println(error); }
private static boolean isBlank(final CharSequence cs) { int strLen; if (cs == null || (strLen = cs.length()) == 0) { return true; } for (int i = 0; i < strLen; i++) { if (!Character.isWhitespace(cs.charAt(i))) { return false; } } return true; }
public static final char[] HEX_DIGITS = "0123456789abcdef".toCharArray();
private static String byte2Hex(byte[] bytes) { int j = bytes.length; char[] md5 = new char[j * 2]; int k = 0; for (int i = 0; i < j; i++) { byte byte0 = bytes[i]; md5[k++] = HEX_DIGITS[byte0 >>> 4 & 0xf]; md5[k++] = HEX_DIGITS[byte0 & 0xf]; } return new String(md5); }
private static String encode(String alg, byte[] message) { try { MessageDigest md = MessageDigest.getInstance(alg); return byte2Hex(md.digest(message)); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } }
public static String md5(String message) { try { return md5(message.getBytes("UTF-8")); } catch (Exception e) { throw new RuntimeException(e); } }
public static String md5(byte[] message) { return encode("MD5", message); }
public static String sha256(String message) { try { return sha256(message.getBytes("UTF-8")); } catch (Exception e) { throw new RuntimeException(e); } }
public static String sha256(byte[] message) { return encode("SHA-256", message); }
public static synchronized String base64Encode(byte[] buff) { if (null == buff) { return null; } else { return Base64.getEncoder().encodeToString(buff); } }
public static String hmacSHA1(String source, String accessSecret) throws Exception { Mac e = Mac.getInstance("HmacSHA1"); e.init(new SecretKeySpec(accessSecret.getBytes("UTF-8"), "HmacSHA1")); byte[] signData = e.doFinal(source.getBytes("UTF-8")); return base64Encode(signData); }
public static String hmacSHA256(String source, String accessSecret) throws Exception { Mac e = Mac.getInstance("HmacSHA256"); e.init(new SecretKeySpec(accessSecret.getBytes("UTF-8"), "HmacSHA256")); byte[] signData = e.doFinal(source.getBytes("UTF-8")); return base64Encode(signData); }
public static final String DOT = ".";
public static final String getSuffix(String fileName) {
if(isBlank(fileName)){ throw new IllegalArgumentException("file name is empty"); }
int index = fileName.lastIndexOf(DOT);
if(index > 0 ){ String suffix = fileName.substring(index); return suffix; }
return ""; } }
|