轻量级前端框架助力开发者提升项目效率与性能
864
2023-02-27
springboot+vue实现页面-文件
本文实例为大家分享了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;
/**
* @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
Map
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
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
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
.headers(headers)
.contentLength(file.length())
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(resource);
return responseEntity;
}
}
工具类
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~