|
|
|
@ -25,25 +25,29 @@ public class DownLoadFileController { |
|
|
|
@RequestMapping("/download") |
|
|
|
public void download(HttpServletRequest request,HttpServletResponse response) { |
|
|
|
try { |
|
|
|
ClassPathResource classPathResource = new ClassPathResource("static/upload/PrintServer.zip"); |
|
|
|
File file = classPathResource.getFile(); |
|
|
|
InputStream inputStream = classPathResource.getInputStream(); |
|
|
|
//输出文件
|
|
|
|
InputStream fis = new BufferedInputStream(inputStream); |
|
|
|
byte[] buffer = new byte[fis.available()]; |
|
|
|
fis.read(buffer); |
|
|
|
fis.close(); |
|
|
|
response.reset(); |
|
|
|
|
|
|
|
//获取文件的名字再浏览器下载页面
|
|
|
|
String name = file.getName(); |
|
|
|
response.addHeader("Content-Disposition", "attachment;filename=" + new String(name.getBytes(), "iso-8859-1")); |
|
|
|
response.addHeader("Content-Length", "" + file.length()); |
|
|
|
OutputStream out = new BufferedOutputStream(response.getOutputStream()); |
|
|
|
response.setContentType("application/octet-stream"); |
|
|
|
out.write(buffer); |
|
|
|
out.flush(); |
|
|
|
out.close(); |
|
|
|
String path = "static/upload/PrintServer.zip"; |
|
|
|
|
|
|
|
String fileName = "PrintServer.zip"; |
|
|
|
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); |
|
|
|
response.setContentType("content-type:octet-stream"); |
|
|
|
/* .getClassLoader() 方法是在静态文件路径添加一个/ */ |
|
|
|
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(path); |
|
|
|
OutputStream outputStream = response.getOutputStream(); |
|
|
|
try { |
|
|
|
byte[] buffer = new byte[1024]; |
|
|
|
int len; |
|
|
|
assert inputStream != null; |
|
|
|
while ((len = inputStream.read(buffer)) != -1) { /* 将流中内容写出去 .*/ |
|
|
|
outputStream.write(buffer, 0, len); |
|
|
|
} |
|
|
|
}catch (IOException e){ |
|
|
|
e.printStackTrace(); |
|
|
|
}finally { |
|
|
|
assert inputStream != null; |
|
|
|
inputStream.close(); |
|
|
|
outputStream.close(); |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
|