String bucket = “个人存储空间名字”;
//默认不指定key的情况下,以文件内容的hash值作为文件名
try {
byte[] uploadBytes = file.getBytes();
Auth auth = Auth.create(accessKey, accessSecretKey);
String upToken = auth.uploadToken(bucket);
Response response = uploadManager.put(uploadBytes, fileName, upToken);
//解析上传成功的结果
DefaultPutRet putRet = JSON.parseObject(response.bodyString(), DefaultPutRet.class);
return true;
} catch (Exception ex) {
ex.printStackTrace();
}
return false;
}
}
3、我们的两个密钥是配置在properties中,如下
4、还是在springboot的配置文件中
pringboot默认只上传1M的图片大小所以修改文件配置
上传文件总的最大值
spring.servlet.multipart.max-request-size=20MB
单个文件的最大值
spring.servlet.multipart.max-file-size=2MB
5、使用QiniuUtils工具类
@RestController
@RequestMapping(“/upload”)
public class UploadController {
@Autowired
QiniuUtils qiniuUtils ;
@PostMapping
public Result upload(@RequestParam(“image”) MultipartFile file){
//原始文件名称比如