本文实例为大家分享了springboot+vue页面下载文件的具体代码,供大家参考,具体内容如下
1.前端代码:
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;
@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 multipartFiles = request.getFiles("file");
Map map = (Map) 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
工具类
package com.gridknow.analyse.utils;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@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();
}
}
}
}
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);
}
}
}
}
}
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]);
}
}
}
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();
}
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());
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



