上传: final long MAX_SIZE = 100*1024 * 1024; String path = getServletContext().getRealPath("/download"); boolean isMultipart = FileUpload.isMultipartContent(request); if (isMultipart) { DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setSizeThreshold(10 * 10 * 1024); factory.setRepository(new File(path + "/temp")); FileUpload upload = new FileUpload(factory); upload.setFileSizeMax(MAX_SIZE); upload.setHeaderEncoding("gb2312"); Files files=new Files(); try { List<FileItem> filelist = upload.parseRequest(request); for (Iterator<FileItem> iterator = filelist.iterator(); iterator .hasNext();) { FileItem item = iterator.next(); if (item.isFormField()) { String name = item.getFieldName(); String value = item.getString("gb2312"); if ("fName".equals(name)) { files.setFName(value); } if ("explanation".equals(name)) { files.setExplanation(value); } } else { String filename = item.getName(); int j=filename.lastIndexOf("//"); String suxString = filename.substring(j + 1); File upFile = new File(path, suxString); item.write(upFile); files.setPath("/download/"+suxString); } } } catch (FileUploadException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } 下载: try { //服务器所在地址 String local=request.getSession().getServletContext().getRealPath("")+"//"; String path = local+filePath; System.out.println("=================下载地址:"+path); File file = new File(path); String filename = file.getName(); //取得文件的扩展名ext String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase(); InputStream fis = new BufferedInputStream(new FileInputStream(path)); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); response.reset(); response.addHeader("Content-Disposition", "attachment;filename="+new String(filename.getBytes())); response.addHeader("Content-Length", ""+file.length()); //设置返回的文件类型 OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); //得到向客户端输出二进制数据的对象 //根据扩展名声称客户端浏览器mime类型 if(ext.equals("DOC")) response.setContentType("application/msword"); else response.setContentType("application/octet-stream"); //设置返回的文件类型 toClient.write(buffer); //输出数据 toClient.flush(); toClient.close(); } catch(IOException ex){ ex.printStackTrace(); }