@RequestMapping(value = "downFile")
public void downFile(HttpServletResponse response, String name, HttpServletRequest request) { ServletContext sc = request.getSession().getServletContext(); String url = sc.getRealPath("/upload/" + name); File file = new File(url);//以下两种文件的下载流的处理方式,第二个方法感觉比较好
downFileWidthData(response, name, url, file);// 用// downFileWidthBuffer(response, name, file);//运用buffer
}
/**
* @param response * @param name * @param file */ private void downFileWidthBuffer(HttpServletResponse response, String name, File file) { Date date = new Date(); long start = System.currentTimeMillis(); System.out.println(start); BufferedOutputStream bos = null; FileInputStream fis = null; try {response.addHeader("Content-Length", "" + file.length());
response.addHeader("Content-Disposition", "attachment;filename=" + new String(name.getBytes("gbk"), "iso-8859-1"));response.setContentType("application/octet-stream;charset=UTF-8");
response.setContentType("application/octet-stream;charset=UTF-8");} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block e1.printStackTrace(); } try { fis = new FileInputStream(file); byte[] buffer = new byte[fis.available()]; fis.read(buffer);bos = new BufferedOutputStream(response.getOutputStream());
bos.write(buffer);
bos.flush();} catch (Exception e) {
// TODO Auto-generated catch block e.printStackTrace(); } finally {try {
if (fis != null) { fis.close(); } if (bos != null) { bos.close();}
} catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } long end = System.currentTimeMillis();System.out.println(end - start);
}/**
* @param response * @param name * @param url * @param file */ private void downFileWidthData(HttpServletResponse response, String name, String url, File file) { long start = System.currentTimeMillis(); System.out.println(start); FileInputStream fis = null; try { fis = new FileInputStream(file); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } DataOutputStream dos = null; OutputStream os = null; try { url = URLEncoder.encode(url, "utf-8"); // response.addHeader("Context-Disposion", // "Attachment:filename="+URLEncoder.encode(name, "utf-8")); response.addHeader("Content-Length", "" + file.length()); response.addHeader("Content-Disposition", "attachment;filename=" + new String(name.getBytes("gbk"), "iso-8859-1")); response.setContentType("application/octet-stream;charset=UTF-8"); os = response.getOutputStream(); dos = new DataOutputStream(os); byte[] b = new byte[1024]; int len; while ((len = fis.read(b)) != -1) { dos.write(b, 0, len); } dos.flush(); os.flush(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally {try {
if (os != null) { os.close(); } if (dos != null) { dos.close(); } if (fis != null) {fis.close();
} } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } long end = System.currentTimeMillis(); System.out.println(end - start);}