java中下载文件最好用servlet,我在用普通的类实现下载功能时提示getOutputStream has already been commit 的error,查了好多资料都没有解决,(有的说response要reset,有的说jsp页面中不能换行等等,我试过后都不行)用servlet来做则不会有这个问题,源代码如下:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String fileName = (String) request.getParameter("fileName");
String path = "/dept/hr/files/training/" + fileName;
String realPath = this.getServletContext().getRealPath(path);
File f = new File(realPath);
if (!f.exists()) {
response.sendError(404, "File not found!");
return;
}
fileName = new String(fileName.getBytes("GB2312"), "ISO8859_1"); //处理文件中含中文的问题,不是万能的
boolean isOnLine = false;