时间:2022-01-14 11:11:12 | 栏目:.NET代码 | 点击:次
实例如下所示:
/** * 文件下载 * @param filePath 文件路径 * @param fileName 文件名称 */ public void download(String filePath,String fileName){ try { //支持中文 fileName = URLEncoder.encode(fileName,"UTF-8"); HttpServletResponse response = ServletActionContext.getResponse(); HttpServletRequest request = ServletActionContext.getRequest(); response.reset(); response.setContentType(request.getServletContext().getMimeType(fileName)); response.setHeader("Content-Disposition", "attachment;filename="+fileName); InputStream in = new FileInputStream(filePath); OutputStream out = response.getOutputStream(); byte[] b = new byte[1024]; int length = 0; while((length = in.read(b)) != -1) { out.write(b,0,length); } in.close(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }