将文件从JSP上传到Servlet时出现奇怪的问题

时间:2018-09-07 13:02:31

标签: java jsp servlets multipartform-data

我正在尝试将.csv文件从JSP上传到Servlet。它在我的本地计算机(Websphere 8.5.5.11上的Java 1.8)上可以完美运行,但是当我将其移至测试环境(WebSphere 8.5.5.12上的Java 1.6)时,出现错误消息:

  

java.lang.UnsupportedOperationException:SRVE8020E:Servlet不接受多部分请求

这看起来很奇怪,因为isMultipart返回为true。

这是我的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设置为使用Java 6编译该项目,但没有运气。据我所知,我无法将本地WebSphere实例设置为使用1.6运行。我尝试使用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?

在此先感谢您的帮助或想法。

1 个答案:

答案 0 :(得分:0)

我知道了。必须删除@MultipartConfig批注,并实现javax.servlet.Servlet接口,并使用Apache Commons FileUpload库。

相关问题