1.获取文件上传路径
String path=request.getServletContext.getRePath("/WEB-INF/resources");
2.获得工厂
3.通过servletFileUpLoad 解析返回一个List<FileItem>集合
4.进行遍历 根据isFiled 判断是普通文本,然后进行遍历,
5.获得文件输入流,和输入流
6.文件上传成功
代码
1 package jquery; 2 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.io.OutputStream; 8 import java.util.List; 9 10 import javax.servlet.ServletException; 11 import javax.servlet.annotation.WebServlet; 12 import javax.servlet.http.HttpServlet; 13 import javax.servlet.http.HttpServletRequest; 14 import javax.servlet.http.HttpServletResponse; 15 16 import org.apache.commons.fileupload.FileItem; 17 import org.apache.commons.fileupload.FileUploadException; 18 import org.apache.commons.fileupload.disk.DiskFileItemFactory; 19 import org.apache.commons.fileupload.servlet.ServletFileUpload; 20 21 @WebServlet("/upload") 22 public class UploadServlet extends HttpServlet{ 23 24 25 @Override 26 public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 27 //获得上传路径 28 String savePath=this.getServletContext().getRealPath("/resources"); 29 30 File file=new File(savePath); 31 if(!file.isDirectory()&&!file.exists()){ 32 file.mkdir(); 33 } 34 35 //文件上传 36 37 //创建工厂 38 DiskFileItemFactory factory=new DiskFileItemFactory(); 39 40 //创建解析器 41 ServletFileUpload upload=new ServletFileUpload(factory); 42 43 //解决上传文件乱码问题 44 upload.setHeaderEncoding("UTF-8"); 45 46 //判断提交过来的是不是表单数据 47 if(!upload.isMultipartContent(request)){ 48 return; 49 } 50 51 //使用servletFileUpLoad 将解析结果返回List<FileItem> 结合中FileItem对应表单输入项 52 53 try { 54 List<FileItem> item=upload.parseRequest(request); 55 for (FileItem fileItem : item) { 56 String message=""; 57 //如果不是file类型 58 if(fileItem.isFormField()){ 59 System.out.println(fileItem.getString()); 60 }else{ 61 //是上传文件 62 //得到上传文件名 63 String filename=fileItem.getName(); 64 System.out.println(filename); 65 if(filename==null||fileItem.equals("")){ 66 continue; 67 } 68 System.out.println(filename); 69 //获得item 文件输入流 70 InputStream in=fileItem.getInputStream(); 71 //创建文件输出流 72 OutputStream out=new FileOutputStream(savePath+"\"+filename); 73 System.out.println(savePath+"\"+filename); 74 //创建缓冲区 75 byte[] buffer=new byte[1024]; 76 int len=0; 77 while((len=in.read(buffer))!=-1){ 78 out.write(buffer, 0, len); 79 } 80 in.close(); 81 out.close(); 82 fileItem.delete(); 83 System.out.println("上传成功"); 84 } 85 } 86 } catch (FileUploadException e) { 87 e.printStackTrace(); 88 } 89 90 91 } 92 }
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>Insert title here</title> 8 <script type="text/javascript" 9 src="${pageContext.request.contextPath }/js/jquery-1.9.1.min.js"></script> 10 </head> 11 <body> 12 13 <form action="${pageContext.request.contextPath }/upload" method="post" 14 enctype="multipart/form-data" method="post"> 15 上传用户名:<input type="text" name="username" /><br /> <input id="file1" 16 type="file" name="file1" /><img src="c/fakepath/1.jpg"> <input 17 type="file" name="file2" /> <input type="submit" value="上传" /> 18 </form> 19 </body> 20 <script type="text/javascript"> 21 </script> 22 </html>