1.调用方代码
@ApiOperation(value = "下载渲染文件zip", httpMethod = "GET")
@GetMapping("/download/files/{taskUid}")
public void downloadFiles(@PathVariable String taskUid,HttpServletResponse response) throws IOException {
OutputStream outputStream = response.getOutputStream();
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition","attachment; filename="+taskUid+".zip");
Response downResponse = renderFeignService.downloadFiles(taskUid);
InputStream inputStream =null;
try {
Response.Body body = downResponse.body();
inputStream = body.asInputStream();
byte[] bytes = new byte[1024];
int len = 0;
while ((len = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, len);
}
}catch (Throwable e){
e.printStackTrace();
}finally {
inputStream.close();
outputStream.flush();
outputStream.close();
}
}
import com.miju.common.data.Page;
import com.miju.common.data.render.*;
import com.miju.common.utils.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import feign.Response;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@FeignClient(name = "xpxDesign-render")
public interface RenderFeignService {
@ApiOperation(value = "下载渲染文件zip", httpMethod = "GET")
@GetMapping(value = "/render/render/download/files/{taskUid}")
Response downloadFiles(@PathVariable String taskUid) throws IOException;
}
2.被调用方代码
@ApiOperation(value = "下载渲染文件zip", httpMethod = "GET")
@GetMapping("/download/files/{taskUid}")
public Result<String> downloadFiles(@PathVariable String taskUid, HttpServletResponse response) throws IOException {
renderTaskService.dowFiles(taskUid,response);
return null;
}
public Result<String> dowFiles(String taskUid, HttpServletResponse response)throws IOException{
//服务器路径
String filePath = pathProperties.getShareTaskPath()+"\\"+taskUid;
//批量下载文件 zip压缩
String zipName = taskUid+".zip";
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition","attachment; filename="+zipName);
ZipOutputStream out = new ZipOutputStream(response.getOutputStream());
try {
//压缩文件下载工具类
FileUtil.doCompress(filePath, out);
response.flushBuffer();
} catch (Exception e) {
log.info("下载渲染文件异常[{}],[{}]",e.toString(),e.getMessage());
return resultUtil.error(ResponseCodeMessage.HTTP_CLIENT_ERROR);
}finally{
out.close();
return null;
}
}
下载文件工具类
package com.miju.common.utils;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* 文件处理
*/
public class FileUtil {
public static void doCompress(String srcFile, String zipFile) throws IOException {
doCompress(new File(srcFile), new File(zipFile));
}
/**
* 文件压缩
* @param srcFile 目录或者单个文件
* @param zipFile 压缩后的ZIP文件
*/
public static void doCompress(File srcFile, File zipFile) throws IOException {
ZipOutputStream out = null;
try {
out = new ZipOutputStream(new FileOutputStream(zipFile));
doCompress(srcFile, out);
} catch (Exception e) {
throw e;
} finally {
out.close();//记得关闭资源
}
}
public static void doCompress(String filelName, ZipOutputStream out) throws IOException{
doCompress(new File(filelName), out);
}
public static void doCompress(File file, ZipOutputStream out) throws IOException{
doCompress(file, out, "");
}
public static void doCompress(File inFile, ZipOutputStream out, String dir) throws IOException {
if ( inFile.isDirectory() ) {
File[] files = inFile.listFiles();
if (files!=null && files.length>0) {
for (File file : files) {
String name = inFile.getName();
if (!"".equals(dir)) {
name = dir + "/" + name;
}
FileUtil.doCompress(file, out, name);
}
}
} else {
FileUtil.doZip(inFile, out, dir);
}
}
public static void doZip(File inFile, ZipOutputStream out, String dir) throws IOException {
String entryName = null;
if (!"".equals(dir)) {
entryName = dir + "/" + inFile.getName();
} else {
entryName = inFile.getName();
}
ZipEntry entry = new ZipEntry(entryName);
out.putNextEntry(entry);
int len = 0 ;
byte[] buffer = new byte[1024];
FileInputStream fis = new FileInputStream(inFile);
while ((len = fis.read(buffer)) > 0) {
out.write(buffer, 0, len);
out.flush();
}
out.closeEntry();
fis.close();
}
/**
* 下载单个文件
* @param path
* @param response
* @return
*/
public HttpServletResponse download(String path, HttpServletResponse response) {
try {
// path是指欲下载的文件的路径。
File file = new File(path);
// 取得文件名。
String filename = file.getName();
// 取得文件的后缀名。
String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return response;
}
}