【问题标题】:Unable to upload file from servlet [duplicate]无法从 servlet 上传文件 [重复]
【发布时间】:2017-06-04 02:46:14
【问题描述】:

我用 bootstrap 写了一个 jsp。在<form> 标记中,我正在调用一个servlet 来上传文件,但是Part part = request.getPart("NewCandCV") 似乎没有获取文件,因为当我尝试在@987654324 上调用和方法时它不断抛出NullPointerException @像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>

小服务程序:

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>");
    }
}

【问题讨论】:

    标签: jsp servlets file-upload


    【解决方案1】:

    您需要使用 MultipartConfig 注释您的 servlet 以表明对多部分表单数据的支持

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-12
      • 2014-03-21
      • 2017-01-28
      • 1970-01-01
      • 1970-01-01
      • 2013-12-01
      • 2016-05-30
      • 2019-12-26
      相关资源
      最近更新 更多