springboot+vue实现页⾯下载⽂件
本⽂实例为⼤家分享了springboot+vue页⾯下载⽂件的具体代码,供⼤家参考,具体内容如下
1.前端代码:
<template v-slot:operate="{ row }">
<vxe-button class="el-icon-download" title="成果下载" circle @click="downloadFile(row)"></vxe-button> </template>
downloadFile(row) {
window.location = "localhost:8001/file/downloadFile?taskId=" + row.id;
}
2.后端代码:
ller;
import com.alibaba.fastjson.JSON;
ity.DataInfo;
idknow.analyse.service.FileService;
idknow.analyse.utils.Download;
idknow.analyse.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
springboot结构import 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 = Files("file");
Map<String, Object> map = (Map<String, Object>) JSON.Parameter("body"));
String companyId = Parameter("companyId");
String companyName = Parameter("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 ResponseUrl());
// ⽂件下载
if (file.isFile()) {
return downloadFile(taskId);
}
// ⽂件夹压缩成zip下载
if (file.isDirectory()) {
String parent = Parent();
// 创建临时存放⽂件夹
File temDir = new File(parent + "/" + taskId);
if (!ists()) {
temDir.mkdirs();
}
// 将需要下载的⽂件夹和内容拷贝到临时⽂件夹中
try {
} catch (IOException e) {
e.printStackTrace();
}
// 设置头部格式
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename="+taskId+".zip");
// 调⽤⼯具类,下载zip压缩包
try {
} 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 ResponseUrl());
String fileName = Name();
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;
}
}
⼯具类
idknow.analyse.utils;
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, Name(), 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) {
// 注意:Name()前⾯需要带上⽗⽂件夹的名字加⼀斜杠,
/
/ 不然最后压缩包中就不能保留原来的⽂件结构,即:所有⽂件都跑到压缩包根⽬录下了 compress(file, zos, name + "/" + Name(), KeepDirStructure);
} else {
compress(file, zos, Name(), 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("删除" + AbsolutePath());
return;
}
// 删除⼦⽂件夹和⼦⽂件
for (File file : files){
if (file.isDirectory()){
delAllFile(file);
} else {
file.delete();
System.out.println("删除" + AbsolutePath());
}
}
// 删除⽂件夹本⾝
directory.delete();
System.out.println("删除" + AbsolutePath());
}
}
}
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论