无法从servlet上传文件

时间:2017-06-04 02:46:14

标签: jsp servlets file-upload

我用bootstrap编写了一个jsp。在<form>标记中,我调用servlet来上传文件,但Part part = request.getPart("NewCandCV")似乎没有提取文件,因为当我尝试时它会继续抛出NullPointerException part part.getName()对象上的通话和方法 任何帮助将受到高度赞赏。 以下是整个代码 我不明白我错过了什么:

的web.xml:

  <servlet>
        <servlet-name>Add New Candidate</servlet-name>
        <servlet-class>AddCandidate</servlet-class>

        <init-param> 
            <param-name>file-upload-internal</param-name> 
            <param-value>Resume/internal</param-value> 
        </init-param>

        <init-param> 
            <param-name>file-upload-external</param-name> 
            <param-value>Resume/external</param-value> 
        </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>Add New Candidate</servlet-name>
    <url-pattern>/Add_Candidate</url-pattern>
  </servlet-mapping>

JSP:

<div id="newProfileModal" class="modal fade" role="dialog">
          <div class="modal-dialog">

            <!-- Modal content-->
            <form method="post" action="Add_Candidate" enctype="multipart/form-data">
                <div class="form-group">
                    <div class="modal-content">
                      <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                        <div class="medium">
                            <h4 class="modal-title">Name Candidate</h4>
                        </div>
                      </div>
                      <div class="modal-body">
                          <label for="usr">Name:</label>
                          <input type="text" id="usr" class="form-control" name="NewCandName"></input>
                          <br/>
                          <label for="attachment">Attach CV</label>
                          <input id="attachment" name="NewCandCV" type="file" class="file-loading" />
                      </div>
                      <div class="modal-footer">
                        <button type="submit" class="btn btn-default">Add</button>
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                      </div>
                    </div>
                </div>
            </form>
          </div>
        </div>

Servlet:

public class AddCandidate extends HttpServlet{

    private String intFilePath;
    private String extFilePath;


    public void init( ){
        intFilePath = getServletContext().getInitParameter("file-upload-internal");
        extFilePath = getServletContext().getInitParameter("file-upload-external");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {   
        response.setContentType("text/html");  
        PrintWriter writer = response.getWriter(); 

        String name = request.getParameter("NewCandName");
        Part part = request.getPart("NewCandCV");
        String CV = part.getName();
        try
        {
            String path="";

            if(profile.equals("TCS Internal"))
                path = intFilePath;
            else if(profile.equals("External"))
                path = extFilePath;

            // gets absolute path of the web application
            String appPath = request.getServletContext().getRealPath("");
            // constructs path of the directory to save uploaded file
            String savePath = appPath + "/" + path;

            // creates the save directory if it does not exists
            File fileSaveDir = new File(savePath);
            if (!fileSaveDir.exists()) {
                fileSaveDir.mkdir();
            }


            String fileName = "CV_"+name+"_"+contact;
            part.write(savePath + File.separator + fileName);
       /*     for (Part part : request.getParts()) {
                tmp=part;
                String fileName = "CV_"+name+"_"+contact;
                part.write(savePath + File.separator + fileName);
            } */

        }
        catch(Exception e){
            writer.println("<html><head></head>"
                    +"<body>Exception!!"
                    +e+"</body></html>");
        }


        writer.println("<html><head></head>"
                +"<body>Upload completed</body></html>");
    }
}

1 个答案:

答案 0 :(得分:0)

您需要注释您的servlet以指示对MultipartConfig

的多部分表单数据的支持