只下载不预览
public void downLoad(String url,HttpServletResponse response) throws IOException { int i = url.lastIndexOf("\\"); String name = url.substring(i + 1); FileInputStream in = new FileInputStream(url); response.reset(); response.setContentType("application/force-download"); response.addHeader("Content-Disposition","attachment; filename=" + URLEncoder.encode(name, "UTF-8")); byte[] bytes = new byte[1024]; int len = 0; while ((len = in.read(bytes)) !=-1){ response.getOutputStream().write(bytes,0,len); } in.close(); response.getOutputStream().flush(); //response不需要手动关闭,tomcat会自动关闭 response.getOutputStream().close(); }
这里的url为下载地址,类似于D:\\opt\\protocol\\L07B-3O.txt 这种