SpringBoot文件的上传与下载
⒈文件实体类
1 package cn.coreqi.security.entities; 2 3 public class FileInfo { 4 5 private String path; 6 7 public FileInfo(String path) { 8 this.path = path; 9 } 10 11 public String getPath() { 12 return path; 13 } 14 15 public void setPath(String path) { 16 this.path = path; 17 } 18 }
⒉控制器代码
1 package cn.coreqi.security.controller; 2 3 import cn.coreqi.security.entities.FileInfo; 4 import org.apache.tomcat.util.http.fileupload.IOUtils; 5 import org.springframework.web.bind.annotation.GetMapping; 6 import org.springframework.web.bind.annotation.PathVariable; 7 import org.springframework.web.bind.annotation.PostMapping; 8 import org.springframework.web.bind.annotation.RestController; 9 import org.springframework.web.multipart.MultipartFile; 10 11 import javax.servlet.http.HttpServletRequest; 12 import javax.servlet.http.HttpServletResponse; 13 import java.io.*; 14 import java.util.Date; 15 16 @RestController 17 public class FileController { 18 19 @PostMapping("/file") 20 public FileInfo upload(MultipartFile file) throws IOException { 21 System.out.println(file.getName()); //文件名 22 System.out.println(file.getOriginalFilename()); //原始文件名 23 String folder = "d:/test"; 24 File localFile = new File(folder,new Date().getTime() + ".txt"); 25 file.transferTo(localFile); //将上传的文件写入到本地的文件中 26 return new FileInfo(localFile.getAbsolutePath()); //绝对路径 27 } 28 29 @GetMapping("/file/{id}") 30 public void download(@PathVariable String id, HttpServletRequest request, HttpServletResponse response){ 31 try( 32 InputStream inputStream = new FileInputStream(new File("d:/test/1553692860875.txt")); 33 OutputStream outputStream = response.getOutputStream(); 34 ) { 35 response.setContentType("application/x-download"); 36 response.addHeader("Content-Disposition","attachment;filename=test.txt"); 37 IOUtils.copy(inputStream,outputStream); 38 outputStream.flush(); 39 40 } catch (FileNotFoundException e) { 41 e.printStackTrace(); 42 } catch (IOException e) { 43 e.printStackTrace(); 44 } 45 } 46 }
⒊测试
1 /** 2 * 上传文件测试 3 */ 4 @Test 5 public void whenUploadSuccess() throws Exception { 6 //mockMvc.perform(MockMvcRequestBuilders.fileUpload("/file") 7 String result = mockMvc.perform(MockMvcRequestBuilders.multipart("/file") 8 .file(new MockMultipartFile("file","test.txt","multipart/form-data","hello upload".getBytes("UTF-8")))) 9 .andExpect(status().isOk()) 10 .andReturn().getResponse().getContentAsString(); 11 System.out.println(result); 12 }
作者:奇
出处:https://www.cnblogs.com/fanqisoft/p/10611005.html
版权:本作品采用「本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。」许可协议进行许可。
分类:
Spring Boot
如果文章内容对您有所帮助,欢迎赞赏.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!