public ResponseEntityObject download1String filename HttpServletRequest req throws Exception String path = getfilePathreq upload; Systemoutprintlnpath; File file = new Filepath+Fileseparator+filen
<p>public ResponseEntity<Object> download1(String filename, HttpServletRequest req) throws Exception
{
String path = getfilePath(req, "upload");
System.out.println(path);
File file = new File(path+File.separator+filename+".xlsx");
File zipFile = new File(path+File.separator+filename+".zip");</p>
<pre><code> // Create zip file
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
// Add file to zip
FileInputStream fileIn = new FileInputStream(file);
zipOut.putNextEntry(new ZipEntry(file.getName()));
byte[] bytes = new byte[1024];
int length;
while ((length = fileIn.read(bytes)) >= 0) {
zipOut.write(bytes, 0, length);
}
zipOut.closeEntry();
fileIn.close();
// Close zip stream
zipOut.close();
// Create input stream resource for zip file
InputStreamResource resource = new InputStreamResource(new FileInputStream(zipFile));
// Set headers for response
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attachment; filename="+zipFile.getName());
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
// Create response entity
ResponseEntity<Object> resp =
ResponseEntity.ok()
.headers(headers).contentLength(zipFile.length())
.contentType(MediaType.parseMediaType("application/zip"))
.body(resource);
// Delete temporary files
file.delete();
zipFile.delete();
return resp;
</code></pre>
原文地址: http://www.cveoy.top/t/topic/iZJB 著作权归作者所有。请勿转载和采集!