【问题标题】:Weird issue when uploading file from JSP to Servlet将文件从 JSP 上传到 Servlet 时出现奇怪的问题
【发布时间】:2019-02-12 20:15:39
【问题描述】:

我正在尝试将 .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 返回为真。

这是我的 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?

提前感谢您的任何帮助或想法。

【问题讨论】:

  • 不相关,但我不明白这里的aRequest.getSession() 代码:getSession(true) 如果有一个会话,则返回当前会话,或者创建一个新会话。您的支票显得多余。
  • [1] 您在本地机器上测试时使用的是 WebSphere for Developers 8.5.5.11,而在测试环境中运行时使用的是完整的 WebSphere 服务器 8.5.5.12?如果是这样,那么两种 WebSphere 风格之间的可选补丁或功能可能会有所不同。 [2] 对于这个问题UnsupportedOperationException: SRVE8020E: Servlet does not accept multipart requests,发帖人报告说他们必须对 WebSphere 8.5.5.0 应用补丁。 [3] 测试服务器上是否有其他应用正在成功上传文件?

标签: java jsp servlets multipartform-data


【解决方案1】:

我想通了。不得不去掉@MultipartConfig 注解,实现javax.servlet.Servlet 接口,并使用Apache Commons FileUpload 库。

【讨论】:

    猜你喜欢
    • 2011-04-02
    • 1970-01-01
    • 2011-07-22
    • 1970-01-01
    • 1970-01-01
    • 2015-04-06
    • 2019-11-28
    • 2011-01-19
    • 1970-01-01
    相关资源
    最近更新 更多