<p>public ResponseEntity<Object> download1(String filename, HttpServletRequest req) throws Exception
{
String path = getfilePath(req, &quot;upload&quot;);
System.out.println(path);
File file = new File(path+File.separator+filename+&quot;.xlsx&quot;);
File zipFile = new File(path+File.separator+filename+&quot;.zip&quot;);</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)) &gt;= 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(&quot;Content-Disposition&quot;, &quot;attachment; filename=&quot;+zipFile.getName());
    headers.add(&quot;Cache-Control&quot;, &quot;no-cache, no-store, must-revalidate&quot;);
    headers.add(&quot;Pragma&quot;, &quot;no-cache&quot;);
    headers.add(&quot;Expires&quot;, &quot;0&quot;);
    
    // Create response entity
    ResponseEntity&lt;Object&gt; resp = 
    		ResponseEntity.ok()
    					  .headers(headers).contentLength(zipFile.length())
    					  .contentType(MediaType.parseMediaType(&quot;application/zip&quot;))
    					  .body(resource);
    
    // Delete temporary files
    file.delete();
    zipFile.delete();
    
    return resp;
</code></pre>
public ResponseEntityObject download1String filename HttpServletRequest req throws Exception			String path = getfilePathreq upload;		Systemoutprintlnpath;		File file = new Filepath+Fileseparator+filen

原文地址: http://www.cveoy.top/t/topic/iZJB 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录