注意:enctype="multipart/form-data" 是用来设置表单的MIME编码。默认情况,这个编码格式是
application/x-www-form-urlencoded,不能用于文件上传,只有使用了multipart/form-data,才能完整的传递文件数据。multiple="multiple" 设置该属性可以一次选择多个文件(批量上传可以使用)。
可以使用ajax提交form表单:$("#uploadForm").submit();
如果需要传递带参数的url可以先给action赋值:$("#uploadForm").attr('action',‘url?id=’+id); 然后再提交表单。
public void downloadFile(HttpServletRequest request, HttpServletResponse response) throws Exception { response.setContentType("text/html;charset=UTF-8"); request.setCharacterEncoding("UTF-8"); BufferedInputStream bis = null; BufferedOutputStream bos = null; String fileName = request.getParameter("fileName"); // 下载文件名 String ctxPath = request.getSession().getServletContext().getRealPath("");// 获得项目部署路径 ctxPath = ctxPath + "aa\\" + fileName; // Windows环境 //ctxPath = ctxPath + "aa/" + fileName;// Linux环境 long fileLength = new File(ctxPath).length(); response.setContentType("application/octet-stream"); response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("UTF-8"), "iso-8859-1")); // 解决中文名称乱码问题 response.setHeader("Content-Length", String.valueOf(fileLength)); bis = new BufferedInputStream(new FileInputStream(ctxPath)); bos = new BufferedOutputStream(response.getOutputStream()); byte[] buff = new byte[2048]; int bytesRead; while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { bos.write(buff, 0, bytesRead); } bis.close(); bos.close(); }