spring boot实现文件上传下载以及多文件上传
来源:互联网 发布:windows 无线触摸板 编辑:程序博客网 时间:2024/05/21 12:41
首先是很简单的界面,在resource/static下创建文件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> <title>Hello World!</title></head><body><form method="POST" enctype="multipart/form-data" action="/upload"> <p>文件:<input type="file" name="file" /></p> <p><input type="submit" value="上传" /></p></form><br><br><br><form method="POST" enctype="multipart/form-data" action="/batch/upload"> <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><br><br><br><a href="/fileDownload">下载</a></body></html>
//然后是controller层package com.example.myproject.web;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.multipart.MultipartFile;import org.springframework.web.multipart.MultipartHttpServletRequest;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.*;import java.util.List;/** * Created by lenovo on 2017/4/27. */@Controllerpublic class FileController { /** * 文件上传具体实现方法; * @param file * @return */ @RequestMapping("/upload") @ResponseBody public String handleFileUpload(@RequestParam("file")MultipartFile file){ if(!file.isEmpty()){ try { String fileName = file.getOriginalFilename(); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File("F:\\files\\" + fileName))); out.write(file.getBytes()); out.flush(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); return"上传失败,"+e.getMessage(); } catch (IOException e) { e.printStackTrace(); return"上传失败,"+e.getMessage(); } return"上传成功"; }else{ return"上传失败,因为文件是空的."; } } @RequestMapping(value = "/fileDownload") @ResponseBody public String download(HttpServletResponse response) throws Exception { BufferedInputStream bis = null; BufferedOutputStream bos = null; //获取下载文件露肩 String downLoadPath = "F:\\迅雷下载\\day13\\avi\\13.01_常见对象(StringBuffer的概述).avi"; /*response.setHeader("content-type", "application/octet-stream"); //获取文件的长度 long fileLength = new File(downLoadPath).length(); //设置文件输出类型 response.setContentType("application/octet-stream"); //设置输出长度 response.setHeader("Content-Length", String.valueOf(fileLength));*/ /********************************************************************/ //获取输入流 bis = new BufferedInputStream(new FileInputStream(downLoadPath)); //输出流 bos = new BufferedOutputStream(new FileOutputStream(new File("F:\\1.avi"))); byte[] buff = new byte[2048]; int bytesRead; while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { bos.write(buff, 0, bytesRead); } //关闭流 bis.close(); bos.close(); return "下载成功"; } @RequestMapping(value="/batch/upload", method= RequestMethod.POST) @ResponseBody public String handleFileUpload(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 { BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File("F:\\files\\" + file.getOriginalFilename()))); bufferedOutputStream.write(file.getBytes()); bufferedOutputStream.flush(); bufferedOutputStream.close(); } catch (Exception e) { return "You failed to upload " + i + " => " + e.getMessage(); } } else { return "You failed to upload " + i + " because the file was empty."; } } return "上传成功"; }}
//我们要设置一些属性//在application.properties
#文件上传配置spring.http.multipart.max-file-size=32MBspring.http.multipart.max-request-size=128MB#启动程序,访问http://localhost:8080/file.html即可
0 0
- spring boot实现文件上传下载以及多文件上传
- Spring Boot实现文件上传
- spring boot文件的上传下载
- Spring boot 文件上传(多文件上传)
- spring boot文件上传【多文件上传】
- Spring boot 文件上传
- spring boot 文件上传
- Spring-boot 文件上传
- Spring Boot 文件上传
- spring-boot 文件上传
- spring boot+angularjs实现文件上传
- Spring mvc实现文件上传下载
- Spring MVC实现文件上传下载
- Spring MVC实现文件上传下载
- SpringBoot:spring boot文件上传【多文件上传】
- Spring boot 文件上传(多文件上传)
- spring boot文件上传、下载
- 使用spring boot 上传文件
- hihoCoder 1051 : 补提交卡 枚举
- Coursera Algorithms Percolation解题记录
- 聊天机器人_ _突击01编队_ _浅聊
- uva 658 It's not a Bug, it's a Feature! 最短路dijstra
- Interface和abstract类的区别
- spring boot实现文件上传下载以及多文件上传
- 自定义adapter notifyDataSetChanged失效的解决
- Android6.0 有关权限的研究(第二版)
- 面经
- 关于java多态的解释学习
- Angular入门级教程三 Angular 中的控制器(controller)
- 洛谷 1417——烹调方案【动规】
- ehcache memcache redis 比较
- linux安装chrome,chrome安装flash player,firefox安装flash player