关键的controller
package com.yuanshijia.securitydemo.controller; import com.yuanshijia.securitydemo.domain.FileInfo; import org.apache.tomcat.util.http.fileupload.IOUtils; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.util.Date; /** * 文件controller */ @RestController @RequestMapping("/file") public class FileController { //存放文件的路径 ,这里直接放到controller文件夹下 private static final String folder = "/Users/yuan/Documents/java/springboot/imooc_security/security-demo/src/main/java/com/yuanshijia/securitydemo/controller"; /** * 文件上传 * @param file * @return * @throws IOException */ @PostMapping public FileInfo update(MultipartFile file) throws IOException { /** * 注意:file名字要和参入的name一致 */ System.out.println("file name=" + file.getName()); System.out.println("origin file name=" + file.getOriginalFilename()); System.out.println("file size=" + file.getSize()); /** * 这里是写到本地 * 还可以用file.getInputStrem() * 获取输入流,然后存到阿里oss。。或七牛。。 */ File localFile = new File(folder, new Date().getTime() + ".txt"); //把传入的文件写到本地文件 file.transferTo(localFile); return new FileInfo(localFile.getAbsolutePath()); //getAbsolutePath为绝对路径 } /** * 文件的下载 */ @GetMapping("/{id}") //id为时间戳 public void download(@PathVariable("id") String id, HttpServletRequest request, HttpServletResponse response) { try ( //jdk7新特性,可以直接写到try()括号里面,java会自动关闭 InputStream inputStream = new FileInputStream(new File(folder, id + ".txt")); OutputStream outputStream = response.getOutputStream() ) { //指明为下载 response.setContentType("application/x-download"); String fileName = "test.txt"; response.addHeader("Content-Disposition", "attachment;fileName=" + fileName); // 设置文件名 //把输入流copy到输出流 IOUtils.copy(inputStream, outputStream); outputStream.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
文件信息类
FileInfo
/** * 文件信息 */ public class FileInfo { private String path; public FileInfo(String path) { this.path = path; } public FileInfo() { } public String getPath() { return path; } public void setPath(String path) { this.path = path; } }
测试类
import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.mock.web.MockMultipartFile; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; import java.util.Date; @RunWith(SpringRunner.class) @SpringBootTest public class UserControllerTest { @Autowired private WebApplicationContext webApplicationContext; private MockMvc mockMvc; @Before public void init(){ mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); } /** * 文件上传 */ @Test public void whenUploadSuccess() throws Exception { String result = mockMvc.perform(MockMvcRequestBuilders.fileUpload("/file") //就是post .file(new MockMultipartFile("file", "text.txt", "multipart/form-data", "hello upload".getBytes("UTF-8")))) //模拟文件上传 .andExpect(MockMvcResultMatchers.status().isOk()) .andReturn().getResponse().getContentAsString() ; System.out.println("result=" + result); } }
上传的结果:
下载结果:
在浏览器输入:http://localhost:8080/file/1525500214929
test就是设置的文件名了,查看内容: