当我调用setMultipartConfig
动态添加的 servletrequest.getParts();
时返回一个空集合,它应该包含上传的文件。
JSP
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form action="/{context here}/hello" method="post" enctype="multipart/form-data">
<input type="file" name="avatar">
<button>Go</button>
</form>
</body>
</html>
小服务程序:
@WebServlet( "/hello" )
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1;
@Override
protected void doGet( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException {
request.getRequestDispatcher( "/hello.jsp" ).forward( request, response );
}
@Override
protected void doPost( HttpServletRequest request, HttpServletResponse response )
throws IOException, ServletException {
// Here an empty collection even if I do select a file...
request.getParts();
}
}
动态添加的监听器
@WebListener
public class HelloListener implements ServletContextListener {
@Override
public void contextInitialized( ServletContextEvent event ) {
Dynamic registered = event.getServletContext().addServlet( "hello", HelloServlet.class );
registered.addMapping( "/hello" );
registered.setMultipartConfig( getMultiPartConfig() );
}
@Override
public void contextDestroyed( ServletContextEvent event ) {}
private MultipartConfigElement getMultiPartConfig() {
String location = "";
long maxFileSize = -1;
long maxRequestSize = -1;
int fileSizeThreshold = 0;
return new MultipartConfigElement(
location,
maxFileSize,
maxRequestSize,
fileSizeThreshold
);
}
}
如何使多部分配置与动态添加的 servlet 一起使用?
使用:JBoss EAP 6.1