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

Java编程技巧:文件上传、下载、预览

目录

      • 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文件

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

相关文章:

  • 【蓝桥杯选拔赛真题63】Scratch云朵降雨 少儿编程scratch图形化编程 蓝桥杯选拔赛真题解析
  • 【新版】系统架构设计师 - 软件架构的演化与维护
  • 安卓循环遍历计时器
  • Docker-基本了解
  • Leetcode383. 赎金信
  • overleaf杂谈-Springer文献格式问题
  • No148.精选前端面试题,享受每天的挑战和学习
  • BASH shell脚本篇4——函数
  • VisualStudio配置OpenCV环境
  • C++手写NMS
  • 第9讲:VUE中监听器WATCH使用详解
  • 微信小程序开发基础(一)认识小程序
  • LeetCode 1049. 最后一块石头的重量 II
  • Golang中的类型转换介绍
  • 本人碰到的RN项目的坑
  • EcmaScript标准-导入与导出-js
  • 如何将matlab中的mat矩阵文件在python中读取出来
  • 解释C语言中 6.18f (浮点数常量后缀)
  • Pandas 2.1中的新改进和新功能
  • c#static(静态)关键字
  • GitHub配置SSH key
  • 文件审计及文件完整性监控
  • 华为智能企业远程办公安全解决方案(1)
  • k8s中常用命令总结
  • Logistic map混沌掩盖信号
  • 外包干了2个月,技术有明显退步...
  • 顺序表和链表
  • k8s--架构基础--云控制器管理器
  • OpenAI 更新 ChatGPT:支持图片和语音输入【附点评】
  • 数据结构:堆的简单介绍