https://blog.csdn.net/yhhyhhyhhyhh/article/details/89888953

文章目录

  • 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>

springboot整合vue实现上传下载文件的更多相关文章

  1. rz和sz上传下载文件工具lrzsz

    ######################### rz和sz上传下载文件工具lrzsz ####################################################### ...

  2. linux上很方便的上传下载文件工具rz和sz

    linux上很方便的上传下载文件工具rz和sz(本文适合linux入门的朋友) ##########################################################&l ...

  3. shell通过ftp实现上传/下载文件

    直接代码,shell文件名为testFtptool.sh: #!/bin/bash ########################################################## ...

  4. SFTP远程连接服务器上传下载文件-qt4.8.0-vs2010编译器-项目实例

    本项目仅测试远程连接服务器,支持上传,下载文件,更多功能开发请看API自行开发. 环境:win7系统,Qt4.8.0版本,vs2010编译器 qt4.8.0-vs2010编译器项目实例下载地址:CSD ...

  5. linux下常用FTP命令 上传下载文件【转】

    1. 连接ftp服务器 格式:ftp [hostname| ip-address]a)在linux命令行下输入: ftp 192.168.1.1 b)服务器询问你用户名和密码,分别输入用户名和相应密码 ...

  6. C#实现http协议支持上传下载文件的GET、POST请求

    C#实现http协议支持上传下载文件的GET.POST请求using System; using System.Collections.Generic; using System.Text; usin ...

  7. HttpClient上传下载文件

    HttpClient上传下载文件 java HttpClient Maven依赖 <dependency> <groupId>org.apache.httpcomponents ...

  8. 初级版python登录验证,上传下载文件加MD5文件校验

    服务器端程序 import socket import json import struct import hashlib import os def md5_code(usr, pwd): ret ...

  9. 如何利用京东云的对象存储(OSS)上传下载文件

    作者:刘冀 在公有云厂商里都有对象存储,京东云也不例外,而且也兼容S3的标准因此可以利用相关的工具去上传下载文件,本文主要记录一下利用CloudBerry Explorer for Amazon S3 ...

随机推荐

  1. Kubernetes 1.15部署日记-使用kubeadm--<1-2-3-4>

    2019年9月17日 由于此次日记篇幅较长blog限制直接使用word发布所以分成几篇来发. 1.环境准备 10.110.149.172|192.168.111.51 K8s-1 Centos7.5 ...

  2. Ansible_Day1

    1.传统运维&自动化运维概念 1)传统的运维概念(硬件.软件.系统.网络) 手工安装系统.机房建设: 软件服务配置.部署通过手工的操作: 没有自动化脚本.流程: 依靠大量的运维人员完成任务: ...

  3. mysql_safe和mysql_multi

    1 mysql_safe 原理 mysqld_safe其实为一个shell脚本(封装mysqld),启动时需要调用server和database(即/bin和/data目录),因此需要满足下述条件之一 ...

  4. c++第四次作业

    继承与派生--访问控制 一.知识要点 (一)知识回顾: 基类的成员可以有public.protected.private三种访问属性.基类的自身成员可以对基类中任何一个其他成员进行访问,但是通过基类的 ...

  5. Bias vs. Variance(1)--diagnosing bias vs. variance

    我们的函数是有high bias problem(underfitting problem)还是 high variance problem(overfitting problem),区分它们很得要, ...

  6. “挂起”bug处理执行方案

    目的:避免bug状态改为挂起后,就无人问津,导致一直未得到解决.因而影响用户的使用与产品质量较差.

  7. 使用dos创建一个maven项目

    创建一个maven项目既可以使用eclipse创建,也可以使用dos命令,此文介绍使用dos界面创建一个maven项目 1.win+R输入cmd,打开dos界面 2.cd到你想创建项目的目录(默认都是 ...

  8. module method

    null 参考链接: https://www.cnblogs.com/lvdabao/p/5953884.html

  9. stm32f103c8t6 怎么使用IAP下载程序

    首先下载官方STM32F10X的IAP Bootloader源码,STM32F10x_AN2557_FW_V3.3.0. 用Keil4打开工程代码STM32F10x_AN2557_FW_V3.3.0P ...

  10. 【DP】 路面修整 usaco 2008 feb_gold

    题目描述: ``` FJ打算好好修一下农场中某条凹凸不平的土路.按奶牛们的要求,修好后的路面高度应当单调上升或单调下降,也就是说,高度上升与高度下降的路段不能同时出现在修好的路中. 整条路被分成了N段 ...