009 spring boot中文件的上传与下载

时间:2023-01-03 18:30:55

一:任务

1.任务

  文件的上传

  文件的下载

二:文件的上传

1.新建一个对象

  FileInfo.java

 package com.cao.dto;

 public class FileInfo {
private String path;
public FileInfo(String path) {
this.path=path;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
} }

2.新建控制器

 package com.cao.web.controller;

 import java.io.File;
import java.io.IOException;
import java.util.Date; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; import com.cao.dto.FileInfo; @RestController
@RequestMapping("/file")
public class FileController {
@PostMapping
public FileInfo uploadFile(MultipartFile fileKey) throws Exception {
System.out.println("fileName: "+fileKey.getName());
System.out.println("OriginalFilename: "+fileKey.getOriginalFilename());
System.out.println("size: "+fileKey.getSize());
//将要存储在controller的目录下
String folder="E:\\workspace-sts-3.9.5.RELEASE\\it-security-demo\\src\\main\\java\\com\\cao\\web\\controller";
File localFile=new File(folder,new Date().getTime()+".txt");
fileKey.transferTo(localFile);
//写入流中,这个主要用于写入其他的地方,例如服务器等,这里不写了
//fileKey.getInputStream()
return new FileInfo(localFile.getAbsolutePath());
}
}

3.测试类

 /**
* 测试文件的上传
* @throws Exception
*/
@Test
public void whenUploadSuccess() throws Exception {
String result=mockMvc.perform(MockMvcRequestBuilders.fileUpload("/file")
.file(new MockMultipartFile("fileKey","test.txt","multipart/form-data","hello".getBytes("UTF-8"))))
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn().getResponse().getContentAsString();
System.out.println("result="+result);
}

4.控制台

  009 spring boot中文件的上传与下载

  存储到的现象

  009 spring boot中文件的上传与下载

三:文件的下载

1.添加io操作的包

           <dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>

2.文件下载的程序

 package com.cao.web.controller;

 import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; import com.cao.dto.FileInfo; @RestController
@RequestMapping("/file")
public class FileController { String folder="E:\\workspace-sts-3.9.5.RELEASE\\it-security-demo\\src\\main\\java\\com\\cao\\web\\controller"; /**
* 文件的上传控制器
* @param fileKey
* @return
* @throws Exception
*/
@PostMapping
public FileInfo uploadFile(MultipartFile fileKey) throws Exception {
System.out.println("fileName: "+fileKey.getName());
System.out.println("OriginalFilename: "+fileKey.getOriginalFilename());
System.out.println("size: "+fileKey.getSize());
//将要存储在controller的目录下
File localFile=new File(folder,new Date().getTime()+".txt");
fileKey.transferTo(localFile);
//写入流中,这个主要用于写入其他的地方,例如服务器等,这里不写了
//fileKey.getInputStream()
return new FileInfo(localFile.getAbsolutePath());
} /**
* 文件的下载
*/
@GetMapping("/{id}")
public void downFile(@PathVariable String id, HttpServletRequest request,HttpServletResponse response) {
try(
InputStream inputStream=new FileInputStream(new File(folder,id+".txt"));
OutputStream outputStream=response.getOutputStream();
)
{
response.setContentType("application/x-download");
response.addHeader("Content-Disposition", "attachment;filename=test.txt");
IOUtils.copy(inputStream, outputStream);
outputStream.flush();
} catch (Exception e) {
// TODO: handle exception
}
} }

3.在浏览器*问

  009 spring boot中文件的上传与下载