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.上传大文件配置

    1.  
  • /**
    * 设置上传大文件大小,配置文件属性设置无效
    */
    @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. MySQL Index--关联条件列索引缺失导致执行计划性能不佳

    某系统反馈慢SQL影响生产,查看SLOW LOG发现下面慢SQL: SELECT COUNT(DISTINCT m.batch_no) FROM ob_relation r INNER JOIN ob ...

  2. SQL SERVER-记录对表操作的触发器

    CREATE TRIGGER [dbo].[KNMT_LOG] ON [dbo].[KNMT] FOR UPDATE, DELETE AS ) ) ) ) DECLARE @STATMT AS VAR ...

  3. c# 写入文本文件

  4. AWVS (Acunetix Web Vulnerability Scanner )

    1.AWVS 是国外一款不错的安全测试工具 ,界面清楚,使用方便.可以爬取网页加载payload测试存在的安全问题            破解版的下载地址: https://www.arvinhk.c ...

  5. Linux命令——lspci

    参考:7 Linux lspci Command Examples to Get PCI Bus Hardware Device Info 简介 lspci可以看成“ls” + “pci”.lspci ...

  6. Apache 安装后Error 403的故障排错方法(linux)

    Apache 安装后Error 403的故障排错方法 2018年01月07日 14:25:41 个人分类: Linux 一.问题描述 在apache2的httpd配置中,很多情况都会出现403. 刚安 ...

  7. 剑指Offer(三十六):两个链表的第一个公共结点

    剑指Offer(三十六):两个链表的第一个公共结点 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.ne ...

  8. VMware Linux系统克隆

    系统克隆 网卡设备无法识别 解决克隆虚拟机后网卡设备无法识别启动问题的方法 一.故障问题 从vmware workstation中克隆(clone)了一个CentOS 6的虚拟机,启动之后发现网卡没有 ...

  9. linux下Boost序列化问题解决

    由于项目需要,要使用boost,所以在网上找了一些例子帮助理解,其中遇到很多问题,再次总结记录一下.#include <boost/archive/text_oarchive.hpp> # ...

  10. fread/IO 模板

    namespace Fread { char cb[1<<15],*cs,*ct; #define getc (cs==ct&&(ct=(cs=cb)+fread(cb,1 ...