react 前端框架如何驱动企业数字化转型与创新发展
1540
2022-11-24
Swagger/Postman测试文件上传
Swagger/Postman测试文件上传
1.Controller2.Service3.测试
目前还不支持多文件上传,本实例是单个文件上传
# 最大支持文件大小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;@Api(tags = "文件上传接口")@RestController@RequestMapping("interval/upload")public class UploadController { private UploadService uploadService; public UploadController(UploadService uploadService) { this.uploadService = uploadService; } @ApiOperation("上传文件") @PostMapping(consumes = "multipart/*", headers = "content-type=multipart/form-data") public String upload(@RequestParam("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.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;@Servicepublic 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.测试
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~