文件的上传和下载
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. git 查看提交记录

    查看提交的内容 -p 选项,同时在 - 后加数字限制一下数目 git log -p -2. commit 500eeadd71a21f1166803e12a792bfa86f4ca784 (HEAD ...

  2. JIRA Rest JAVA Client API实现问题管理及自定义字段(原创)

    JIRA是一个缺陷跟踪管理系统,被广泛应用于缺陷跟踪.客户服务.需求收集.流程审批.任务跟踪.项目跟踪和敏捷管理等工作领域,当我们需要把第三方业务系统集成进来时,可以调用他的API. JIRA本身的A ...

  3. css 题目笔记(本文收集了一些个人觉得比较有意思的css题目,欢迎大家给出自己的解答)

    1.本文收集了一些个人觉得比较有意思的css题目,欢迎大家给出自己的解答 P标签的最大宽度不可以大于H2标签文字宽度的10% 这里应该是P标签的最大宽度由前面的匿名内联元素宽度(就是大字号文字宽度)决 ...

  4. 给button添加UAC的小盾牌图标

    Sample Code: public partial class Form1 : Form { public Form1() { InitializeComponent(); } private v ...

  5. [LeetCode]SetMatrix Zero

    题目说明 Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. c ...

  6. 小程序之底部tabBar

    用法简介: 1.app.json中配置下tabBar即可,注意tabBar至少需要两个最多五个Item选项 这里简单列举一些属性值:对于tabBar整体属性设置: 对于tabBar中每个Item属性设 ...

  7. google tensorflow bert代码分析

    参考网上博客阅读了bert的代码,记个笔记.代码是 bert_modeling.py 参考的博客地址: https://blog.csdn.net/weixin_39470744/article/de ...

  8. 169.254地址无网关信息 ----- 解决方案 启动DHCP服务

    169.254.X.X是Windows操作系统在DHCP信息租用失败时自动给客户机分配的IP地址,.看到地址的时候没有网关. 解决方案   启动DHCP服务 1. 首先确认 路由器  DHCP 服务已 ...

  9. JavaScript文档对象模型

    文档对象模型(Document Object Model, DOM)是W3C提出的用于访问和修改文档的接口. JavaScript设计的初衷是为Web提供交互功能,它通过DOM接口来访问和修改文档. ...

  10. JDK动态代理[3]----WeakCache缓存的实现机制

    上一篇我们分析了Proxy类的内部是怎样产生代理类的,我们看到了Proxy内部用到了缓存机制,如果根据提供的类加载器和接口数组能在缓存中找到代理类就直接返回该代理类,否则会调用ProxyClassFa ...