Jsp 无法从servlet上载文件

Jsp 无法从servlet上载文件,jsp,servlets,file-upload,Jsp,Servlets,File Upload,我已经用bootstrap编写了一个jsp。在标记中,我调用了一个servlet来上传文件,但是Part Part=request.getPart(“NewCandCV”)似乎没有提取文件,因为当我尝试调用Part对象上的Part方法时,它会不断抛出NullPointerException 任何帮助都将不胜感激。 下面是全部代码。 我无法理解我错过了什么: web.xml: <servlet> <servlet-name>Add New Candida

我已经用bootstrap编写了一个jsp。在
标记中,我调用了一个servlet来上传文件,但是
Part Part=request.getPart(“NewCandCV”)
似乎没有提取文件,因为当我尝试调用
Part
对象上的
Part
方法时,它会不断抛出
NullPointerException
任何帮助都将不胜感激。 下面是全部代码。
我无法理解我错过了什么:

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>");
    }
}
public类AddCandidate扩展了HttpServlet{
私有字符串intFilePath;
私有字符串extFilePath;
公共void init(){
intFilePath=getServletContext().getInitParameter(“文件上载内部”);
extFilePath=getServletContext().getInitParameter(“文件上载外部”);
}
public void doPost(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException
{   
response.setContentType(“text/html”);
PrintWriter=response.getWriter();
字符串名称=request.getParameter(“NewCandName”);
Part Part=request.getPart(“NewCandCV”);
字符串CV=part.getName();
尝试
{
字符串路径=”;
if(配置文件等于(“TCS内部”))
path=intFilePath;
else if(profile.equals(“外部”))
path=extFilePath;
//获取web应用程序的绝对路径
字符串appPath=request.getServletContext().getRealPath(“”);
//构造目录的路径以保存上载的文件
字符串savePath=appPath+“/”+path;
//如果不存在保存目录,则创建该目录
File fileSaveDir=新文件(保存路径);
如果(!fileSaveDir.exists()){
fileSaveDir.mkdir();
}
字符串fileName=“CV_”+姓名+“”+联系人;
写入(savePath+File.separator+fileName);
/*for(部分:request.getParts()){
tmp=零件;
字符串fileName=“CV_”+姓名+“”+联系人;
写入(savePath+File.separator+fileName);
} */
}
捕获(例外e){
writer.println(“”)
+“例外!!”
+e+);
}
writer.println(“”)
+“上传完成”);
}
}

您需要对servlet进行注释,以表明对多部分formdata的支持