springboot整合vue实现上传下载文件
作者:SingleOneMan 发布时间:2023-11-14 07:10:37
标签:springboot,vue,上传,下载
springboot整合vue实现上传下载文件,供大家参考,具体内容如下
环境
springboot 1.5.x
完整代码下载:springboot整合vue实现上传下载
1、上传下载文件api文件
设置上传路径,如例子:
private final static String rootPath =
System.getProperty(“user.home”)+File.separator+fileDir+File.separator;
api接口:
下载url示例:http://localhost:8080/file/download?fileName=新建文本文档.txt
//上传不要用@Controller,用@RestController
@RestController
@RequestMapping("/file")
public class FileController {
private static final Logger logger = LoggerFactory.getLogger(FileController.class);
//在文件操作中,不用/或者\最好,推荐使用File.separator
private final static String fileDir="files";
private final static String rootPath = System.getProperty("user.home")+File.separator+fileDir+File.separator;
@RequestMapping("/upload")
public Object uploadFile(@RequestParam("file") MultipartFile[] multipartFiles, final HttpServletResponse response, final HttpServletRequest request){
File fileDir = new File(rootPath);
if (!fileDir.exists() && !fileDir.isDirectory()) {
fileDir.mkdirs();
}
try {
if (multipartFiles != null && multipartFiles.length > 0) {
for(int i = 0;i<multipartFiles.length;i++){
try {
//以原来的名称命名,覆盖掉旧的
String storagePath = rootPath+multipartFiles[i].getOriginalFilename();
logger.info("上传的文件:" + multipartFiles[i].getName() + "," + multipartFiles[i].getContentType() + "," + multipartFiles[i].getOriginalFilename()
+",保存的路径为:" + storagePath);
Streams.copy(multipartFiles[i].getInputStream(), new FileOutputStream(storagePath), true);
//或者下面的
// Path path = Paths.get(storagePath);
//Files.write(path,multipartFiles[i].getBytes());
} catch (IOException e) {
logger.error(ExceptionUtils.getFullStackTrace(e));
}
}
}
} catch (Exception e) {
return ResultUtil.error(e.getMessage());
}
return ResultUtil.success("上传成功!");
}
/**
* http://localhost:8080/file/download?fileName=新建文本文档.txt
* @param fileName
* @param response
* @param request
* @return
*/
@RequestMapping("/download")
public Object downloadFile(@RequestParam String fileName, final HttpServletResponse response, final HttpServletRequest request){
OutputStream os = null;
InputStream is= null;
try {
// 取得输出流
os = response.getOutputStream();
// 清空输出流
response.reset();
response.setContentType("application/x-download;charset=GBK");
response.setHeader("Content-Disposition", "attachment;filename="+ new String(fileName.getBytes("utf-8"), "iso-8859-1"));
//读取流
File f = new File(rootPath+fileName);
is = new FileInputStream(f);
if (is == null) {
logger.error("下载附件失败,请检查文件“" + fileName + "”是否存在");
return ResultUtil.error("下载附件失败,请检查文件“" + fileName + "”是否存在");
}
//复制
IOUtils.copy(is, response.getOutputStream());
response.getOutputStream().flush();
} catch (IOException e) {
return ResultUtil.error("下载附件失败,error:"+e.getMessage());
}
//文件的关闭放在finally中
finally
{
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
logger.error(ExceptionUtils.getFullStackTrace(e));
}
try {
if (os != null) {
os.close();
}
} catch (IOException e) {
logger.error(ExceptionUtils.getFullStackTrace(e));
}
}
return null;
}
}
访问:http://localhost:8080
上传:
批量上传:
下载:
2.上传大文件配置
/**
* 设置上传大文件大小,配置文件属性设置无效
*/
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory config = new MultipartConfigFactory();
config.setMaxFileSize("1100MB");
config.setMaxRequestSize("1100MB");
return config.createMultipartConfig();
}
3.vue前端主要部分
<template>
<div style="top:100px;width:300px">
<el-form :model="form" label-width="220px">
<el-form-item label="请输入文件名" required>
<el-input v-model="form.fileName" auto-complete="off" class="el-col-width" required></el-input>
</el-form-item>
<el-form-item>
<el-button size="small" type="primary" @click="handleDownLoad">下载</el-button>
</el-form-item>
<el-form-item>
<el-upload class="upload-demo" :action="uploadUrl" :before-upload="handleBeforeUpload" :on-error="handleUploadError" :before-remove="beforeRemove" multiple :limit="5" :on-exceed="handleExceed" :file-list="fileList">
<el-button size="small" type="primary">点击上传</el-button>
<div slot="tip" class="el-upload__tip">一次文件不超过1Gb</div>
</el-upload>
</el-form-item>
</el-form>
</div>
</template>
来源:https://blog.csdn.net/yhhyhhyhhyhh/article/details/89888953


猜你喜欢
- BufferedReader读取本地文件在使用BufferedWriter写入文件时,如果忘记关闭文件(close)同时也没有调用flush
- 主要实现的功能:1.程序附带多张拼图随机拼图。2.可手动添加拼图。3.游戏成功判断。4.30秒超时判断。 Puzzle.csus
- 接收 / 返回文本消息①接收/返回文本消息原理说明当普通微信用户向公众账号发消息时,微信服务器将POST消息的XML数据包到开发者填写的UR
- 前言Android 很多场合需要使用到数据加密,比如:本地登录密码加密,网络传输数据加密,等。在android 中一般的加密方式有如下:亦或
- 我就废话不多说了,大家还是直接看代码吧~package com.zejian.annotationdemo; import java.lan
- 好多时候,我们都需要知道某些目录下的文件什么时候被修改、删除过等,如果能用miniFilter驱动过滤来做的话当然是最好不过了,这是内核级别
- 一:在函数入参中使用通配符@AspectJ支持3种通配符* :匹配任意字符,但它只能匹配上下文中的一个元素... :匹配任意字符,可以匹配上
- 1.过滤器:所谓过滤器顾名思义是用来过滤的,在java web中,你传入的request,response提前过滤掉一些信息,或者提前设置一
- 本文实例讲述了Android实现在一个activity中添加多个listview的方法。分享给大家供大家参考,具体如下:listview的i
- 银行卡大家都使用,在密码输错超过限制次数之后,就容易被锁死,智能到银行柜台才能解锁,那么这一功能如果实现的呢,今天小编通过实例代码给大家详细
- 概述背景函数式编程的理论基础是阿隆佐·丘奇(Alonzo Church)于 1930 年代提出的 &lambd
- 1、通过C#调用Java的方法:在C#中添加调用的一些代码,利用Unity提供的一些接口实现调用Java!private const str
- //方法一//须添加对System.Web的引用//using System.Web.Security;/// <summary>
- 1.java过滤器过滤允许整个项目跨域访问,可通过filter来进行过虑:public class SimpleCORSFilter imp
- 构造http headerprivate static final String URL = "url";private
- 本文介绍了Android 实现截屏方式整理,分享给大家。希望对大家有帮助可能的需求:截自己的屏截所有的屏带导航栏截屏不带导航栏截屏截屏并编
- 在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的。但是在通过了Ap
- 之前的一篇文章中的代码中有一个using的用法,刚开始查看了一些资料说是强制关闭对象的一个命令。今天又查了一些资料,才明白,原来using指
- 第一步,导jar包<!--Email--> <dependency&
- 在学习C#语言的时候,首先要学习控制台的应用程序,这样才能专注于语言的学习,减少学习的梯度,也有利于输出自己需要输出的内容。因此第一步学习C