程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> java-multipart/form-data提取不出數據來

java-multipart/form-data提取不出數據來

編輯:編程解疑
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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved