Springboot+Vue實(shí)現(xiàn)批量文件上傳(pdf、word、excel)并支持在線預(yù)覽功能
點(diǎn)擊上方?web項(xiàng)目開發(fā),選擇?設(shè)為星標(biāo)
優(yōu)質(zhì)文章,及時送達(dá)
效果圖
前端初始頁面

上傳doc,docx,xls,xlsx,ppt,pptx,txt成功頁面

文件在線預(yù)覽頁面

環(huán)境介紹
JDK:1.8
數(shù)據(jù)庫:Mysql5.6
前端:Vue
后端:SpringBoot
完整源碼獲取
掃碼關(guān)注回復(fù)括號內(nèi)文字【辦公軟件】獲取源碼
如果你在運(yùn)行這個代碼的過程中有遇到問題,請加小編微信xxf960513,我拉你進(jìn)對應(yīng)微信學(xué)習(xí)群!!幫助你快速掌握這個功能代碼!
核心代碼介紹
pox.xml
<dependency><groupId>com.asposegroupId><artifactId>aspose-wordsartifactId><version>16.8.0version>dependency><dependency><groupId>com.asposegroupId><artifactId>aspose-cellsartifactId><version>8.5.2version>dependency><dependency><groupId>com.asposegroupId><artifactId>aspose-slidesartifactId><version>15.9.0version>dependency>
UploadParsePdfCtrler.class
package?com.yxyz.ctrler;import java.io.FileOutputStream;import java.io.InputStream;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.servlet.http.HttpServletRequest;import org.springframework.beans.factory.annotation.Value;import org.springframework.util.StringUtils;import org.springframework.web.bind.annotation.CrossOrigin;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import?org.springframework.web.multipart.MultipartFile;import com.yxyz.rest.CodeMsg;import com.yxyz.rest.Result;import com.yxyz.util.AsposeUtil;import com.yxyz.util.FileUtil;import com.yxyz.util.StringUtil;import io.swagger.annotations.Api;import?io.swagger.annotations.ApiOperation;/*** Copyright @ 2020 Zonlyn. All rights reserved.* @Description: 該類的功能描述**/public class UploadParsePdfCtrler{//源文件存儲位置private String savePath;//生成pdf緩存位置private String pdftemppath;//項(xiàng)目訪問名稱private String projName;public Object uploadToPdf(HttpServletRequest request,MultipartFile[] files) throws Exception{if(null == files || files.length == 0){return Result.error(CodeMsg.NOFILEUPLOAD);}//判斷是否配置了項(xiàng)目名稱projName = StringUtils.isEmpty(projName)?"":projName;//緩存 文件存儲名、源名稱對應(yīng)關(guān)系、預(yù)覽地址????ListFileUtil.checkExistDir(savePath);????FileUtil.checkExistDir(pdftemppath);for(MultipartFile file : files){/** 保存上傳文件*///文件存儲名//源文件名String orinName = file.getOriginalFilename();String preName = StringUtil.getUuid();String stuffName = orinName.substring(orinName.lastIndexOf("."));??????String?svName?=?preName?+?stuffName;byte[] cache = new byte[1024];int hasRead = 0;InputStream in = file.getInputStream();FileOutputStream out = new FileOutputStream(savePath+svName);while((hasRead=in.read(cache, 0, cache.length)) != -1){out.write(cache, 0, hasRead);}out.flush();if(null != out){out.close();}if(null != in){in.close();}/** 上傳文件轉(zhuǎn)換pdf,存儲至 ${web.upload-path}*/??????String?pdfSaveName?=?pdftemppath+preName+".pdf";??????AsposeUtil.trans(savePath+svName,?pdfSaveName);String httpUrl = request.getScheme() + "://" + request.getServerName()?????????+?":"?+?request.getServerPort()?+projName+"/"+?preName+".pdf";Maprs = new HashMap<>(); rs.put("oldname", orinName);rs.put("newname", preName+".pdf");??????rs.put("url",?httpUrl);??????saveName_orinName_Url.add(rs);?????}return Result.success(saveName_orinName_Url);}}
main.js
import?Vue?from?'vue'import?'normalize.css/normalize.css'?//?A?modern?alternative?to?CSS?resetsimport ElementUI from 'element-ui'import 'element-ui/lib/theme-chalk/index.css'//?import?locale?from?'element-ui/lib/locale/lang/en'?//?lang?i18nimport?'@/styles/index.scss'?//?global?cssimport App from './App'import store from './store'import router from './router'import '@/icons' // iconimport?'@/permission'?//?permission?control/*** If you don't want to use mock-server* you want to use MockJs for mock api* you can execute: mockXHR()** Currently MockJs will be used in the production environment,* please remove it before going online ! ! !*/if (process.env.NODE_ENV === 'production') {const { mockXHR } = require('../mock')mockXHR()}// set ElementUI lang to ENVue.use(ElementUI)// 如果想要中文版 element-ui,按如下方式聲明//?Vue.use(ElementUI)Vue.config.productionTip?=?falsenew Vue({el: '#app',router,store,render: h => h(App)})
index.vue
<template><div class="dashboard-container"><el-uploadref="upload"multipleclass="upload-demo"action="http://139.159.147.237:8080/yxyz/filetopaf/uploadtopdf":on-preview="handlePreview":on-remove="handleRemove":file-list="fileList":before-upload="beforeUpload"list-type="picture"><el-button slot="trigger" size="small" type="primary">選取文件el-button><div slot="tip" class="el-upload__tip">可以上傳doc/xlsx/pdfdiv>????el-upload>div>template><script>export default {name: 'Dashboard',components: { },data() {return {fileList: [],dialogVisible: false,??????pdfUrl:?''}},computed: {},created() {this.fileArr = []},??methods:?{handleRemove(file, fileList) {console.log(file, fileList)},handlePreview(file) {console.log(file, 'hha')// if (file.raw.type === 'application/pdf') {// this.pdfUrl = file.response.data[0].url// this.dialogVisible = true// return// }window.open(file.response.data[0].url)},beforeUpload(file) {console.log(file)????}}}script><style lang="scss" scoped>.dashboard {&-container {margin: 30px;}&-text {font-size: 30px;line-height: 46px;}}style>
--完--
如果你覺得這個案例以及我們的分享思路不錯,對你有幫助,請分享給身邊更多需要學(xué)習(xí)的朋友。別忘了《留言+點(diǎn)在看》給作者一個鼓勵哦!
1、springboot+mybatis+vue前后端分離實(shí)現(xiàn)用戶登陸注冊功能
2、SpringBoot+Vue前后分離實(shí)現(xiàn)郵件發(fā)送功能
3、SpringBoot+Spring Data JPA+Vue前后端分離實(shí)現(xiàn)分頁功能
4、SpringBoot+Spring Data JPA+Vue前后端分離實(shí)現(xiàn)Excel導(dǎo)出功能
5、Spring Boot + Vue前后端分離實(shí)現(xiàn)圖片上傳功能
6、springboot+jpa+tymeleaf實(shí)現(xiàn)分頁功能
7、springboot+jpa+thymeleaf實(shí)現(xiàn)信息修改功能
8、SpringBoot+vue開發(fā)微信小程序留言功能
9、SpringBoot實(shí)現(xiàn)生成帶參數(shù)的小程序二維碼功能
10、springboot+jpa+thymeleaf實(shí)現(xiàn)信息增刪改查功能
11、用java實(shí)現(xiàn)Graphics2D繪制驗(yàn)證碼功能
12、Springboot+layui前后端分離實(shí)現(xiàn)word轉(zhuǎn)pdf功能
13、用java將本地圖片轉(zhuǎn)base64格式, 再轉(zhuǎn)圖片!你用過這個功能?
14、springboot+layui+thymelefe實(shí)現(xiàn)用戶批量添加功能
15、springboot+Tymeleaf實(shí)現(xiàn)用戶密碼MD5加鹽解密登錄
16、springboot+vue實(shí)現(xiàn)用戶注冊后必須通過郵箱激活才能登錄激活才能登錄
17、SpringBoot+Vue實(shí)現(xiàn)用戶頭像修改功能
18、Springboot+Vue實(shí)現(xiàn)富文本發(fā)表文章功能
19、springboot+vue實(shí)現(xiàn)不同管理權(quán)限的用戶登陸展示的菜單欄目不同功能
20、Springboot+vue實(shí)現(xiàn)上傳視頻并在線播放功能
21、SpringBoot+Vue前后端分離實(shí)現(xiàn)郵件定時發(fā)送功能
22、springboot+vue實(shí)現(xiàn)多圖片同時上傳功能
23、Springboot+Vue前后端分離實(shí)現(xiàn)Excle文件導(dǎo)入并在前端頁面回顯功能
24、Springboot+Vue實(shí)現(xiàn)從數(shù)據(jù)庫中獲取數(shù)據(jù)生成樹狀圖在前端頁面展示功能
25、Springboot+Vue實(shí)現(xiàn)從數(shù)據(jù)庫中獲取數(shù)據(jù)生成餅狀圖并在前端頁面展示功能
為了方便大家更好的學(xué)習(xí),本公眾號經(jīng)常分享一些完整的單個功能案例代碼給大家去練習(xí),如果本公眾號沒有你要學(xué)習(xí)的功能案例,你可以聯(lián)系小編(微信:xxf960513)提供你的小需求給我,我安排我們這邊的開發(fā)團(tuán)隊(duì)免費(fèi)幫你完成你的案例。
注意:只能提單個功能的需求不能要求功能太多,比如要求用什么技術(shù),有幾個頁面,頁面要求怎么樣?
