色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

springboot+vue實現文件上傳下載

瀏覽:12日期:2022-10-25 15:56:48

本文實例為大家分享了springboot+vue實現文件上傳下載的具體代碼,供大家參考,具體內容如下

一、文件上傳(基于axios的簡單上傳)

所使用的技術:axios、springboot、vue;實現思路:通過h5 :input元素標簽進行選擇文件,獲取所選選擇的文件路徑,new fromdata對象,設置fromdata的參數,設置axios對應的請求頭,最后通過axios發送post請求后端服務。后端服務同過MultipartFile進行文件接收。具體代碼如下:

前端代碼:

1、創建vue對象

import Vue from ’vue’import App from ’./App.vue’import router from ’./router’import store from ’./store’import http from ’axios’Vue.config.productionTip = false;Vue.prototype.$http=http;window.vm=new Vue({ router, store, render: h => h(App)}).$mount(’#app’)

2、實現上傳組件

在input標簽中添加改變事件監聽,當發生改變時調用up方法。

<template> <div class='hello'> <input name='file' type='file' accept='image/png, image/gif, image/jpeg' @change='up' /> </div></template><script>export default { name: 'HelloWorld', props: { msg: String }, methods: { up(e) { let file = e.target.files[0]; alert(file.name); console.log(file); let param = new FormData(); //創建form對象 param.append('file', file); //通過append向form對象添加數據 console.log(param.get('file')); //FormData私有類對象,訪問不到,可以通過get判斷值是否傳進去 let config = { headers: { 'Content-Type': 'multipart/form-data' } }; //添加請求頭 this.$http .post('http://127.0.0.1:8081/data/up', param, config) .then(response => { console.log(response.data); }).catch( error=>{ alert('失敗'); } ); } }};</script><!-- Add 'scoped' attribute to limit CSS to this component only --><style scoped lang='less'></style>

后端代碼:

上傳文件代碼

@RequestMapping(value = '/up', method = RequestMethod.POST) @ResponseBody public Result<String> uploade(@RequestParam('file') MultipartFile file) { try { log.error('開始上傳?。?!'); String originalFilename = file.getOriginalFilename(); InputStream inputStream = file.getInputStream(); String path='d:/2020test/'; File file1 = new File(path + originalFilename); if(!file1.getParentFile().exists()){ file1.getParentFile().mkdirs(); } file.transferTo(file1); log.info('上傳成功!'); } catch (IOException e) { e.printStackTrace(); } Result<String> stringResult = new Result<String>(); stringResult.setMsg('sue'); stringResult.setData('file'); return stringResult; }

二、文件下載

通過response輸出流返回文件內容,核心代碼設置下載文件的名字(res.setHeader(“Content-Disposition”, “attachment;filename=” + java.net.URLEncoder.encode(realFileName.trim(), “UTF-8”));)

@RequestMapping(value = '/get', method = RequestMethod.GET) public void downloadFile(HttpServletResponse res) { String realFileName='C:/Users/xiongyi/Desktop/12.xls'; File excelFile = new File(realFileName); res.setCharacterEncoding('UTF-8'); res.setHeader('content-type', 'application/octet-stream;charset=UTF-8'); res.setContentType('application/octet-stream;charset=UTF-8'); //加上設置大小下載下來的.xlsx文件打開時才不會報“Excel 已完成文件級驗證和修復。此工作簿的某些部分可能已被修復或丟棄”// res.addHeader('Content-Length', String.valueOf(excelFile.length())); try { res.setHeader('Content-Disposition', 'attachment;filename=' + java.net.URLEncoder.encode(realFileName.trim(), 'UTF-8')); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } byte[] buff = new byte[1024]; BufferedInputStream bis = null; OutputStream os = null; try { os = res.getOutputStream(); bis = new BufferedInputStream(new FileInputStream(new File(realFileName))); int i = bis.read(buff); while (i != -1) { os.write(buff, 0, buff.length); os.flush(); i = bis.read(buff); } } catch (IOException e) { e.printStackTrace(); } finally { if (bis != null) { try { bis.close(); } catch (IOException e) { } } } Result<String> stringResult = new Result<String>(); stringResult.setMsg('sue'); stringResult.setData('nimabi');}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 国产手机在线小视频免费观看 | 91精选视频在线观看 | 香港经典毛片a免费观看 | 久久久精品国产免费观看同学 | 国产精品黄 | 手机在线色 | 欧美日韩国产va另类 | 免费国产高清精品一区在线 | 91成人精品视频 | 国产永久在线观看 | 久久精品视频99精品视频150 | 天堂影院jav成人天堂免费观看 | 国产成人精品视频免费大全 | 中文字幕一区二区三区久久网站 | 深夜福利视频在线看免费 | 一色屋成人免费精品网 | 熟女毛片| 国产免费一区二区三区 | 欧美成在线视频 | 一级毛片观看 | 亚洲国产成人久久综合野外 | 96精品视频在线播放免费观看 | 91色综合久久 | 一级毛片在线播放 | 成人av手机在线观看 | 国产成人精品亚洲 | 小屁孩和大人啪啪 | 国产精品亚洲二区 | 2022免费国产精品福利在线 | 人碰人碰人成人免费视频 | 91精品国产免费久久 | 亚洲欧美中文字幕在线网站 | 一级黄片一级毛片 | 欧美精品三区 | 国产一区在线免费观看 | 国产aⅴ一区二区 | 视频二区好吊色永久视频 | 中文精品久久久久国产网址 | 26uuu影院亚洲欧美综合 | 成人欧美日韩视频一区 | 亚洲国产精品一区二区三区在线观看 |