自己对照别的博主的博客实现的,记录用。
整个上传的结构如下:
上传的页面:unload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>文件上传
提示上传信息message.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>提示消息 ${message}
处理上传的servlet
package Servlet;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.fileupload.FileItem;import org.apache.commons.fileupload.disk.DiskFileItemFactory;import org.apache.commons.fileupload.servlet.ServletFileUpload;public class UploadHandleServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String savepath=this.getServletContext().getRealPath("/WEB-INF/upload"); File file=new File(savepath); System.out.println(savepath); if(!file.exists() && !file.isDirectory()) { System.out.println(savepath+"目录不存在创建"); file.mkdir(); } String message=""; try{ System.out.println("1"); DiskFileItemFactory fctory=new DiskFileItemFactory(); ServletFileUpload fileuplaod=new ServletFileUpload(fctory); fileuplaod.setHeaderEncoding("UTF-8"); if(!ServletFileUpload.isMultipartContent(request)){ System.out.println("5"); return; } System.out.println("6"); Listlist=fileuplaod.parseRequest(request); for(FileItem item : list){ if(item.isFormField()){ System.out.println("2"); String name=item.getFieldName(); String value=item.getString("UTF-8"); System.out.println(name+"="+value); }else{ System.out.println("3"); String name=item.getName(); System.out.println(name); if(name==null||name.trim().equals("")){ continue; } name=name.substring(name.lastIndexOf("\\")+1); InputStream in=item.getInputStream(); FileOutputStream out =new FileOutputStream(savepath+"\\"+name); byte buffer[]=new byte[1024]; int len=0; while((len=in.read(buffer))>0){ out.write(buffer, 0, len);; } System.out.println("4"); in.close(); out.close(); item.delete(); message="文件上传成功"; } } }catch(Exception e){ message="文件上传失败!"; e.printStackTrace(); } request.setAttribute("message", message); request.getRequestDispatcher("../message.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); }}
映射:
This is the description of my J2EE component This is the display name of my J2EE component UploadHandleServlet Servlet.UploadHandleServlet UploadHandleServlet /Servlet/UploadHandleServlet
上传结果:
这里需要注意:
if(!ServletFileUpload.isMultipartContent(request)){ return; }
判断上传的类型,在unload.jsp form表单定类型
enctype="multipart/form-data"
file1和file2是在获取上传文件的名字是出错了才出现的结果,getFiledName获取普通输入项,如上传的用户名, getName获取上传的文件名称
String name=item.getFieldName();