JAVA中实现根据文件路径下载文件
import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class TestUtil { /* 根据文件所在路径下载文件 */ public void download(HttpServletResponse response, String filePath){ File file = new File(filePath); // 取得文件名。 String fileName = file.getName(); InputStream fis = null; try { fis = new FileInputStream(file); response.reset(); response.setCharacterEncoding("UTF-8"); response.setContentType("application/force-download");// 设置强制下载不打开 response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"), "iso8859-1")); response.setHeader("Content-Length", String.valueOf(file.length())); byte[] b = new byte[1024]; int len; while ((len = fis.read(b)) != -1) { response.getOutputStream().write(b, 0, len); } response.flushBuffer(); fis.close(); }catch (IOException e) { throw new RuntimeException(e); } } }
如以上方法在不同浏览器下载文件名乱码,可换用以下方法:
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URLEncoder; public class TestUtil { /* 根据文件所在路径下载文件 */ public void download(HttpServletRequest request, HttpServletResponse response, String filePath){ File file = new File(filePath); // 取得文件名。 String fileName = file.getName(); InputStream fis = null; try { fis = new FileInputStream(file); request.setCharacterEncoding("UTF-8"); String agent = request.getHeader("User-Agent").toUpperCase(); if ((agent.indexOf("MSIE") > 0) || ((agent.indexOf("RV") != -1) && (agent.indexOf("FIREFOX") == -1))) fileName = URLEncoder.encode(fileName, "UTF-8"); else { fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1"); } response.reset(); response.setCharacterEncoding("UTF-8"); response.setContentType("application/force-download");// 设置强制下载不打开 response.addHeader("Content-Disposition", "attachment;filename=" + fileName); response.setHeader("Content-Length", String.valueOf(file.length())); byte[] b = new byte[1024]; int len; while ((len = fis.read(b)) != -1) { response.getOutputStream().write(b, 0, len); } response.flushBuffer(); fis.close(); }catch (IOException e) { throw new RuntimeException(e); } } }