一:文件打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)) != -<