0
當我將setMultipartConfig
調用到動態添加的servlet時,request.getParts();
應該包含上傳的文件時返回一個空集合。如何使setMultipartConfig適用於動態添加的servlet?
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>
該servlet:
@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
);
}
}
如何讓多配置與dinamically添加servlet來工作?
使用: 的JBoss EAP 6.1
這似乎是'@WebServlet( 「/你好」)'聲明,想通了這一點正確,然後再發布... –