1.servlet代码: import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.jspsmart.upload.SmartUpload; public class LogDownLoad extends HttpServlet { //设置此项即告诉SERVLET是下载文件,而不是网页或TXT.(可以弹出对话框) private static final String CONTENT_TYPE = "APPLICATION/OCTET-STREAM"; public void init() throws ServletException { } //Process the HTTP Get request public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // response.setContentType(CONTENT_TYPE); // PrintWriter out = response.getWriter(); //注意,这里给注释掉了,如果不注释会出现 //java.lang.IllegalStateException: getWriter() has already been called for this response //这样的错误.,这是因为写文件流时,已经有个GETWRITER打开了.,但在下面输出时,可以在IF{}内用. //Clears any data that exists in the buffer as well as the status code and headers. If the response has been committed, this method throws an IllegalStateException. response.reset(); String fileName = ""; //文件名,包括后缀名 String filePath = " "; //文件路径,包括文件名+后缀名 filePath=request.getParameter("path"); if(filePath.equals("rulePath")) { filePath=request.getSession().getServletContext().getRealPath("/"); if(!filePath.endsWith(File.separator)) { filePath+=File.separator; } filePath+="task//TaskRules.xls"; System.out.println("contextpath"+filePath); } fileName= toUtf8String(new File(filePath).getName()); System.out.println("filepath="+filePath); System.out.println("filename="+fileName); response.setContentType("application/x-zip-compressed"); response.addHeader("Content-Disposition","attachment;filename=" + fileName ); OutputStream os = null; FileInputStream fis = null; try { File f=new File(filePath); os = response.getOutputStream(); fis = new FileInputStream(f); byte[] b = new byte[1024]; int i = 0; while((i = fis.read(b)) > 0) { os.write(b, 0 ,i); } os.flush(); System.out.println("333"); } catch(Exception e) { System.out.println(e.getMessage()); } finally { if(fis != null) { fis.close(); fis = null; } if(os != null) { os.close(); os = null; } } } //Process the HTTP Post request public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } private String toUtf8String(String s) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c >= 0 && c <= 255) { sb.append(c); } else { byte[] b; try { b = Character.toString(c).getBytes("utf-8"); } catch (Exception ex) { System.out.println(ex); b = new byte[0]; } for (int j = 0; j < b.length; j++) { int k = b[j]; if (k < 0) k += 256; sb.append("%" + Integer.toHexString(k).toUpperCase()); } } } return sb.toString(); } //Clean up resources public void destroy() { } } 2.web.xml加入: <servlet> <servlet-name>LogDownLoad</servlet-name> <servlet-class>com.tkdm04.cmd.dataprocess.dataprocess.LogDownLoad</servlet-class> </servlet> 3.页面上调用js方法: <script language="javascript"> function callDownLoad() { document.forms[0].action="/tkdm04/LogDownLoad?path="+document.getElementById("logPath").value.trim()+"" document.forms[0].submit(); } </script>