文件的上传和下载
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、文件上传(第二中方法)
  1. @RequestMapping("/upload1")
  2. @ResponseBody
  3. public String upload1(@RequestParam("file")MultipartFile file,HttpServletRequest request) throws IOException {
  4. //获取文件名
  5. String filename=file.getOriginalFilename();
  6. //文件上传后路径
  7. String path="E://images//";
  8. File file1=new File(path+filename);
  9. //如果文件不存在,则创建新的文件夹
  10. if(!file1.getParentFile().exists()){
  11. file1.getParentFile().mkdirs();
  12. }
  13. //上传的文件存放的位置
  14. file.transferTo(file1);
  15. return "ok";
  16. }
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 文件上传和下载的更多相关文章

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

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

  2. Springboot文件上传与下载

    一.创建简单的springboot-web项目 二.文件上传属性配置 #默认支持文件上传 spring.http.multipart.enabled =true spring.http.multipa ...

  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. 【LeetCode】502. IPO

    题目 假设 LeetCode 即将开始其 IPO.为了以更高的价格将股票卖给风险投资公司,LeetCode希望在 IPO 之前开展一些项目以增加其资本. 由于资源有限,它只能在 IPO 之前完成最多 ...

  2. Python生成gexf文件并导入gephi做网络图分析

    Gephi是一款优秀的复杂网络分析软件,支持导入多种格式的文件.gexf格式是Gephi 推荐的格式,基于 XML.本文是一个用python写的简单Demo,示例如何生成一个典型的gexf格式文件.代 ...

  3. GDAL VS2010 win7(64位)安装、使用说明(图文解析)

    一.电脑配置及安装版本 Win 7(64位机) Visual Studio 2010 GDAL 1.9.2(我也尝试了最新版GDAL1.11.0,应该同样可以用的,只是在重新配置时又选用了老一点的版本 ...

  4. Class与Style绑定

    本文主要介绍如何使用Vue来绑定操作元素的class列表和内联样式(style属性). 因为class和style都是属性,所以通过v-bind命令来处理它们:只需要通过表达式计算出结果即可,不过字符 ...

  5. 【jQuery源码】preFilter

    preFilter: { "ATTR": function( match ) { //属性名解码 match[1] = match[1].replace( runescape, f ...

  6. Cloudera Manager安装之利用parcels方式安装3或4节点集群(包含最新稳定版本或指定版本的安装)(添加服务)(CentOS6.5)(五)

    参考博客 Cloudera Manager安装之利用parcels方式安装单节点集群  Cloudera Manager安装之Cloudera Manager 5.3.X安装(三)(tar方式.rpm ...

  7. ES6-Iterator & for...of循环

    依赖文件地址 :https://github.com/chanceLe/ES6-Basic-Syntax/tree/master/js <!DOCTYPE html> <html&g ...

  8. tar 命令压缩时报错 tar: Removing leading `/' from member names

    在使用tar命令进行压缩打包的时候我们常常会遇到下面的错误.虽然它不会影响我们最后的压缩打包,但是间接说明了我们的命令是有问题的.接下来我们来看看解决的方法. 报错内容: [root@haha ~]# ...

  9. Linux-(diff)

    diff 命令 1.命令格式: diff  [参数]  [文件1或目录1]  [文件2或目录2] 2.命令功能: diff命令能比较单个文件或者目录内容.如果指定比较的是文件,则只有当输入为文本文件时 ...

  10. printf()的转换说明的修饰符中的标记、数字、和.数字

    先记下代码和运行结果 再解释 #include <stdio.h> #include <stdlib.h> #include <limits.h> #define ...