Java实现点击下载文件的时候,弹出“另存为”对话框,选择保存位置,然后下载,代码如下:
public void downLoad(String filePath, HttpServletResponse response) throws Exception { System.out.println("filePath"+filePath); File f = new File(filePath); if (!f.exists()) { response.sendError(404, "File not found!"); return; } BufferedInputStream br = new BufferedInputStream(new FileInputStream(f)); byte[] buf = new byte[1024]; int len = 0; response.reset(); response.setContentType("application/x-msdownload"); response.setHeader("Content-Disposition", "attachment; filename=" + f.getName()); OutputStream out = response.getOutputStream(); while ((len = br.read(buf)) > 0) out.write(buf, 0, len); br.close(); out.close(); }
取消
评论