文件的上传和下载
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. html5 页面基本骨架

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  2. 大数据技术之_19_Spark学习_01_Spark 基础解析小结(无图片)

    1.准备安装包 2.Spark Standalone 即独立模式  2.1.解压安装包到你安装的目录.  2.2.拷贝 conf 目录下的 slaves 文件,将 slave 节点的 hostname ...

  3. 【11】JMicro微服务-配置管理

    如非授权,禁止用于商业用途,转载请注明出处作者:mynewworldyyl 往下看前,建议完成前面1到10小节 JMicro目前仅支持基于Zookeeper做配置管理,全部配置信息可以在ZK做增删改查 ...

  4. MySql登陆密码忘记-解决方案

    方法一:MySQL提供跳过访问控制的命令行参数,通过在命令行以此命令启动MySQL服务器: safe_mysqld --skip-grant-tables& 即可跳过MySQL的访问控制,任何 ...

  5. js操作符类型转换

    乘法 我们来看下前面的题目: console.dir(5*"5"); console.dir(5*"a");//NaN console.dir(5*NaN);/ ...

  6. postman—数据同步和创建测试集

    postman使用之二:数据同步和创建测试集 一.数据同步 启动postman 后在右上角可以登录账号,登录后就可以同步自己的api测试脚本,连上网在办公区在家都可以同步. 二.创建测试集 1.点击c ...

  7. Python2.x 中文乱码问题

    Python 文件中如果未指定编码,在执行过程会出现报错: #!/usr/bin/pythonprint "你好,世界"; 以上程序执行输出结果为: File "test ...

  8. Sklearn-RandomForest

    在scikit-learn中,RandomForest的分类类是RandomForestClassifier,回归类是RandomForestRegressor,需要调参的参数包括两部分,第一部分是B ...

  9. 在Pycharm中使用AutoPEP8来检验代码的规范性

    在学习python的过程中,我们经常会遇到代码不规范而导致的程序报错,代码的规范和严谨就显得尤为重要了,所以编写代码的过程中,我们需要遵循PE8规范,在Pycharm 中我们可以安装插件 AutoPE ...

  10. springboot-mongodb的多数据源配置

    pom.xml中引入mongodb的依赖 <dependency> <groupId>org.springframework.boot</groupId> < ...