Swagger/Postman测试文件上传
目前还不支持多文件上传,本实例是单个文件上传
# 最大支持文件大小
spring.servlet.multipart.max-file-size = 800MB
# 最大支持请求大小
spring.servlet.multipart.max-request-size = 800MB
1.Controller
package cn.tianyustudio.musicpartner_api.controller.interval;
import cn.tianyustudio.musicpartner_api.exception.BusinessException;
import cn.tianyustudio.musicpartner_api.service.UploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
(tags = "文件上传接口")
("interval/upload")
public class UploadController {
private UploadService uploadService;
public UploadController(UploadService uploadService) {
this.uploadService = uploadService;
}
("上传文件")
(consumes = "multipart/*", headers = "content-type=multipart/form-data")
public String upload( ("file") MultipartFile file) throws BusinessException{
return uploadService.upload(file);
}
}
2.Service
package cn.tianyustudio.musicpartner_api.service;
import cn.tianyustudio.musicpartner_api.exception.BusinessException;
import cn.tianyustudio.musicpartner_api.global.Constant;
import com.google.gson.Gson;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
public class UploadService {
public String upload(MultipartFile file) throws BusinessException{
verifyFile(file);
try {
return toUpload(file.getBytes());
}catch (Exception e){
throw new BusinessException("上传失败!");
}
}
private void verifyFile(MultipartFile file) throws BusinessException {
Long fileSize = file.getSize();
boolean flag = true;
if (file.isEmpty()){
throw new BusinessException("文件为空!");
}
if (fileSize > Constant.VIDEO_MAX_SIZE){
throw new BusinessException("文件太大无法上传!");
}
// 文件原名
String fileNameOriginal = file.getOriginalFilename();
String mimeType = file.getContentType();
if (mimeType.startsWith("image/")) {
if (fileSize > Constant.IMG_MAX_SIZE){
throw new BusinessException("图片太大无法上传!");
}
flag = false;
}
if(fileNameOriginal.endsWith(".ove")){
if (fileSize > Constant.IMG_MAX_SIZE){
throw new BusinessException("ove文件太大无法上传!");
}
flag = false;
}
if (mimeType.startsWith("audio/")) {
if (fileSize > Constant.MUSIC_MAX_SIZE){
throw new BusinessException("声音太大无法上传!");
}
flag = false;
}
if (mimeType.startsWith("video/")) {
if (fileSize > Constant.VIDEO_MAX_SIZE){
throw new BusinessException("视频太大无法上传!");
}
flag = false;
}
if (flag){
throw new BusinessException("文件格式不合法!");
}
}
private String toUpload(byte[] file) throws Exception{
//构造一个带指定 Region 对象的配置类
Configuration cfg = new Configuration(Zone.zone2());
//...其他参数参考类注释
UploadManager uploadManager = new UploadManager(cfg);
//...生成上传凭证,然后准备上传
String accessKey = Constant.QINIU_ACCESSKEY;
String secretKey = Constant.QINIU_SECRETKEY;
String bucket = Constant.QINIU_BUCKET;
//默认不指定key的情况下,以文件内容的hash值作为文件名
String key = null;
ByteArrayInputStream byteInputStream=new ByteArrayInputStream(file);
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
Response response = uploadManager.put(byteInputStream,key,upToken,null, null);
//解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
return putRet.key;
}
}
3.测试