springboot+vue页面下载文件

springboot+vue页面下载文件

1.前端代码:

<template v-slot:operate="{ row }">
            <vxe-button style="color: #409eff; font-weight: bolder" class="el-icon-download" title="成果下载" circle @click="downloadFile(row)"></vxe-button>
</template>
downloadFile(row) {
	window.location = "http://localhost:8001/file/downloadFile?taskId=" + row.id;
}
  1. 后端代码:
package com.gridknow.analyse.controller;


import com.alibaba.fastjson.JSON;
import com.gridknow.analyse.entity.DataInfo;
import com.gridknow.analyse.service.FileService;
import com.gridknow.analyse.utils.Download;
import com.gridknow.analyse.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.Map;


/**
 * @ClassName FileController
 * @Description: TODO
 * @Author Administrator
 * @Date 2020/8/20 14:02
 * @Version TODO
 **/

@Controller
@RequestMapping("/file")
public class FileController {

    @Value("${gridknow.mltc.imgurl}")
    private String imgUrl;

    @Autowired
    private FileService fileService;


    @CrossOrigin
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public Result upload(MultipartHttpServletRequest request) {
        List<MultipartFile> multipartFiles = request.getFiles("file");
        Map<String, Object> map = (Map<String, Object>) JSON.parse(request.getParameter("body"));
        String companyId = request.getParameter("companyId");
        String companyName = request.getParameter("companyName");
        boolean bool = fileService.uploadAndInsert(multipartFiles, map, companyId, companyName);
        if (bool) {
            return new Result(200);
        } else {
            return new Result(500);
        }
    }

    @GetMapping("/downloadFile")
    public ResponseEntity<Object> downloadFile(@RequestParam("taskId") String taskId, HttpServletResponse response) {
        DataInfo dataInfo = fileService.queryTaskById(taskId);
        if (dataInfo == null) {
            return null;
        }
        File file = new File(dataInfo.getResponseUrl());
        // 文件下载
        if (file.isFile()) {
            return downloadFile(taskId);
        }
        // 文件夹压缩成zip下载
        if (file.isDirectory()) {
            String parent = file.getParent();
            // 创建临时存放文件夹
            File temDir = new File(parent + "/" + taskId);
            if (!temDir.exists()) {
                temDir.mkdirs();
            }
            // 将需要下载的文件夹和内容拷贝到临时文件夹中
            try {
                Download.copyDir(dataInfo.getResponseUrl(), parent + "/" + taskId);
            } catch (IOException e) {
                e.printStackTrace();
            }
            // 设置头部格式
            response.setContentType("application/zip");
            response.setHeader("Content-Disposition", "attachment; filename="+taskId+".zip");
            // 调用工具类,下载zip压缩包
            try {
                Download.toZip(temDir.getPath(), response.getOutputStream(), true);
            } catch (IOException e) {
                e.printStackTrace();
            }
            // 删除临时文件夹和内容
            Download.delAllFile(new File(parent + "/" + taskId));
        }
        return null;

    }
    
    public ResponseEntity<Object> downloadFile(String taskId) {
        DataInfo dataInfo = fileService.queryTaskById(taskId);
        if (dataInfo == null) {
            return null;
        }
        File file = new File(dataInfo.getResponseUrl());
        String fileName = file.getName();
        InputStreamResource resource = null;
        try {
            resource = new InputStreamResource(new FileInputStream(file));
        } catch (Exception e) {
            e.printStackTrace();
        }
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", String.format("attachment;filename=\"%s", fileName));
        headers.add("Cache-Control", "no-cache,no-store,must-revalidate");
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");
        ResponseEntity<Object> responseEntity = ResponseEntity.ok()
                .headers(headers)
                .contentLength(file.length())
                .contentType(MediaType.parseMediaType("application/octet-stream"))
                .body(resource);
        return responseEntity;
    }
}

工具类

package com.gridknow.analyse.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @ClassName Download
 * @Description: TODO
 * @Author Administrator
 * @Date 2020/9/2 9:54
 * @Version TODO
 **/
@Slf4j
public class Download {

    private static final int BUFFER_SIZE = 2 * 1024;

    public static void toZip(String srcDir, OutputStream out, boolean KeepDirStructure) throws RuntimeException {

        long start = System.currentTimeMillis();
        ZipOutputStream zos = null;
        try {
            zos = new ZipOutputStream(out);
            File sourceFile = new File(srcDir);
            compress(sourceFile, zos, sourceFile.getName(), KeepDirStructure);
            long end = System.currentTimeMillis();
            log.info("压缩完成,耗时:" + (end - start) + " ms");
        } catch (Exception e) {
            throw new RuntimeException("zip error from ZipUtils", e);
        } finally {
            if (zos != null) {
                try {
                    zos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    /**
     * 递归压缩方法
     *
     * @param sourceFile       源文件
     * @param zos              zip输出流
     * @param name             压缩后的名称
     * @param KeepDirStructure 是否保留原来的目录结构, true:保留目录结构;
     *                         false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
     * @throws Exception
     *
     */

    private static void compress(File sourceFile, ZipOutputStream zos, String name, boolean KeepDirStructure)
            throws Exception {

        byte[] buf = new byte[BUFFER_SIZE];
        if (sourceFile.isFile()) {
            // 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
            zos.putNextEntry(new ZipEntry(name));
            // copy文件到zip输出流中
            int len;
            FileInputStream in = new FileInputStream(sourceFile);
            while ((len = in.read(buf)) != -1) {
                zos.write(buf, 0, len);
            }
            // Complete the entry
            zos.closeEntry();
            in.close();
        } else {
            File[] listFiles = sourceFile.listFiles();
            if (listFiles == null || listFiles.length == 0) {
                // 需要保留原来的文件结构时,需要对空文件夹进行处理
                if (KeepDirStructure) {
                    // 空文件夹的处理
                    zos.putNextEntry(new ZipEntry(name + "/"));
                    // 没有文件,不需要文件的copy
                    zos.closeEntry();
                }
            } else {
                for (File file : listFiles) {
                    // 判断是否需要保留原来的文件结构
                    if (KeepDirStructure) {
                        // 注意:file.getName()前面需要带上父文件夹的名字加一斜杠,
                        // 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了
                        compress(file, zos, name + "/" + file.getName(), KeepDirStructure);
                    } else {
                        compress(file, zos, file.getName(), KeepDirStructure);
                    }
                }
            }
        }
    }

    /**
     * 拷贝文件夹
     *
     * @param oldPath 原文件夹
     * @param newPath 指定文件夹
     */
    public static void copyDir(String oldPath, String newPath) throws IOException {
        File file = new File(oldPath);
        //文件名称列表
        String[] filePath = file.list();

        if (!(new File(newPath)).exists()) {
            (new File(newPath)).mkdir();
        }

        for (int i = 0; i < filePath.length; i++) {
            if ((new File(oldPath + File.separator + filePath[i])).isDirectory()) {
                copyDir(oldPath  + File.separator  + filePath[i], newPath  + File.separator + filePath[i]);
            }

            if (new File(oldPath  + File.separator + filePath[i]).isFile()) {
                copyFile(oldPath + File.separator + filePath[i], newPath + File.separator + filePath[i]);
            }

        }
    }

    /**
     * 拷贝文件
     *
     * @param oldPath 资源文件
     * @param newPath 指定文件
     */
    public static void copyFile(String oldPath, String newPath) throws IOException {
        File oldFile = new File(oldPath);
        File file = new File(newPath);
        FileInputStream in = new FileInputStream(oldFile);
        FileOutputStream out = new FileOutputStream(file);;

        byte[] buffer=new byte[2097152];

        while((in.read(buffer)) != -1){
            out.write(buffer);
        }
        in.close();
        out.close();
    }

    /**
     * 删除文件或文件夹
     * @param directory
     */
    public static void delAllFile(File directory){
        if (!directory.isDirectory()){
            directory.delete();
        } else{
            File [] files = directory.listFiles();

            // 空文件夹
            if (files.length == 0){
                directory.delete();
                System.out.println("删除" + directory.getAbsolutePath());
                return;
            }

            // 删除子文件夹和子文件
            for (File file : files){
                if (file.isDirectory()){
                    delAllFile(file);
                } else {
                    file.delete();
                    System.out.println("删除" + file.getAbsolutePath());
                }
            }

            // 删除文件夹本身
            directory.delete();
            System.out.println("删除" + directory.getAbsolutePath());
        }
    }

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 后端实现文件下载 在Spring Boot中,我们可以使用以下代码实现文件下载: ```java @GetMapping("/download") public ResponseEntity<Resource> downloadFile() throws IOException { Resource resource = new UrlResource("file:/path/to/file"); HttpHeaders headers = new HttpHeaders(); headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\""); return ResponseEntity.ok().headers(headers).contentLength(resource.contentLength()).contentType(MediaType.APPLICATION_OCTET_STREAM).body(resource); } ``` 其中,`UrlResource`是Spring提供的获取文件资源的类,`HttpHeaders.CONTENT_DISPOSITION`设置文件下载方式为附件,`MediaType.APPLICATION_OCTET_STREAM`指定文件类型为二进制流。 2. 前端实现文件下载Vue中,我们可以使用Element UI中的`el-button`组件来实现文件下载: ```html <el-button type="primary" icon="el-icon-download" @click="downloadFile">下载文件</el-button> ``` 在Vue组件中,我们需要定义`downloadFile`方法来实现文件下载: ```javascript downloadFile() { window.location.href = '/download'; } ``` 其中,`window.location.href`将页面重定向到下载链接,即后端实现的文件下载接口。 需要注意的是,如果需要传递参数给后端,可以使用axios或者fetch来发送GET或POST请求,将参数传递给后端,再在后端实现文件下载。具体实现方法可以参考以下代码: ```javascript downloadFile() { axios.get('/download', { params: { filename: 'example.txt' }, responseType: 'blob' }).then(response => { const blob = new Blob([response.data], {type: 'application/octet-stream'}) const url = window.URL.createObjectURL(blob) const link = document.createElement('a') link.href = url link.download = 'example.txt' link.click() window.URL.revokeObjectURL(url) }) } ``` 在这个例子中,我们使用了axios来发送GET请求,`params`中传递了参数filename,`responseType`设置返回类型为二进制流。在请求成功后,我们将返回的数据转换为Blob对象,通过URL.createObjectURL方法生成下载链接,然后创建一个a标签并设置download属性,模拟点击a标签来下载文件。最后使用URL.revokeObjectURL方法释放资源。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值