网站建议:179001057@qq.com

Action中完成下載文件代碼

技术2022-05-12  3

背景描述:在用Struts實現文件下載 實現:代碼如下,但是在Action中返回的必須是ActionMapping的null對象(即: return null;) private void outputFile(HttpServletResponse response, File file) throws IOException { //回复响应给用户文件下载对话框 response.setHeader("Content-disposition", "attachment;filename=" + FILE_NAME); byte[] b = new byte[100]; OutputStream os = response.getOutputStream(); FileInputStream fileInS = new FileInputStream(file); int n; while((n = fileInS.read(b)) != -1) { os.write(b, 0, n); } fileInS.close(); os.close(); }

最新回复(0)