首页 > 代码库 > java 文件下载
java 文件下载
@RequestMapping(value = "http://www.mamicode.com/downLoad")
public void downLoad(HttpServletResponse response, String value,String oldName ,String dateFileName) throws ParseException{
String path =uploadsDir+FOLDER+"/attach/"+dateFileName+"/"+value;
File file = new File(path);
InputStream fis;
try {
String filename = new String(oldName.getBytes("ISO-8859-1"),"utf-8");
fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.replaceAll(" ", "").getBytes("utf-8"),"iso8859-1"));
response.addHeader("Content-Length", "" + file.length());
OutputStream os = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
os.write(buffer);
os.flush();
os.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
java 文件下载