SpringBoot下文件上传与下载的实现
原文:http://blog.csdn.net/colton_null/article/details/76696674
SpringBoot后台如何实现文件上传下载?
最近做的一个项目涉及到文件上传与下载。前端上传采用百度webUploader插件。有关该插件的使用方法还在研究中,日后整理再记录。本文主要介绍SpringBoot后台对文件上传与下载的处理。
单文件上传
// 单文件上传 @RequestMapping(value = "/upload") @ResponseBody public String upload(@RequestParam("file") MultipartFile file) {try {if (file.isEmpty()) {return "文件为空";}// 获取文件名String fileName = file.getOriginalFilename();logger.info("上传的文件名为:" + fileName);// 获取文件的后缀名String suffixName = fileName.substring(fileName.lastIndexOf("."));logger.info("文件的后缀名为:" + suffixName);// 设置文件存储路径String filePath = "D://aim//";String path = filePath + fileName + suffixName;File dest = new File(path);// 检测是否存在目录if (!dest.getParentFile().exists()) {dest.getParentFile().mkdirs();// 新建文件夹 }file.transferTo(dest);// 文件写入return "上传成功";} catch (IllegalStateException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return "上传失败"; }
如果想要修改文件路径及文件名,修改filePath以及fileName即可。
多文件上传
//多文件上传 @RequestMapping(value = "/uploadMore", 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);String filePath = "D://aim//";if (!file.isEmpty()) {try {byte[] bytes = file.getBytes();stream = new BufferedOutputStream(new FileOutputStream(new File(filePath + file.getOriginalFilename())));//设置文件路径及名字stream.write(bytes);// 写入 stream.close();} catch (Exception e) {stream = null;return "第 " + i + " 个文件上传失败 ==> "+ e.getMessage();}} else {return "第 " + i+ " 个文件上传失败因为文件为空";}}return "上传成功"; }
文件下载
//文件下载相关代码 @RequestMapping("/download") public String downloadFile(HttpServletRequest request, HttpServletResponse response) {String fileName = "aim_test.txt";// 设置文件名,根据业务需要替换成要下载的文件名if (fileName != null) {//设置文件路径String realPath = "D://aim//"File file = new File(realPath , fileName);if (file.exists()) {response.setContentType("application/force-download");// 设置强制下载不打开response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名byte[] buffer = new byte[1024];FileInputStream fis = null;BufferedInputStream bis = null;try {fis = new FileInputStream(file);bis = new BufferedInputStream(fis);OutputStream os = response.getOutputStream();int i = bis.read(buffer);while (i != -1) {os.write(buffer, 0, i);i = bis.read(buffer);}System.out.println("success");} catch (Exception e) {e.printStackTrace();} finally {if (bis != null) {try {bis.close();} catch (IOException e) {e.printStackTrace();}}if (fis != null) {try {fis.close();} catch (IOException e) {e.printStackTrace();}}}}}return null; }
springboot 文件上传大小配置
application.properties中添加
SpringBoot下文件上传与下载的实现相关推荐
- 微信小程序+SpringBoot实现文件上传与下载
微信小程序+SpringBoot实现文件上传与下载 1.文件上传 1.1 后端部分 1.1.1 引入Apache Commons FIleUpload组件依赖 1.1.2 设置上传文件大小限制 1.1 ...
- 一篇文章教你学会使用SpringBoot实现文件上传和下载
文章目录 一.搭建SpringBoot开发环境 1.创建项目 2.配置application.properties参数 3.实体响应类和异常信息类 4.创建FileController 二.接口测试 ...
- SpringBoot实现文件上传和下载
文件上传需要使用到 MultipartResolver接口. Spring MVC 使用 MultipartResolver接口的实现类:CommonsMultipartResolver .Commo ...
- springboot+web文件上传和下载
一.首先安装mysql数据库,开启web服务器. 二.pom.xml文件依赖包配置如下: <?xml version="1.0" encoding="UTF-8&q ...
- asp.net coree文件上传与下载实例
asp.net core像springboot非常相似,我们从一个springboot文件上传与下载的例子来引到asp.net core的文件上传与下载: 本文asp.net core版本为:6.0 ...
- SpringBoot整合阿里云OSS文件上传、下载、查看、删除
SpringBoot整合阿里云OSS文件上传.下载.查看.删除 该项目源码地址:https://github.com/ggb2312/springboot-integration-examples ( ...
- springBoot 简单优雅是实现文件上传和下载
前言 好久没有更新spring Boot 这个项目了.最近看了一下docker 的知识,后期打算将spring boot 和docker 结合起来.刚好最近有一个上传文件的工作呢,刚好就想起这个脚手架 ...
- SpringBoot+MongoDB GridFS文件上传、下载、预览实战
SpringBoot + MongoDB GridFS 随着web 3.0的兴起,数据的形式不局限于文字,还有语音.视频.图片等.高效存储与检索二进制数据也成为web 3.0必须要考虑的问题.然而这种 ...
- springboot文件上传、下载使用ftp工具将文件上传至服务器
springboot文件上传.下载使用ftp工具 首先在服务器搭建ftp服务 配置文件(在application.properties中) # Single file max size multipa ...
最新文章
- rtop – 通过SSH监控远程主机
- 1-2 Zabbix web界面支持中文
- 功能分支重新设置后,Git推送被拒绝
- uclibc和glibc的差别
- jmeter模拟http请求/发送gzip数据
- hadoop安装,提前确认hadoop版本是32位还是64位。
- Java笔记-以系统时间为基准15分钟运行一次指定代码
- 测试项目开源_测验您对开源的承诺
- linux 进程退出原因,linux – 为什么waitpid不等待进程退出?
- 各种变换的原理----DX版本
- 关于static继承的问题
- 在Array原型链上扩展remove,contain等方法所遇到的坑
- 三星S4 GT-I9500 Google服务包及刷机教程
- phpQuery乱码解决经验分享
- 目前比较流行的网站开发框架
- php判断360浏览器是否是兼容模式,JS判断是否360安全浏览器极速内核的方法
- sd卡umount时busy解决方法
- Kteer软件 创建.ktr文件
- play框架用起来(1)
- 初二上册计算机编程入门先学什么,8年级以上学生必读,这项AP课程带你零基础入门编程!...
热门文章
- php安装gb,php安装程序的原理
- vue element-ui Notification 挤在一起,重叠问题 解决办法
- 行人检测--What Can Help Pedestrian Detection?
- 【Dual-Path-RNN-Pytorch源码分析】Segmentation
- miui12 android版本,miui12基于安卓几版本开发的?miui12是安卓11吗
- Spring导入配置类或文件
- 2021护理正高考试成绩查询,中国卫生人才网:2021年护资考试成绩现可查询!
- 【Netty】入门Netty官方例子解析(一)写个 Discard Server
- python中函数的作用域_Python中的函数作用域
- 最常用的Python爬虫和数据分析常用第三方库,收藏吧