将文件从 JSP 上传到 Servlet 时出现奇怪的问题
Weird issue when uploading file from JSP to Servlet
我正在尝试将 .csv 文件从 JSP 上传到 servlet。它在我的本地机器上完美运行(Websphere 8.5.5.11 上的 Java 1.8),但是当我将它移动到测试环境(WebSphere 8.5.5.12 上的Java 1.6)时,我收到一条错误消息:
java.lang.UnsupportedOperationException: SRVE8020E: Servlet does not accept multipart requests
这看起来很奇怪,因为 isMultipart 返回为真。
这是我的 JSP 表格:
<form method="POST" name="batchForm" action="BatchTierProcessing" enctype="multipart/form-data">
<input type="hidden" name="pageName" value="batchTierProcess"/>
<text>Upload File for Batch Processing:</text>
<input type="file" name="file" id="file"/>
<input type="submit" value="Upload" name="upload" id="upload"/>
</form>
这是我的 servlet:
@WebServlet(name = "UWTSAS", urlPatterns = {"/UWTSAS"}, loadOnStartup = 1)
@MultipartConfig
public class PMSTandalone extends AWRServlet {
@Override
protected void doPost(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletException, IOException {
boolean isMultipart = ServletFileUpload.isMultipartContent(aRequest);
FileItem csv = null;
String pageNameMultiPart = "";
Exception fakeException = new Exception();
PageNameEnum pageName = null;
Part filePart = null;
//if a multipart request (batch process) grab file before request is touched by anything else
if(isMultipart) {
LogMessageUtility.log("INFO", "Is multipart request", fakeException);
filePart = aRequest.getPart("file");
pageName = PageNameEnum.getValue("pmSABTS");
LogMessageUtility.log("INFO", "Page Name Multipart", fakeException);
}
else {
pageName = PageNameEnum.getValue(aRequest.getParameter("pageName"));
}
HttpSession aSession = aRequest.getSession(false);
if(aSession == null) {
aSession = aRequest.getSession(true);
}
buildUserSession(aRequest, aSession);
boolean batchSuccessful = true;
String destination = "PMStandBatchRes";
LogMessageUtility.log("INFO", "Batch Processing Started", fakeException);
try {
batchSuccessful = getFinalScoresBatch(aRequest, aResponse, aSession, filePart);
}
catch(Exception e) {
LogMessageUtility.log("ERROR", e.getMessage(), e);
}
}
if(!destination.equals("") && batchSuccessful) {
dispatchToResource(aRequest, aResponse, WebRatingProperty.getSetting(destination));
}
}
我尝试设置 Eclipse 以使用 Java6 编译该项目,但没有成功。据我所知,我无法使用 1.6 将我的本地 WebSphere 实例设置为 运行。我尝试使用 Apache Commons FileUpload 和 Commons IO 而不是通过 Java 本机执行,但没有成功,并且我开始考虑弄清楚如何手动解析请求。
任何人都知道发生了什么,或者对我应该尝试的事情有任何想法吗?我已经浏览过这些问题和答案:
How to upload files to server using JSP/Servlet?
Convenient way to parse incoming multipart/form-data parameters in a Servlet
How can I read other parameters in a multipart form with Apache Commons
How to upload files to server using JSP/Servlet?
在此先感谢您的帮助或想法。
我想通了。必须删除 @MultipartConfig 注释,并实现 javax.servlet.Servlet 接口,并使用 Apache Commons FileUpload 库。
我正在尝试将 .csv 文件从 JSP 上传到 servlet。它在我的本地机器上完美运行(Websphere 8.5.5.11 上的 Java 1.8),但是当我将它移动到测试环境(WebSphere 8.5.5.12 上的Java 1.6)时,我收到一条错误消息:
java.lang.UnsupportedOperationException: SRVE8020E: Servlet does not accept multipart requests
这看起来很奇怪,因为 isMultipart 返回为真。
这是我的 JSP 表格:
<form method="POST" name="batchForm" action="BatchTierProcessing" enctype="multipart/form-data">
<input type="hidden" name="pageName" value="batchTierProcess"/>
<text>Upload File for Batch Processing:</text>
<input type="file" name="file" id="file"/>
<input type="submit" value="Upload" name="upload" id="upload"/>
</form>
这是我的 servlet:
@WebServlet(name = "UWTSAS", urlPatterns = {"/UWTSAS"}, loadOnStartup = 1)
@MultipartConfig
public class PMSTandalone extends AWRServlet {
@Override
protected void doPost(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletException, IOException {
boolean isMultipart = ServletFileUpload.isMultipartContent(aRequest);
FileItem csv = null;
String pageNameMultiPart = "";
Exception fakeException = new Exception();
PageNameEnum pageName = null;
Part filePart = null;
//if a multipart request (batch process) grab file before request is touched by anything else
if(isMultipart) {
LogMessageUtility.log("INFO", "Is multipart request", fakeException);
filePart = aRequest.getPart("file");
pageName = PageNameEnum.getValue("pmSABTS");
LogMessageUtility.log("INFO", "Page Name Multipart", fakeException);
}
else {
pageName = PageNameEnum.getValue(aRequest.getParameter("pageName"));
}
HttpSession aSession = aRequest.getSession(false);
if(aSession == null) {
aSession = aRequest.getSession(true);
}
buildUserSession(aRequest, aSession);
boolean batchSuccessful = true;
String destination = "PMStandBatchRes";
LogMessageUtility.log("INFO", "Batch Processing Started", fakeException);
try {
batchSuccessful = getFinalScoresBatch(aRequest, aResponse, aSession, filePart);
}
catch(Exception e) {
LogMessageUtility.log("ERROR", e.getMessage(), e);
}
}
if(!destination.equals("") && batchSuccessful) {
dispatchToResource(aRequest, aResponse, WebRatingProperty.getSetting(destination));
}
}
我尝试设置 Eclipse 以使用 Java6 编译该项目,但没有成功。据我所知,我无法使用 1.6 将我的本地 WebSphere 实例设置为 运行。我尝试使用 Apache Commons FileUpload 和 Commons IO 而不是通过 Java 本机执行,但没有成功,并且我开始考虑弄清楚如何手动解析请求。
任何人都知道发生了什么,或者对我应该尝试的事情有任何想法吗?我已经浏览过这些问题和答案:
How to upload files to server using JSP/Servlet?
Convenient way to parse incoming multipart/form-data parameters in a Servlet
How can I read other parameters in a multipart form with Apache Commons
How to upload files to server using JSP/Servlet?
在此先感谢您的帮助或想法。
我想通了。必须删除 @MultipartConfig 注释,并实现 javax.servlet.Servlet 接口,并使用 Apache Commons FileUpload 库。