ServletFileUpload.isMultipartContent(request)返回false

时间:2018-06-13 11:20:44

标签: java

从jsp页面中选择多个文件并提交表单,在Servlet ServletFileUpload.isMultipartContent(request)中返回false。

以下是代码:

JSP代码:

<form action="multiFileSelection" enctype="multipart/form-data">
    <input type="file" id="selectedFile" multiple/>
    <input type="submit" value="Submit"/>

</form>

Servlet代码:

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
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 org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.FileItemFactory;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
import org.apache.tomcat.util.http.fileupload.servlet.ServletRequestContext;

@WebServlet("/multiFileSelection")
public class multiFileSelection extends HttpServlet {
    private static final long serialVersionUID = 1L;

public multiFileSelection() {
    super();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    boolean isMultipart = ServletFileUpload.isMultipartContent(request); 
    System.out.println("Multiple Selction returns : " + isMultipart);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request, response);
}

}

1 个答案:

答案 0 :(得分:0)

它现在正在工作。我在@WebServlet下面添加了@MultipartConfig注释(&#34; / UploadFileServlet&#34;)。