maven坐标地址
<dependency> <groupId>com.upyun</groupId> <artifactId>java-sdk</artifactId> <version>4.2.3</version> </dependency>
上代码
// 获取文件流hash InputStream inputStream = file.getInputStream(); String fileHash = DigestUtils.md5DigestAsHex(inputStream).toLowerCase(); // 初始化sdk RestManager manager = new RestManager("image-learn-app-avatar", "a392223903", "kqoyJ4kp37IXdDzzqCYHdn3m8ty6q0pxnzvhx1"); manager.setTimeout(60); // 根据hash生成二级目录 String filePath1 = fileHash.substring(0, 2); String filePath2 = fileHash.substring(2, 4); // 创建所需的文件二级目录 String path = File.separator + app + File.separator + filePath1 + File.separator + filePath2 + File.separator; try { System.out.println(path); Response result = manager.mkDir(path); if (!result.isSuccessful()) { throw new BusinessException("创建目录失败,错误码" + result.code()); } } catch (UpException e) { throw new BusinessException("创建目录失败:" + e.getMessage()); } // 填充完整文件名称 String fillName = fileHash + "." + getExtensionByContentType(file.getContentType()); String fillPath = path + File.separator + fillName; // 上传文件 Map<String, String> params = new HashMap<String, String>(); try (Response result = manager.writeFile(fillPath, file.getInputStream(), params)) { if (!result.isSuccessful()) { throw new BusinessException("上传文件失败,错误码" + result.code()); } } catch (UpException e) { throw new BusinessException("上传文件失败:" + e.getMessage()); }