亲试好用,不过我用herf调servlet下载的,用herf走get方法,转自:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String pathsavefile="C:/测试文件(下载).txt";//要下载的文件 String fileName="下载返回文件测试.txt";//保存窗口中显示的文件名 try { response.reset(); response.setContentType("APPLICATION/OCTET-STREAM");/*要显示到客户端的文件名转码是必需的,特别是中文名, * 否则可能出现文件名乱码甚至是浏览器显示无法下载的问题*/ fileName=response.encodeURL(newString(fileName.getBytes(),"ISO8859_1"));//转码 response.setHeader("Content-Disposition", "p_w_upload; filename=\""+fileName+"\""); ServletOutputStream out = response.getOutputStream(); InputStream inStream=new FileInputStream(pathsavefile); //循环取出流中的数据 byte[] b = new byte[1024]; int len; while((len=inStream.read(b)) >0) out.write(b,0,len); response.setStatus( response.SC_OK ); response.flushBuffer(); out.close(); inStream.close(); } catch (Exception e) { System.out.println(e); } }