springboot+vue实现文件上传下载


Posted in Vue.js onNovember 17, 2020

本文实例为大家分享了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
  class="file"
  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");
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Vue.js 相关文章推荐
浅谈Vue使用Elementui修改默认的最快方法
Dec 05 Vue.js
Vue过滤器,生命周期函数和vue-resource简单介绍
Jan 12 Vue.js
vue 实现click同时传入事件对象和自定义参数
Jan 29 Vue.js
vue3.0 自适应不同分辨率电脑的操作
Feb 06 Vue.js
vue-router懒加载的3种方式汇总
Feb 28 Vue.js
vue3中的组件间通信
Mar 31 Vue.js
浅谈vue2的$refs在vue3组合式API中的替代方法
Apr 18 Vue.js
原生JS封装vue Tab切换效果
Apr 28 Vue.js
vue-cli4.5.x快速搭建项目
May 30 Vue.js
Vue实现tab导航栏并支持左右滑动功能
Jun 28 Vue.js
vue中 this.$set的使用详解
Nov 17 Vue.js
vue整合百度地图显示指定地点信息
Apr 06 Vue.js
vue 表单输入框不支持focus及blur事件的解决方案
Nov 17 #Vue.js
解决vue elementUI 使用el-select 时 change事件的触发问题
Nov 17 #Vue.js
Vue项目利用axios请求接口下载excel
Nov 17 #Vue.js
vue实现下载文件流完整前后端代码
Nov 17 #Vue.js
vue+iview实现文件上传
Nov 17 #Vue.js
vue中echarts的用法及与elementui-select的协同绑定操作
Nov 17 #Vue.js
vue+iview实现分页及查询功能
Nov 17 #Vue.js
You might like
php下关于Cannot use a scalar value as an array的解决办法
2010/08/08 PHP
javascript实现上传图片前的预览(TX的面试题)
2007/08/20 Javascript
javascript 面向对象全新理练之数据的封装
2009/12/03 Javascript
火狐下table中创建form导致两个table之间出现空白
2013/09/02 Javascript
jQuery实现简单的间隔向上滚动效果
2015/03/09 Javascript
JS实现点击网页判断是否安装app并打开否则跳转app store
2016/11/18 Javascript
折叠菜单及选择器的运用
2017/02/03 Javascript
基于vue2.0动态组件及render详解
2018/03/17 Javascript
在Bootstrap开发框架中使用dataTable直接录入表格行数据的方法
2018/10/25 Javascript
详解微信小程序之scroll-view的flex布局问题
2019/01/16 Javascript
JS原生瀑布流效果实现
2019/04/26 Javascript
springboot+vue实现文件上传下载
2020/11/17 Vue.js
python实现监控linux性能及进程消耗性能的方法
2014/07/25 Python
Python文件去除注释的方法
2015/05/25 Python
python中私有函数调用方法解密
2016/04/29 Python
Python基于高斯消元法计算线性方程组示例
2018/01/17 Python
Python rstrip()方法实例详解
2018/11/11 Python
详解分布式任务队列Celery使用说明
2018/11/29 Python
PyQt5实现五子棋游戏(人机对弈)
2020/03/24 Python
python的mysql数据库建立表与插入数据操作示例
2019/09/30 Python
Pandas聚合运算和分组运算的实现示例
2019/10/17 Python
利用python实现.dcm格式图像转为.jpg格式
2020/01/13 Python
Pycharm同步远程服务器调试的方法步骤
2020/11/04 Python
python 实现ping测试延迟的两种方法
2020/12/10 Python
英国天然保健品网站:Simply Supplements
2017/03/22 全球购物
Perfume’s Club澳大利亚官网:西班牙领先的在线美容店
2021/02/01 全球购物
char型变量中能不能存贮一个中文汉字
2015/07/08 面试题
人事助理岗位职责
2013/11/18 职场文书
甜点店创业计划书
2014/01/27 职场文书
大学生毕业鉴定
2014/01/31 职场文书
就业推荐表自我鉴定范文
2014/03/21 职场文书
大四毕业生自荐书
2014/07/05 职场文书
2014年建筑工程工作总结
2014/12/03 职场文书
Golang 入门 之url 包
2022/05/04 Golang
MySQL数据库简介与基本操作
2022/05/30 MySQL
Win11 Dev 预览版25174.1000发布 (附更新修复内容汇总)
2022/08/05 数码科技