@RequestMapping(value = "download/{id}")
private void downloadPicture(HttpServletRequest request,HttpServletResponse response,@PathVariable Integer id) {
PzFile pzFile = pzFileService.getById(id);
if(!Util.checkNull(pzFile.getPath())){
pzFile.setDownloadCount(pzFile.getDownloadCount()+1);
pzFileService.update(pzFile);
}
//取得服务器IP 取得服务器端口
/* String docUrl = "http://"+request.getLocalAddr()+":"+request.getLocalPort()+"/"+lzDoc.getDocUrl();
URL url = null;*/
String path = pzFile.getPath();
path = path.replace("image-server/", "D:/pinzhi/");
String fileName = pzFile.getFileRealName();
try {
OutputStream out = response.getOutputStream();
this.setResponseHeader(response, fileName);
/* url = new URL(docUrl);*/
File file = new File(path);
InputStream input = new FileInputStream(file);
//DataInputStream dataInputStream = new DataInputStream(url.openStream());
DataInputStream dataInputStream = new DataInputStream(input);
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = dataInputStream.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
out.write(output.toByteArray());
dataInputStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//发送响应流方法
public void setResponseHeader(HttpServletResponse response, String fileName) {
try {
try {
fileName = new String(fileName.getBytes(),"ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setContentType("application/octet-stream;charset=ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
} catch (Exception ex) {
ex.printStackTrace();
}
}