Springboot文件上传与下载
一、创建简单的springboot-web项目
二、文件上传属性配置
#默认支持文件上传
spring.http.multipart.enabled =true
spring.http.multipart.file-size-threshold =0
# 上传文件的临时目录
#spring.http.multipart.location=E:/upload/temp/
# 最大支持文件大小
spring.http.multipart.max-file-size =100MB
# 最大支持请求大小
spring.http.multipart.max-request-size =100Mb
三、文件上传代码
1.Controller层代码:
@RestController
public class UploadController {
private static final Logger LOGGER = LoggerFactory.getLogger(UploadController.class); @GetMapping("/toUpload")
public String upload() {
return "upload";
} @PostMapping("/upload")
public String UploadFile(@RequestParam("file") MultipartFile file) {
if (file.isEmpty()) {
return "请选择文件";
}
//获取文件名
String fileName = file.getOriginalFilename();
String filePath = "C:/Users/upload/";
File dest = new File(filePath + fileName);
try {
file.transferTo(dest);
LOGGER.info("上传成功");
return "上传成功";
} catch (IOException e) {
LOGGER.error(e.toString(), e);
}
return "上传失败!";
}
}
2.jsp代码
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<title>单文件上传</title>
</head>
<body>
<form method="post" action="/upload" enctype="multipart/form-data">
<input type="file" name="file"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
四、文件下载代码
@RequestMapping(value = "/download", method = RequestMethod.GET)
@ResponseBody
public String testDownload(HttpServletResponse res,HttpServletRequest request) {
String fileName = "xxx.txt";
String filePath = "D:/uploadFile";
File file = new File(filePath + "/" + fileName);
System.out.println(file);
if (file.exists()){//判断文件是否存在
//判断浏览器是否为火狐
try {
if ("FF".equals(getBrowser(request))) {
// 火狐浏览器 设置编码new String(realName.getBytes("GB2312"), "ISO-8859-1");
fileName = new String(fileName.getBytes("GB2312"), "ISO-8859-1");
}else{
fileName = URLEncoder.encode(fileName, "UTF-8");//encode编码UTF-8 解决大多数中文乱码
fileName = fileName.replace("+", "%20");//encode后替换空格 解决空格问题
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
res.setContentType("application/force-download");//设置强制下载
res.setHeader("Content-Disposition", "attachment;filename=" + fileName);//设置文件名
byte[] buff = new byte[1024];// 用来存储每次读取到的字节数组
//创建输入流(读文件)输出流(写文件)
BufferedInputStream bis = null;
OutputStream os = null;
try {
os = res.getOutputStream();
bis = new BufferedInputStream(new FileInputStream(file));
int i = bis.read(buff);
while (i != -1) {
os.write(buff, 0, buff.length);
os.flush();
i = bis.read(buff);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}else {
return "文件不存在!!!";
}
return "download success";
} /**
* @Title: getBrowser
* @Description: 判断客户端浏览器
* @return String
* @author
* @date
*/
private static String getBrowser(HttpServletRequest request) {
String UserAgent = request.getHeader("USER-AGENT").toLowerCase();
if (UserAgent != null) {
if (UserAgent.indexOf("msie") != -1)
return "IE";
if (UserAgent.indexOf("firefox") != -1)
return "FF";
if (UserAgent.indexOf("safari") != -1)
return "SF";
}
return null;
}
}
五、测试
Springboot文件上传与下载的更多相关文章
- SpringBoot 文件上传、下载、设置大小
本文使用SpringBoot的版本为2.0.3.RELEASE 1.上传单个文件 ①html对应的提交表单 <form action="uploadFile" method= ...
- springboot 文件上传和下载
文件的上传和下载 1.文件上传 html页面代码如下 <form method="post" action="/file/upload1" enctype ...
- SpringBoot整合阿里云OSS文件上传、下载、查看、删除
1. 开发前准备 1.1 前置知识 java基础以及SpringBoot简单基础知识即可. 1.2 环境参数 开发工具:IDEA 基础环境:Maven+JDK8 所用技术:SpringBoot.lom ...
- springboot+web文件上传和下载
一.首先安装mysql数据库,开启web服务器. 二.pom.xml文件依赖包配置如下: <?xml version="1.0" encoding="UTF-8&q ...
- SpringBoot下文件上传与下载的实现
原文:http://blog.csdn.net/colton_null/article/details/76696674 SpringBoot后台如何实现文件上传下载? 最近做的一个项目涉及到文件上传 ...
- 七、springBoot 简单优雅是实现文件上传和下载
前言 好久没有更新spring Boot 这个项目了.最近看了一下docker 的知识,后期打算将spring boot 和docker 结合起来.刚好最近有一个上传文件的工作呢,刚好就想起这个脚手架 ...
- springboot文件上传下载,转载的
Spring Boot入门——文件上传与下载 原文来自:https://www.cnblogs.com/studyDetail/articles/7003253.html 1.在pom.xml文件中添 ...
- 补习系列(11)-springboot 文件上传原理
目录 一.文件上传原理 二.springboot 文件机制 临时文件 定制配置 三.示例代码 A. 单文件上传 B. 多文件上传 C. 文件上传异常 D. Bean 配置 四.文件下载 小结 一.文件 ...
- spring boot文件上传、下载
主题:Spring boot 文件上传(多文件上传)[从零开始学Spring Boot]http://www.iteye.com/topic/1143595 Spring MVC实现文件下载http: ...
随机推荐
- MySQL/MariaDB数据库忘掉密码解决办法--技术流ken
前言 有些时候我们常常会忘掉一些服务的密码,比如系统密码,我们可以进入救援模式进行修改密码,可参考我之前的博客<Centos7破解密码的两种方法--技术流ken>.但有些时候我们也会忘掉数 ...
- VUE v-for问题
今天写一个拖动然后使装备交换的功能,在背包格子里 发现直接设置Bags数组的项,v-for渲染出来的列表不会对应改变,只有设置值才会改变 有点拗口,贴代码吧 var repear = this.Bag ...
- Java 在PDF中添加水印——文本/图片水印
水印是一种十分常用的防伪手段,常用于各种文档.资料等.常见的水印,包括文字类型的水印.图片或logo类型的水印.以下Java示例,将分别使用insertTextWatermark(PdfPageBas ...
- mysql索引结构及其原理
1.定义 索引是一种数据结果,帮助提高获取数据的速度 为了提高查找速度,有很多查询优化算法.但是每种查找算法都只能应用于特定数据结构之上. 索引就是数据库创建的满足特定查找算法的数据结构,这些数据结构 ...
- Nginx系列
包括nginx的入门和进阶学习. 目录 nginx系列1:认识nginx nginx系列2:搭建nginx环境 nginx系列3:搭建一个静态资源web服务器 nginx系列4:日志管理 nginx系 ...
- composer windows下安装
composer windows安装 因要使用PhpSpreadsheet处理excel表格 选择composer安装 1. 下载Composer-Setup.exe 2.点击直接运行---选择ph ...
- flex-骰子布局
弹性容器单行:主轴居中,交叉轴居中. display: flex; flex-direction: row; align-items: center; justify-content: center; ...
- 本地服务器硬件信息获取指令wmic
获取BIOS序列号 wmic bios list full | find "SerialNumber" SerialNumber=P50168VB 获取CPUID(WIN32_PR ...
- 获取用户IP
public static string GetIP() { string ip; if (System.Web.HttpContext.Cu ...
- vue(3)—— vue的全局组件、局部组件
组件 vue有局部组件和全局组件,这个组件后期用的会比较多,也是非常重要的 局部组件 template与components属性结合使用挂载 其中 Vmain.Vheader.Vleft.Vconte ...