java实现一个文件上传接口
发布日期:2021-06-30 17:19:10
浏览次数:2
分类:技术文章
本文共 1859 字,大约阅读时间需要 6 分钟。
接口声明
这个接口的参数描述还算详细。主要是用了Swagger的注解@ApiParam。@RestController@RequestMapping("/upload")@Api(tags="文件上传")public class AliyunOssController { @Autowired UploadService service; @PostMapping(value = "/comment/{bId}/{uId}") @ApiOperation(value = "上传评论图片或视频,成功后返回文件路径",notes = "/upload/comment/板块ID/用户ID") public JSONObject comment(@ApiParam(value = "版块ID", required = true) @PathVariable String bId, @ApiParam(value = "用户ID", required = true) @PathVariable String uId, @ApiParam(value = "文件", required = true) @RequestParam(value="f") MultipartFile file) { String url = service.upload(bId,uId,file); …… JSONObject jo = new JSONObject(); jo.put("code",200); jo.put("msg","ok"); jo.put("data",url); return jo; }}
接口实现
@Service("uploadService")public class UploadService { public String upload(String bId, String uId, MultipartFile file) { //获得文件保存路径 String url = getFilePath(bId, uId, file); saveToLocal(file); return url; } private void saveToLocal(MultipartFile file){ //上传到本地 try { String fileName = file.getOriginalFilename(); InputStream input = file.getInputStream(); OutputStream outputStream = new FileOutputStream("q:" + File.separator + fileName); byte[] b = new byte[4096]; int count = input.read(b); while (count != -1) { for(int i = 0; i < count; i++){ outputStream.write(b[i]); } count = input.read(b); } input.close(); outputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}
转载地址:https://leftfist.blog.csdn.net/article/details/114702328 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2024年05月01日 14时40分05秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
location区段
2019-04-30
nginx访问控制、基于用户认证、https配置
2019-04-30
用zabbix监控nginx
2019-04-30
SaltStack
2019-04-30
Jenkins 控制台输出中的奇怪字符
2019-04-30
Linux添加系统调用
2019-04-30
linux内存的寻址方式
2019-04-30
ubunut16.04的pip3出现问题,重新安装pip3
2019-04-30
how2heap-double free
2019-04-30
how2heap-fastbin_dup_consolidate
2019-04-30
orw_shellcode_模板
2019-04-30
[fmt+shellcode]string
2019-04-30
fmt在bss段(neepusec_easy_format)
2019-04-30
[double free] 9447 CTF : Search Engine
2019-04-30
python 函数式编程
2019-04-30
python编码
2019-04-30
scala maven plugin
2019-04-30
flink 1-个人理解
2019-04-30
redis cli
2019-04-30