目录
- 1、上传文件
- 1.1、代码
- 1.2、postman测试截图
- 2、下载resources目录中的模板文件
- 2.1、项目结构
- 2.2、代码
- 2.3、使用场景
- 3、预览文件
- 3.1、项目结构
- 3.2、代码
- 3.3、使用场景
1、上传文件
1.1、代码
@PostMapping("/uploadFile")
public String uploadFile(MultipartFile file) {
System.out.println("文件名称:" + file.getOriginalFilename());
return "成功";
}
@PostMapping("/uploadFile2")
public String uploadFile2(
@RequestParam("file") MultipartFile file
) {
System.out.println("文件名称:" + file.getOriginalFilename());
return "成功";
}
@PostMapping("/uploadFile3")
public String uploadFile3(
@RequestPart("file") MultipartFile file
) {
System.out.println("文件名称:" + file.getOriginalFilename());
return "成功";
}
// 发送文件的同时带上参数
@PostMapping("/uploadFile4")
public String uploadFile4(
@RequestPart("file") MultipartFile file, // 可以换成“MultipartFile file”或者“@RequestParam("file") MultipartFile file”
@RequestParam("id") String id
) {
System.out.println("文件名称:" + file.getOriginalFilename());
System.out.println("id:" + id);
return "成功";
}
1.2、postman测试截图
2、下载resources目录中的模板文件
2.1、项目结构
假设resources
目录下有一个pdf文件:用户数据导入模板.xlsx
,然后我们来下载该文件
2.2、代码
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.MediaTypeFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
@RestController
public class TestController {
@GetMapping("/downloadLocalFile")
public void downloadLocalFile(HttpServletResponse response) throws IOException {
String fileName = "用户数据导入模板.xlsx";
Resource r = new ClassPathResource(fileName);
try (
FileInputStream fileInputStream = new FileInputStream(r.getFile());
ServletOutputStream outputStream = response.getOutputStream();
) {
response.setContentType("application/force-download");
try {
fileName = new String(fileName.getBytes("utf-8"), "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
IOUtils.copyLarge(fileInputStream, outputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
}
2.3、使用场景
3、预览文件
3.1、项目结构
以resources
下面的文件为例,展示预览文件的代码,这是从本地获取文件,当然也可以通过其他方式获取文件
3.2、代码
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.MediaTypeFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
@RestController
public class TestController {
@GetMapping("/previewLocalFile")
public void previewLocalFile(HttpServletResponse response) throws IOException {
String fileName = "SQL必知必会(第5版).pdf";
Resource r = new ClassPathResource(fileName);
try (
FileInputStream fileInputStream = new FileInputStream(r.getFile());
ServletOutputStream outputStream = response.getOutputStream();
) {
// 区别点1:将“response.setContentType("application/force-download");”替换成下面内容
response.setContentType(MediaTypeFactory.getMediaType(fileName).orElse(MediaType.APPLICATION_OCTET_STREAM).toString());
try {
fileName = new String(fileName.getBytes("utf-8"), "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// 区别点2:预览是“filename”,下载是“attachment;filename=”
response.setHeader("Content-Disposition", "filename=" + fileName);
IOUtils.copyLarge(fileInputStream, outputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.3、使用场景
在网盘软件中预览pdf文件