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: ...
随机推荐
- RequireJS入门级_RequireJS能给我们带来什么帮助?
前言:其实很早前就已经接触和了解到RequireJS了,当时只是大概明白它能用来控制JS的加载顺序,即:页面一定要先加载这个JS,再加载那个JS,对于RequireJS的好处还没有一个真正的体会和认识 ...
- Elasticsearch与Hadoop集成大数据处理介绍
传统大数据处理 现代数据架构 Hadoop在20业务场景的应用 DataLake A data lake is a system or repository of data stored in its ...
- Java SimpleDateFormat处理日期与字符串的转换
1.为什么要使用SimpleDateFormat? 在Java中,如果我们想获取当前时间,一般会使用Date类的无参构造函数,如下所示,我们获取到当前时间并输出: import java.util.D ...
- jQuery(四)、文档处理
1 内部插入 1.1 append(content | fn) 向每个匹配的元素内部追加内容. 参数: (1) content:要追加到目标中的内容. (2) function(index, html ...
- phpcms V9 二次开发------(获取点击数详解)
关于phpcms V9的点击数的使用应该有不少数是直接调用网上搜索到的代码,但是对于一些想要深入研究开发的人来说,看到网上的代码后更是不解,本人这几天看了看,了解了一些东西,在这里写出来分享一下,首先 ...
- harris角点检测的简要总结
目录 1. 概述相关 2. 原理详解 1) 算法思想 2) 数学模型 3) 优化推导 3. 具体实现 1) 详细步骤 2) 最终实现 4. 参考文献 1. 概述相关 harris角点检测是一种特征提取 ...
- 服务器三:多线程epoll
#include <fcntl.h> #include <sys/socket.h> #include <netinet/in.h> #include <ar ...
- jQuery链式编程
链式编程 多行代码合并成一行代码,前提要认清此行代码返回的是不是对象.是对象才能进行链式编程 .html(‘val’).text(‘val’).css()链式编程,隐式迭代 链式编程注意:$(‘div ...
- Canvas 绘图学习笔记2
1 绘制文本 fillText(string,x,y,maxWidth) //填充试绘制文本 strokeText(string,x,y,maxWidth) 画线试绘制文本 设置字体样式: cont ...
- Invalid Host header
这个主要是自己遇到很多次了,每次都去网上查改哪里,这次记到自己这里吧,以后把遇到的vue工具的一些问题都整理到这里 在vue中开发的项目有时候需要到手机上看效果,但是你配好本地端口之后,会出现访问内容 ...