public void downloadD(HttpServletRequest request, HttpServletResponse res, String contractName, String contractPath) throws IOException { // FileAttach fileAttach = fileAttachService.getFileAttachById(fileId); OutputStream os = res.getOutputStream(); try { res.reset(); String fileName = URLEncoder.encode(contractName, "UTF-8"); /* * see http://support.microsoft.com/default.aspx?kbid=816868 */ if (fileName.length() > 150) { String guessCharset = "gb2312"; /*根據request的locale 得出可能的編碼,中文操做系統一般是gb2312*/ fileName = new String(contractName.getBytes(guessCharset), "ISO8859-1"); } res.setHeader("Content-Disposition", "attachment; filename=" + fileName); res.setContentType("application/octet-stream; charset=utf-8"); String path = request.getSession().getServletContext() .getRealPath("/"); File dirPath = new File(path +"/"+ contractPath); os.write(FileUtils.readFileToByteArray(dirPath)); os.flush(); } finally { if (os != null) { os.close(); } } }