標籤:str servlet sdn 去掉 type exception 記錄檔 java position
原文:http://blog.csdn.net/liutt55/article/details/78126614
public void downProcessFile(HttpServletRequest request,HttpServletResponse response,String path){ try { File file = new File(path); String filename = file.getName();// 擷取記錄檔名稱 InputStream fis = new BufferedInputStream(new FileInputStream(path)); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); response.reset(); // 先去掉檔案名稱中的空格,然後轉換編碼格式為utf-8,保證不出現亂碼,這個檔案名稱用於瀏覽器的下載框中自動顯示的檔案名稱 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) { } }
java以流的形式輸出檔案