当前位置: 首页 > news >正文

springboot文件上传和下载接口的简单思路

springboot文件上传和下载的简单思路

  • 文件上传
  • 文件下载

文件上传

在springboot中,上传文件只需要在接口中通过 MultipartFile 对象来获取前端传递的数据,然后将数据存储,并且返回一个对外访问路径即可。一般对于上传文件的文件名,都要通过uuid进行处理。

@RestController
@RequestMapping("/file")
public class FileController {// 服务器存储位置private static  String parentPath = "D:\\IdeaProjects\\demoZ\\src\\main\\resources\\static\\";// 上传接口@RequestMapping("/upload")public String uploadFile(@RequestParam MultipartFile file) throws IOException {String originalFilename = file.getOriginalFilename(); // 获取文件名String type = file.getContentType(); // 获取文件类型long size = file.getSize(); // 获取文件大小// 这里只对一种类型进行了简单判断,可以自行修改if ("image/jpeg".equals(type)){type = ".jpg";}// 构建uuid作为唯一标识String uuid = UUID.randomUUID().toString()+type;File uploadFile = new File(parentPath+uuid);// 判断父级目录是否存在,不存在则创建File parentFile = uploadFile.getParentFile();if(!parentFile.exists()){parentFile.mkdirs();}// 文件存储到磁盘上file.transferTo(uploadFile);// 返回对外访问路径return "http://localhost:9090/file/"+uuid;}
}

在这里插入图片描述
在这里插入图片描述

文件下载

文件下载的话,只需要在接口中传入文件的uuid名称,然后通过流的方式直接输出即可。

@RestController
@RequestMapping("/file")
public class FileController {private static  String parentPath = "D:\\IdeaProjects\\demoZ\\src\\main\\resources\\static\\";// 文件上传接口....// 文件下载接口@RequestMapping("/{uuid}")public void download(@PathVariable String uuid, HttpServletResponse response) throws IOException {// 通过response将数据输出// 根据文件的唯一标识码获取文件File uploadFile = new File(parentPath+uuid);// 设置输出流的格式ServletOutputStream os = response.getOutputStream();response.addHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(uuid,"UTF-8"));response.setContentType("application/octet-stream");// 创建文件输入流FileInputStream fis = new FileInputStream(uploadFile);// 读取文件的字节流byte[] buf = new byte[1024];int readLen = 0;while ((readLen=fis.read(buf))!=-1){// 边读边写os.write(buf,0,readLen);}os.flush();os.close();}
}

tips:通过文件上传的返回值即可测试下载接口 http://localhost:9090/file/59038923-ea78-40df-970e-70f49dc966b3.jpg

这里的话,为了简单就只写了后端的文件上传和下载,没有涉及到数据库的文件信息存储,正常情况下是根据你文件的唯一标识去获取下载这张图片的url地址的。(不过接口写的没问题哈)

http://www.lryc.cn/news/124801.html

相关文章:

  • MySQL索引和事务
  • typeScript 之 基础
  • android app控制ros机器人五(百度地图)
  • 【ts】【cocos creator】excel表格转JSON
  • 每天一个知识点——L2R
  • 解决flutter showDialog下拉框,复选框等无法及时响应的问题
  • [C++ 网络协议编程] UDP协议
  • reactNative跳转appstore链接报错:Redirection to URL with a scheme that is not HTTP(S)
  • html css实现爱心
  • react中使用shouldComponentUpdate生命周期函数调用setState引起的无限循环的错误
  • 麦肯锡发布《2023科技趋势展望报告》,生成式AI、下一代软件开发成为趋势,软件测试如何贴合趋势?
  • 【爬虫】P1 对目标网站的背景调研(robot.txt,advanced_search,builtwith,whois)
  • maven如何建立JavaWeb项目并连接数据库,验证登录
  • CVPR 2023 | 用户可控的条件图像到视频生成方法(基于Diffusion)
  • 动态规划(基础)
  • 【Pytorch:nn.Embedding】简介以及使用方法:用于生成固定数量的具有指定维度的嵌入向量embedding vector
  • 动态库的命名规则
  • 【Linux】网络---->网络理论
  • Android学习之路(4) UI控件之输入框
  • 1.初识Web
  • 【微服务技术一】Eureka、Nacos、Ribbon(配置管理、注册中心、负载均衡)
  • 【Linux】可重入函数 volatile关键字 以及SIGCHLD信号
  • 【动态规划】回文串问题
  • Laravel Swift Mail发送带附件的邮件报错 “Swift_IoException The path cannot be empty“处理
  • Linux下常见的代理服务器软件介绍
  • SCSS的基本用法
  • alertmanager创建nginx-ingress basic auth鉴权
  • 系列六、Redis中的五大数据类型及相关操作
  • 四大运营商的大流量卡测评,看完您会选哪个运营商?
  • Apache-Maven