一:文件打zip包并下载;
准备两个项目8800和8899;
8899:负责提供文件的下载服务;
8800:负责去8899资源管理工程下载文件,并打zip包下载;
1):文件资源提供方8899代码
@RestController
public class DownloadFileController {
private static Logger logger = Logger.getLogger(DownloadFileController.class);
@PostMapping(value = "/download/file")
public void downloadFile(HttpServletResponse response,@RequestParam String filePath){
logger.info("文件路径:"+filePath);
//这里模拟去资源服务器下载的过程
File file = new File(filePath);
//file对象转byte[],实际可能是将流对象转byte[],所以这个方法的代码根据实际情况而定;
byte[] filebyte = this.Filebyte(file);
try {
ServletOutputStream outputStream = response.getOutputStream();
//这里需要的就是个byte数字,实际去资源服务器下载可能会得到流对象如:inputStream,实际只需将inputSteam转为byte[]即可;
outputStream.write(filebyte);
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* File 转 byte数组
* @param tradeFile
* @return
*/
public byte[] Filebyte(File tradeFile){
byte[] buffer = null;
try
{
FileInputStream fis = new FileInputStream(tradeFile);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != -<

本文介绍如何在SpringCloud项目中利用Feign从8899服务获取文件资源,然后在8800服务端进行zip打包并提供下载。详细步骤包括:1) 8899服务提供文件下载接口;2) 8800服务调用接口,处理文件并打包成zip;3) 提供下载接口http://localhost:8800/download/zip,测试下载的zip文件名按代码定义。
最低0.47元/天 解锁文章
9269

被折叠的 条评论
为什么被折叠?



