springboot整合vue實現(xiàn)上傳下載文件
springboot整合vue實現(xiàn)上傳下載文件,供大家參考,具體內(nèi)容如下
環(huán)境
springboot 1.5.x
完整代碼下載:springboot整合vue實現(xiàn)上傳下載
1、上傳下載文件api文件
設(shè)置上傳路徑,如例子:
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 + "”是否存在");
}
//復(fù)制
IOUtils.copy(is, response.getOutputStream());
response.getOutputStream().flush();
} catch (IOException e) {
return ResultUtil.error("下載附件失敗,error:"+e.getMessage());
}
//文件的關(guān)閉放在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.上傳大文件配置
/**
* 設(shè)置上傳大文件大小,配置文件屬性設(shè)置無效
*/
@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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺談SpringMVC的攔截器(Interceptor)和Servlet 的過濾器(Filter)的區(qū)別與聯(lián)系 及Spr
這篇文章主要介紹了淺談SpringMVC的攔截器(Interceptor)和Servlet 的過濾器(Filter)的區(qū)別與聯(lián)系 及SpringMVC 的配置文件,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2020-07-07
詳解在SpringBoot中@Transactional事物操作和事物無效問題排查
這篇文章主要介紹了詳解在SpringBoot中@Transactional事物操作和事物無效問題排查,本文詳細的介紹了SpringBoot中集成使用@Transactional注解操作事物以及事物開啟后無效的問題排查,需要的朋友可以參考下2021-06-06
Java源碼解析HashMap的tableSizeFor函數(shù)
今天小編就為大家分享一篇關(guān)于Java源碼解析HashMap的tableSizeFor函數(shù),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01
java 線程中start方法與run方法的區(qū)別詳細介紹
這篇文章主要介紹了java 線程中start方法與run方法的區(qū)別詳細介紹的相關(guān)資料,在java線程中調(diào)用start方法與run方法的區(qū)別在哪里? 這兩個問題是兩個非常流行的初學(xué)者級別的多線程面試問題,這里進行詳細說明,需要的朋友可以參考下2016-11-11
spring?java?動態(tài)獲取consul?K/V的方法
這篇文章主要介紹了spring?java?動態(tài)獲取consul?K/V的相關(guān)資料,主要包括springConsul配置kv路徑以及自動注入consulKV到服務(wù)中,本文給大家介紹的非常詳細,需要的朋友可以參考下2023-10-10
SSM使用mybatis分頁插件pagehepler實現(xiàn)分頁示例
本篇文章主要介紹了SSM使用mybatis分頁插件pagehepler實現(xiàn)分頁示例,使用分頁插件的原因,簡化了sql代碼的寫法,實現(xiàn)較好的物理分頁,非常具有實用價值,需要的朋友可以參考下2018-03-03
java ArrayList.remove()的三種錯誤用法以及六種正確用法詳解
這篇文章主要介紹了java ArrayList.remove()的三種錯誤用法以及六種正確用法詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2020-01-01

