欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot+vue實(shí)現(xiàn)頁(yè)面下載文件

 更新時(shí)間:2020年12月22日 13:01:24   作者:Wenchao Zhang  
這篇文章主要為大家詳細(xì)介紹了springboot+vue實(shí)現(xiàn)頁(yè)面下載文件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了springboot+vue頁(yè)面下載文件的具體代碼,供大家參考,具體內(nèi)容如下

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;
}

2.后端代碼:

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();
   // 創(chuàng)建臨時(shí)存放文件夾
   File temDir = new File(parent + "/" + taskId);
   if (!temDir.exists()) {
    temDir.mkdirs();
   }
   // 將需要下載的文件夾和內(nèi)容拷貝到臨時(shí)文件夾中
   try {
    Download.copyDir(dataInfo.getResponseUrl(), parent + "/" + taskId);
   } catch (IOException e) {
    e.printStackTrace();
   }
   // 設(shè)置頭部格式
   response.setContentType("application/zip");
   response.setHeader("Content-Disposition", "attachment; filename="+taskId+".zip");
   // 調(diào)用工具類,下載zip壓縮包
   try {
    Download.toZip(temDir.getPath(), response.getOutputStream(), true);
   } catch (IOException e) {
    e.printStackTrace();
   }
   // 刪除臨時(shí)文件夾和內(nèi)容
   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("壓縮完成,耗時(shí):" + (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 是否保留原來(lái)的目錄結(jié)構(gòu), true:保留目錄結(jié)構(gòu);
  *       false:所有文件跑到壓縮包根目錄下(注意:不保留目錄結(jié)構(gòu)可能會(huì)出現(xiàn)同名文件,會(huì)壓縮失敗)
  * @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輸出流中添加一個(gè)zip實(shí)體,構(gòu)造器中name為zip實(shí)體的文件的名字
   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) {
    // 需要保留原來(lái)的文件結(jié)構(gòu)時(shí),需要對(duì)空文件夾進(jìn)行處理
    if (KeepDirStructure) {
     // 空文件夾的處理
     zos.putNextEntry(new ZipEntry(name + "/"));
     // 沒(méi)有文件,不需要文件的copy
     zos.closeEntry();
    }
   } else {
    for (File file : listFiles) {
     // 判斷是否需要保留原來(lái)的文件結(jié)構(gòu)
     if (KeepDirStructure) {
      // 注意:file.getName()前面需要帶上父文件夾的名字加一斜杠,
      // 不然最后壓縮包中就不能保留原來(lái)的文件結(jié)構(gòu),即:所有文件都跑到壓縮包根目錄下了
      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());
  }
 }

}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring @Async無(wú)法實(shí)現(xiàn)異步的解決方案

    Spring @Async無(wú)法實(shí)現(xiàn)異步的解決方案

    這篇文章主要介紹了Spring @Async無(wú)法實(shí)現(xiàn)異步的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java中資源加載的方法及Spring的ResourceLoader應(yīng)用小結(jié)

    Java中資源加載的方法及Spring的ResourceLoader應(yīng)用小結(jié)

    在Java開(kāi)發(fā)中,資源加載是一個(gè)基礎(chǔ)而重要的操作,這篇文章主要介紹了深入理解Java中資源加載的方法及Spring的ResourceLoader應(yīng)用,本文通過(guò)實(shí)例代碼演示了通過(guò)ClassLoader和Class獲取資源的內(nèi)容,以及使用Spring的ResourceLoader加載多個(gè)資源的過(guò)程,需要的朋友可以參考下
    2024-01-01
  • Mybatis使用foreach批量更新數(shù)據(jù)報(bào)無(wú)效字符錯(cuò)誤問(wèn)題

    Mybatis使用foreach批量更新數(shù)據(jù)報(bào)無(wú)效字符錯(cuò)誤問(wèn)題

    這篇文章主要介紹了Mybatis使用foreach批量更新數(shù)據(jù)報(bào)無(wú)效字符錯(cuò)誤問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Java并發(fā)編程(CyclicBarrier)實(shí)例詳解

    Java并發(fā)編程(CyclicBarrier)實(shí)例詳解

    這篇文章主要介紹了Java并發(fā)編程(CyclicBarrier)實(shí)例詳解的相關(guān)資料,JAVA編寫并發(fā)程序的時(shí)候,我們需要仔細(xì)去思考一下并發(fā)流程的控制,如何讓各個(gè)線程之間協(xié)作完成某項(xiàng)工作。
    2017-07-07
  • Java線程變量ThreadLocal源碼分析

    Java線程變量ThreadLocal源碼分析

    ThreadLocal用來(lái)提供線程內(nèi)部的局部變量,不同的線程之間不會(huì)相互干擾,這種變量在多線程環(huán)境下訪問(wèn)時(shí)能保證各個(gè)線程的變量相對(duì)獨(dú)立于其他線程內(nèi)的變量,在線程的生命周期內(nèi)起作用,可以減少同一個(gè)線程內(nèi)多個(gè)函數(shù)或組件之間一些公共變量傳遞的復(fù)雜度
    2022-08-08
  • Springboot微服務(wù)分布式框架Rouyi Cloud權(quán)限認(rèn)證(登錄流程之token解析)

    Springboot微服務(wù)分布式框架Rouyi Cloud權(quán)限認(rèn)證(登錄流程之token解析)

    這篇文章主要介紹了Springboot微服務(wù)分布式框架Rouyi Cloud權(quán)限認(rèn)證的相關(guān)知識(shí),重點(diǎn)講解下整個(gè)框架的入口,登錄流程之token解析,感興趣的朋友跟隨小編一起看看吧
    2024-04-04
  • mybatis?實(shí)現(xiàn)多層級(jí)collection嵌套

    mybatis?實(shí)現(xiàn)多層級(jí)collection嵌套

    這篇文章主要介紹了mybatis?實(shí)現(xiàn)多層級(jí)collection嵌套,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java中的LinkedHashSet源碼解讀

    Java中的LinkedHashSet源碼解讀

    這篇文章主要介紹了Java中的LinkedHashSet源碼解讀,LinkedHashSet?是?Java?中的一個(gè)集合類,它是?HashSet?的子類,并實(shí)現(xiàn)了?Set?接口,與?HashSet?不同的是,LinkedHashSet?保留了元素插入的順序,并且具有?HashSet?的快速查找特性,需要的朋友可以參考下
    2023-09-09
  • Java中的final關(guān)鍵字深入理解

    Java中的final關(guān)鍵字深入理解

    這篇文章主要介紹了Java中的final關(guān)鍵字深入理解的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • MybatisPlus的LambdaQueryWrapper用法詳解

    MybatisPlus的LambdaQueryWrapper用法詳解

    LambdaQueryWrapper<Tag>?是 MyBatis-Plus 框架中的一個(gè)功能強(qiáng)大的查詢構(gòu)造器,它用于構(gòu)建 SQL 查詢條件,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-10-10

最新評(píng)論