springboot 文件上传和下载
文件的上传和下载
1、文件上传
html页面代码如下
<form method="post" action="/file/upload1" enctype="multipart/form-data">
<p>
文件:<input type="file" name="file">
<input type="submit" value="上传">
</p>
</form>
Controller代码,
@RequestMapping(“/upload1”)
@ResponseBody
public String upload1(@RequestParam(“file”)MultipartFile file,HttpServletRequest request) throws IOException {
//获取文件名
String filename=file.getOriginalFilename();
//文件上传后路径
String path=”E://images//“;
File file1=new File(path+filename);
//如果文件不存在,则创建新的文件夹
if(!file1.getParentFile().exists()){
file1.getParentFile().mkdirs();
}
byte[] bytes=file.getBytes(); //获得文件字节
//将filename文件写入到path路径下
FileOutputStream fos=new FileOutputStream(file1);
fos.write(bytes); //将字节写入
fos.flush();
fos.close();
return “ok”;
}
判断文件的另一个方法,将
File file1=new File(path+filename);
//如果文件不存在,则创建新的文件夹
if(!file1.getParentFile().exists()){
file1.getParentFile().mkdirs();
}
FileOutputStream fos=new FileOutputStream(file1);
替换成
File file1=new File(path);
//如果文件不存在,则创建新的文件夹
if(!file1.exists()){
file1.mkdirs();
}
FileOutputStream fos=new FileOutputStream(path+filename);
2、文件上传(第二中方法)
@RequestMapping("/upload1")@ResponseBodypublic String upload1(@RequestParam("file")MultipartFile file,HttpServletRequest request) throws IOException {//获取文件名String filename=file.getOriginalFilename();//文件上传后路径String path="E://images//";File file1=new File(path+filename);//如果文件不存在,则创建新的文件夹if(!file1.getParentFile().exists()){file1.getParentFile().mkdirs();}//上传的文件存放的位置file.transferTo(file1);return "ok";}
3、文件下载代码
//文件下载
@RequestMapping(“/download”)
@ResponseBody
public String download(HttpServletResponse response) throws IOException {
//要下载的文件路径
String filepath=”E://images//“;
//要下载的文件名称
String filename=”1.jpg”;
File file=new File(filepath,filename);
//判断文件是否存在
if(file.exists()){
response.setContentType(“application/force-download”);//设置强制下载不打开
response.addHeader(“Content-Disposition”,”attachment;fileName=”+filename);//设置文件名
byte[]buf=new byte[1024];
//文件输入了
FileInputStream fis=null;
//带缓冲的字节流
BufferedInputStream bis=null;
OutputStream os=null;//输出流
try{
fis=new FileInputStream(file);
bis=new BufferedInputStream(fis);
os=response.getOutputStream();
int i=bis.read(buf);
while (i!=-1){
os.write(buf);
i=bis.read(buf);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(bis!=null){
bis.close();
}if(fis!=null){
fis.close();
}
}
}
return “download”;
}
4、多文件上传代码
html页面代码
<form action="/file/uploads" method="post" enctype="multipart/form-data">
<p>文件1:<input type="file" name="file"></p>
<p>文件2:<input type="file" name="file"></p>
<p>文件3:<input type="file" name="file"></p>
<p> <input type="submit" value="上传"></p>
</form>
Controller代码
@RequestMapping(“/uploads”)
@ResponseBody
public String uploads(HttpServletRequest request){
//获取上传的文件
List<MultipartFile>files=((MultipartHttpServletRequest)request).getFiles(“file”);
MultipartFile file=null;
BufferedOutputStream stream=null;
for(int i=0;i<files.size();i++){
file=files.get(i);
if(!file.isEmpty()){
try{
byte[]bytes=file.getBytes();
stream=new BufferedOutputStream(new FileOutputStream(
new File(file.getOriginalFilename())));
stream.write(bytes);
stream.close();
} catch (IOException e) {
return “failure,”+i+”=>”+e.getMessage();
}
}else{
return “failure,is null”;
}
}
return “ok”;
}
总结:以上方法都亲测有效,如果问题请留言。
springboot 文件上传和下载的更多相关文章
- SpringBoot 文件上传、下载、设置大小
本文使用SpringBoot的版本为2.0.3.RELEASE 1.上传单个文件 ①html对应的提交表单 <form action="uploadFile" method= ...
- Springboot文件上传与下载
一.创建简单的springboot-web项目 二.文件上传属性配置 #默认支持文件上传 spring.http.multipart.enabled =true spring.http.multipa ...
- 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: ...
随机推荐
- 解决chrome无法启用印象笔记-剪藏功能
新版Chrome浏览器安全的问题,导致从印象笔记官网下载的rxs(crx)文件无法直接通过drap&drop功能添加功能块.可以将rxs(crx)文件后缀名改成rar格式,然后解压,通 ...
- h5在线1v1客服|web在线客服系统|h5即时聊天
网上有很多环信.美恰之类的客服系统,最近也使用h5+css3+fontJs+swiper+wcPop等技术架构开发了一个在线客服(1v1沟通聊天),可以应用到在线临时聊天.在线咨询等情景.实现了消息. ...
- HashMap、HashSet、LinkedHashSet、TreeSet的关系
类图及说明如下:
- Hadoop简介与伪分布式搭建—DAY01
一. Hadoop的一些相关概念及思想 1.hadoop的核心组成: (1)hdfs分布式文件系统 (2)mapreduce 分布式批处理运算框架 (3)yarn 分布式资源调度系统 2.hadoo ...
- python网络爬虫技术图谱
- 快速初步了解Neo4j与使用
快速初步了解Neo4j与使用 Neo4j是一个高性能的,NOSQL图形数据库,它将结构化数据存储在网络上而不是表中.它是一个嵌入式的.基于磁盘的.具备完全的事务特性的Java持久化引擎,但是它将结构化 ...
- 白月黑羽Python在线教程
推荐白月黑羽Python在线教程 白月黑羽 站在初学者的角度为大家安排了Python学习教程,帮助大家迅速掌握程序开发技能. http://www.python3.vip/doc/tutorial/p ...
- MySQL的Sleep进程占用大量连接解决方法
第一部分为产生大量sleep进程的原理及对应解决方法第二部分为设置wait_timeout值,有效减少sleep进程 ========================================= ...
- mongodb 错误 SCRAM-SHA-1 authentication failed for --转
log 日志错误信息 2018-10-24T16:14:42.244+0800 I NETWORK [initandlisten] connection accepted from 192.168.1 ...
- zabbix邮件内容乱码与邮件内容为附件解决办法
在zabbix的实际使用过程中,在收到邮件预警的时候,我们会发现邮件内容是乱码的,在手机端收到的是附件,而且附件下载后的文件类型是打不开的.这样我们不知道我们是哪个服务器的哪项服务出了问题,接下来我们 ...