代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
Spring Boot 文件上传与下载
作者:
/ 发布于
2018/8/31
/
668
1、在pom.xml文件中添加依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.wyl</groupId> <artifactId>SpringBootFile</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>SpringBootFile</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.7</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- thymeleaf模板插件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- devtools插件,热部署 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <scope>true</scope> </dependency> </dependencies> </project> 2、application.properties文件中取消模板文件缓存 spring.thymeleaf.cache=false 3、编写模板文件 file.html <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <head> <meta charset="UTF-8" /> <title>Insert title here</title> </head> <body> <h1 th:inlines="text">文件上传</h1> <form action="fileUpload" method="post" enctype="multipart/form-data"> 选择文件: <input type="file" name="fileName"/> <input type="submit" value="提交"/> </form> </body> </html> multifile.html <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <head> <meta charset="UTF-8" /> <title>Insert title here</title> </head> <body> <h1 th:inlines="text">文件上传</h1> <form action="multifileUpload" method="post" enctype="multipart/form-data" > 选择文件1: <input type="file" name="fileName"/> 选择文件2: <input type="file" name="fileName"/> 选择文件3: <input type="file" name="fileName"/> <input type="submit" value="提交"/> </form> </body> </html> 4、编写Controller import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; @Controller public class FileUploadController { /* * 获取file.html页面 */ @RequestMapping("file") public String file(){ return "/file"; } /** * 实现文件上传 * */ @RequestMapping("fileUpload") @ResponseBody public String fileUpload(@RequestParam("fileName") MultipartFile file){ if(file.isEmpty()){ return "false"; } String fileName = file.getOriginalFilename(); int size = (int) file.getSize(); System.out.println(fileName + "-->" + size); String path = "F:/test" ; File dest = new File(path + "/" + fileName); if(!dest.getParentFile().exists()){ //判断文件父目录是否存在 dest.getParentFile().mkdir(); } try { file.transferTo(dest); //保存文件 return "true"; } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); return "false"; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return "false"; } } /* * 获取multifile.html页面 */ @RequestMapping("multifile") public String multifile(){ return "/multifile"; } /** * 实现多文件上传 * */ @RequestMapping(value="multifileUpload",method=RequestMethod.POST) /**public @ResponseBody String multifileUpload(@RequestParam("fileName")List<MultipartFile> files) */ public @ResponseBody String multifileUpload(HttpServletRequest request){ List<MultipartFile> files = ((MultipartHttpServletRequest)request).getFiles("fileName"); if(files.isEmpty()){ return "false"; } String path = "F:/test" ; for(MultipartFile file:files){ String fileName = file.getOriginalFilename(); int size = (int) file.getSize(); System.out.println(fileName + "-->" + size); if(file.isEmpty()){ return "false"; }else{ File dest = new File(path + "/" + fileName); if(!dest.getParentFile().exists()){ //判断文件父目录是否存在 dest.getParentFile().mkdir(); } try { file.transferTo(dest); }catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return "false"; } } } return "true"; } } 6、多文件上传中遇到的问题 org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field fileName exceeds its maximum permitted size of 1048576 bytes. Spring Boot默认文件上传大小为2M,多文档上传中总是出现文件大小超出限度 解决方法: a、在application.properties文件中设置文件大小 # Single file max size multipart.maxFileSize=50Mb # All files max size multipart.maxRequestSize=50Mb 但是,事实证明此种方法不能够解决以上问题 b、在启动类App.class文件中配置Bean来设置文件大小 复制代码 import javax.servlet.MultipartConfigElement; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.MultipartConfigFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * Hello world! * */ @SpringBootApplication @Configuration public class App { public static void main( String[] args ) { System.out.println( "Hello World!" ); SpringApplication.run(App.class, args); } /** * 文件上传配置 * @return */ @Bean public MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); //单个文件最大 factory.setMaxFileSize("10240KB"); //KB,MB /// 设置总上传数据总大小 factory.setMaxRequestSize("102400KB"); return factory.createMultipartConfig(); } 复制代码 7、文件下载 复制代码 @RequestMapping("download") public String downLoad(HttpServletResponse response){ String filename="2.jpg"; String filePath = "F:/test" ; File file = new File(filePath + "/" + filename); if(file.exists()){ //判断文件父目录是否存在 response.setContentType("application/force-download"); response.setHeader("Content-Disposition", "attachment;fileName=" + filename); byte[] buffer = new byte[1024]; FileInputStream fis = null; //文件输入流 BufferedInputStream bis = null; OutputStream os = null; //输出流 try { os = response.getOutputStream(); fis = new FileInputStream(file); bis = new BufferedInputStream(fis); int i = bis.read(buffer); while(i != -1){ os.write(buffer); i = bis.read(buffer); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("----------file download" + filename); try { bis.close(); fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; }
试试其它关键字
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
贡献的其它代码
Label
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3