【问题标题】:File upload problems with ServletFileUploadServletFileUpload 的文件上传问题
【发布时间】:2014-07-01 01:10:05
【问题描述】:

我找到了问题,在下面回答:

原问题: 菜鸟程序员在这里。 我正在开发一个 openfire 服务器项目,我们的团队正在尝试集成一个苹果推送通知服务插件。但是,我发现的插件代码有问题,jsp页面永远无法显示在openfire的管理控制台中。

经过数小时的手动调试(因为我完全烂透了,对 apache 的文件上传模块一无所知),我发现问题出在这行之间:

List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);

如果我禁用整个 try 子句,页面会正常显示。当然,这意味着这个jsp页面将失去保存上传文件的能力。在又无数个小时的搜索这条线之后,我什么也没发现,所以我决定在这里问它并尝试一下。

我不太明白这一行出了什么问题,eclipse 给了我Type safety: The expression of type List needs unchecked conversion to conform to List&lt;FileItem&gt; 的警告,但是插件编译和构建都很好。语法对我来说也很合适(谷歌搜索了一段时间,看到人们也使用相同的语法)。 Openfire 在调试方面毫无用处,error.log 或 debug.log 中没有记录任何有价值的信息。

如果有帮助,这里是整个 jsp 文件。提前致谢。

apns.jsp:

<%@ page import="java.io.File,
                 java.util.List,
                 org.jivesoftware.openfire.XMPPServer,
                 org.jivesoftware.util.*,
                 com.wecapslabs.openfire.plugin.apns.ApnsPlugin,
                 org.apache.commons.fileupload.FileItem,
                 org.apache.commons.fileupload.disk.DiskFileItemFactory,
                 org.apache.commons.fileupload.servlet.ServletFileUpload,
                 org.apache.commons.fileupload.FileUploadException"
    errorPage="error.jsp"
%>

<%@ page contentType="text/html; charset=UTF-8"%>  
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>  
<%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt" %>  

<%-- Define Administration Bean --%>  
<jsp:useBean id="admin" class="org.jivesoftware.util.WebManager"  />  
<c:set var="admin" value="${admin.manager}" />  
<% admin.init(request, response, session, application, out ); %>  

<%  // Get parameters
    boolean save = request.getParameter("save") != null;
    boolean success = request.getParameter("success") != null;
    boolean error = request.getParameter("error") != null;
    String password = ParamUtils.getParameter(request, "password");
    String badge = ParamUtils.getParameter(request, "badge");
    String sound = ParamUtils.getParameter(request, "sound");
    String production = ParamUtils.getParameter(request, "production");

    ApnsPlugin plugin = (ApnsPlugin) XMPPServer.getInstance().getPluginManager().getPlugin("apns");

    // Handle a save
    if (save) {
        plugin.setPassword(password);
        plugin.setBadge(badge);
        plugin.setSound(sound);
        plugin.setProduction(production);

         try {
            List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
            for (FileItem item : multiparts) {
                if (!item.isFormField()) {
                    String filename = item.getName();
                    item.write(new File(ApnsPlugin.keystorePath()));
                }
            }
            response.sendRedirect("apns.jsp?success=true");
            return;
        } catch (Exception e) {
            response.sendRedirect("apns.jsp?error=true");
            return;
        } 

    }

    password = plugin.getPassword();
    badge = Integer.toString(plugin.getBadge());
    sound = plugin.getSound();
    production = plugin.getProduction() ? "true" : "false";
%>

<html>
    <head>
        <title>APNS Settings Properties</title>
        <meta name="pageID" content="apns-settings"/>
    </head>
    <body>

<%  if (success) { %>
    <div class="jive-success">
    <table cellpadding="0" cellspacing="0" border="0">
    <tbody>
        <tr><td class="jive-icon"><img src="images/success-16x16.gif" width="16" height="16" border="0"></td>
        <td class="jive-icon-label">
            APNS certificate updated successfully.
        </td></tr>
    </tbody>
    </table>
    </div><br>
<% } %>

<form action="apns.jsp?save" method="post" enctype="multipart/form-data">

<div class="jive-contentBoxHeader">APNS certificate</div>
<div class="jive-contentBox">
    <label for="file">p12 certificate:</label>
    <input type="file" name="file" />
    <br>

    <label for="password">certificate password:</label>
    <input type="password" name="password" value="<%= password %>" />
    <br>

    <label for="badge">payload badge</label>
    <input type="badge" name="badge" value="<%= badge %>" />
    <br>

    <label for="sound">payload sound</label>
    <input type="badge" name="sound" value="<%= sound %>" />
    <br>

    <label for="production">sandbox or production</label>
    <input type="radio" name="production" value="false" <%= production.equals("true") ? "" : "checked" %>>Sandbox
    <input type="radio" name="production" value="true" <%= production.equals("true") ? "checked" : "" %>>Production
</div>
<input type="submit" value="Save">
</form>


</body>
</html>

【问题讨论】:

    标签: jsp openfire apache-commons-fileupload


    【解决方案1】:

    让我在这里回答我自己的问题--

    因为 eclipse 没有警告我导入问题,所以我以为插件需要的所有东西都包含在内,事实并非如此。

    因为 openfire 并没有真正告诉你出了什么问题,所以你不知道它只是缺少导入。

    由于插件无法通过maven openfire插件安装(作者停止支持插件...),我不得不手动将源文件链接到openfire源代码中,不可避免地错过了maven为我们做的最重要的事情——依赖检查。

    事实证明,我只需要 commons-fileupload.jar 和 commons-io.jar,根据这篇文章: How to upload files to server using JSP/Servlet?

    将文件放入 {plugin-name}/lib 中,重新构建,然后瞧。

    【讨论】:

      猜你喜欢
      • 2012-10-14
      • 1970-01-01
      • 1970-01-01
      • 2010-10-17
      相关资源
      最近更新 更多