问题描述
- multipart/form-data提取不出数据来
-
<form id="resiger" name="resiger" method="post" action="Student.servlet" enctype="multipart/form-data"> <input type="text" name="action" value="doNewStudent">..... <input type="file" name="photo" id="photo" class="wbk"/><span id="photo"> <input type="submit" name="button" id="button" value="添 加" /> <input type="reset" name="button2" id="button2" value="清 除" /> <input type="button" name="button3" id="button3" value="返回首页"/> --没有给表单加上enctype="multipart/form-data"的时候,能够把全部的数据提取出来,如action,只是提取不出文本域的中的文件而已,这是正常的,但是加上enctype="multipart/form-data"之后,却连普通的数据都提取不出来了,让老师看了下代码,他说没什么问题,可能是配置或者哪儿出问题了,但我就是找不出来(我是在servlet中提取的)------- package com.servlet; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Part; import com.commons.db.Pager; import com.commons.util.ActionResult; import com.commons.util.Validator; import com.factory.ServiceFactory; import com.model.Student; import com.service.StudentService; @WebServlet(name="StudentServlet",urlPatterns="/Student.servlet") public class StudentServlet extends HttpServlet { private StudentService studentService=ServiceFactory.getStudentService(); private static final long serialVersionUID = 1L; public StudentServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); String action=request.getParameter("action"); System.out.println("动作"+action); if(action==null){ this.doNewStudent(request, response); } else if(action.equals("goPage")){ this.goPage(request, response); }else if(action.equals("goList")){ // this.goList(request, response); this.findAll(request, response); }else if(action.equals("doNewStudent")){ this.doNewStudent(request,response); }else if(action.equals("goUpdate")){ goUpdate(request,response); }else if(action.equals("doUpdate")){ this.doUpdate(request, response); }else if(action.equals("doDelete")){ this.doDelete(request, response); }else if(action.equals("fenYe")){ this.findAll(request, response); } } protected void goPage(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { String url=""; url=request.getParameter("url"); response.sendRedirect(url); } protected void goList(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { List<Student> lists=this.studentService.findAllStudent(); request.getSession().setAttribute("lists", lists); response.sendRedirect("adminOperator.jsp"); } protected void doNewStudent(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { String name=request.getParameter("name"); System.out.println(name); int age=Integer.parseInt(request.getParameter("age")); String sex=request.getParameter("sex"); String grade=request.getParameter("grade"); String major=request.getParameter("major"); String address=request.getParameter("address"); Part part=request.getPart("photo"); // 获取文件扩展名 System.out.println("长度是:"+part.getSize()); String ext = part.getHeader("content-disposition").substring( part.getHeader("content-disposition").lastIndexOf(".") + 1, part.getHeader("content-disposition").length() - 1);// 获取文件扩展 part.write(this.getServletContext().getRealPath("photo")+"\"+ext); Date date=new Date(); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-S"); String stuPhoto=sdf.format(date); // 将文件放入到服务器磁盘 part.write(this.getServletContext().getRealPath("ptoto")+"\"+stuPhoto); Student student=new Student(name, sex, age, grade, major, address, stuPhoto); boolean flag=this.studentService.addStudent(student); if(flag){ this.goList(request, response); }else{ request.setAttribute("student", student); request.getRequestDispatcher("newStudent.jsp").forward(request, response); } } protected void goUpdate(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ String sid=request.getParameter("sid"); Student student=this.studentService.serchStudnet(sid); request.setAttribute("student", student); request.getRequestDispatcher("update.jsp").forward(request, response); } protected void doUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ String sid=request.getParameter("sid"); String name=request.getParameter("name"); int age=Integer.parseInt(request.getParameter("age")); String sex=request.getParameter("sex"); String grade=request.getParameter("grade"); String major=request.getParameter("major"); String address=request.getParameter("address"); Part part=request.getPart("photo"); // 获取文件扩展名 String ext=part.getHeader("content-dispoistion").substring( part.getHeader("content-dispoistion").indexOf(".")+1,part.getHeader("content-dispoistion").length()-1); part.write(this.getServletContext().getRealPath("photo")+"\"+ext); Date date=new Date(); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-S"); String stuPhoto=sdf.format(date); // 将文件放入到服务器磁盘 part.write(this.getServletContext().getRealPath("ptoto")+"\"+stuPhoto); Student student=new Student(name, sex, age, grade, major, address, stuPhoto); student.setSid(sid); boolean flag=studentService.updateStudent(student); if(flag){ this.goList(request, response); }else{ request.setAttribute("student", student); request.getRequestDispatcher("update.jsp").forward(request, response); } } protected void doDelete(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { String sid=request.getParameter("sid"); boolean flag=studentService.deleteStudent(sid); if(flag){ this.goList(request, response); }else{ response.sendRedirect("adminOperator.jsp"); } } protected void findAll(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { String pageNoStr=request.getParameter("pageNo"); int pageNo=1; if(!Validator.isEmpty(pageNoStr)){ pageNo=Integer.parseInt(pageNoStr); } long rowCount=studentService.getStusRowCount(); Pager pager=new Pager(rowCount, 3, pageNo); List<Student> stuList=studentService.findAllStudent(pager); request.getSession().setAttribute("lists", stuList); request.getSession().setAttribute("pager", pager); System.out.println(pager.getCurrentPage()); response.sendRedirect("adminOperator.jsp"); // return new ActionResult("adminOperator.jsp",true); } }
解决方案
你自己用纯Servlet来实现页面文件流的提取,这行Part part=request.getPart("photo");是不能实现的。
你需要自己解析HttpServletRequest对象中的文件流数据。参考该帖九楼:http://bbs.csdn.net/topics/310136006
解决方案二:
multipart/form-data 表示你要通过解析binary数据来获取返回的参数
时间: 2024-12-03 06:55:34