一、创建简单的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文件上传与下载的更多相关文章

  1. SpringBoot 文件上传、下载、设置大小

    本文使用SpringBoot的版本为2.0.3.RELEASE 1.上传单个文件 ①html对应的提交表单 <form action="uploadFile" method= ...

  2. springboot 文件上传和下载

    文件的上传和下载 1.文件上传 html页面代码如下 <form method="post" action="/file/upload1" enctype ...

  3. SpringBoot整合阿里云OSS文件上传、下载、查看、删除

    1. 开发前准备 1.1 前置知识 java基础以及SpringBoot简单基础知识即可. 1.2 环境参数 开发工具:IDEA 基础环境:Maven+JDK8 所用技术:SpringBoot.lom ...

  4. springboot+web文件上传和下载

    一.首先安装mysql数据库,开启web服务器. 二.pom.xml文件依赖包配置如下: <?xml version="1.0" encoding="UTF-8&q ...

  5. SpringBoot下文件上传与下载的实现

    原文:http://blog.csdn.net/colton_null/article/details/76696674 SpringBoot后台如何实现文件上传下载? 最近做的一个项目涉及到文件上传 ...

  6. 七、springBoot 简单优雅是实现文件上传和下载

    前言 好久没有更新spring Boot 这个项目了.最近看了一下docker 的知识,后期打算将spring boot 和docker 结合起来.刚好最近有一个上传文件的工作呢,刚好就想起这个脚手架 ...

  7. springboot文件上传下载,转载的

    Spring Boot入门——文件上传与下载 原文来自:https://www.cnblogs.com/studyDetail/articles/7003253.html 1.在pom.xml文件中添 ...

  8. 补习系列(11)-springboot 文件上传原理

    目录 一.文件上传原理 二.springboot 文件机制 临时文件 定制配置 三.示例代码 A. 单文件上传 B. 多文件上传 C. 文件上传异常 D. Bean 配置 四.文件下载 小结 一.文件 ...

  9. spring boot文件上传、下载

    主题:Spring boot 文件上传(多文件上传)[从零开始学Spring Boot]http://www.iteye.com/topic/1143595 Spring MVC实现文件下载http: ...

随机推荐

  1. h5与c3权威指南笔记--css3结构性伪类选择器root,not,empty,target

    root:将样式绑定到根元素(html中的根元素是<html></html>) 举个栗子 :root{ background-color: yellow; } body{ ba ...

  2. 一看就能学会的H5视频推流方案

    本文由云+社区发表 作者:周超 导语 随着直播平台爆发式增长,直播平台从 PC 端转战移动端,紧跟着直播的潮流,自己学习实现了一套简单的 H5 视频推流的解决方案,下面就给小伙伴们分享一下自己学习过程 ...

  3. solr8.0 从数据库导入数据(三)

    第一步:导入相关包: 在创建的核心目录下新建lib文件夹(如果有,无需建立),从Solr源码包的dist文件夹中导入两个solr-dataimporthandler包,以及一个mysql驱动包. 第二 ...

  4. Linux使用wget、安装Python

    原文:https://blog.csdn.net/ghost_chou/article/details/81905302 1.指定需要安装的目录或创建并访问到该选择的目录下. mkdir /data/ ...

  5. 扫码下单支持同桌单人点餐FAQ

    一.使用场景 满足较多商户希望同一桌台,各自点各自的菜品的业态场景(例如杭味面馆,黄焖鸡米饭店,面馆等大多数轻快餐店) 二.配置步骤及注意事项 管理员后台配置--配置管理--店铺配置--扫码点餐tab ...

  6. python使用rabbitMQ介绍四(路由模式)

    一.模式介绍 路由模式,与发布-订阅模式一样,消息发送到exchange中,消费者把队列绑定到exchange上. 这种模式在exchange上添加添加了一个路由键(routing-key),生产者发 ...

  7. Git 简单粗暴使用

    1.现在总结一下今天学的两点内容: 初始化一个Git仓库,使用git init命令. 添加文件到Git仓库,分两步: 第一步,使用命令git add <file>,注意,可反复多次使用,添 ...

  8. 移动端和PC端弹出遮罩层后,页面禁止滚动的解决方法及探究

    PC端解决方案 pc端的解决思路就是在弹出遮罩层的时候取消已经存在的滚动条,达到无法滚动的效果. 也就是说给body添加overflow:hidden属性即可,IE6.7下不会生效,需要给html增加 ...

  9. 查询本地电脑IP地址

    使用Windows+R键打开"运行"窗口,然后输入CMD进入命令提示窗口 进入命令窗口之后,输入:ipconfig/all 回车即可看到整个电脑的详细的IP配置信息

  10. 分享收集的WebGL 3D学习资源

    大家好,我在本文中分享了我收集的WebGL 3D相关的博客.书籍.教程.demo等内容,希望对大家学习WebGL和3D有所帮助,谢谢- 相关博客 Wonder技术 Wonder是我们的产品,包含Web ...