如何从SlingHttpServletRequest检索文件?

时间:2018-11-07 10:21:12

标签: ajax jsp servlets apache-felix sling

我对开发完全陌生。因此,如果我有任何疑问,请原谅我。我正在尝试将多个文件上传到Web服务器。该网络服务器正在Adobe Enterprise Manager上运行。我有一个JSP页面,该页面通过AJAX调用将表单作为 SlingHttpServletRequest 提交给servlet。

我的Jsp页面以及AJAX调用:

<div class="content">
        <form method="POST" action="/servletpath" enctype="multipart/form-data" id="form-id">
              <table>
                        <tr>
                            <td><label>...</label></td>
                            <td>
                            <select id="id1">
                              <option value="option1">option1</option>
                              <option value="option2">option2</option>
                              <option value="option3">option3</option>
                              <option value="option4">option4</option>
                            </select>
                            </td>
                        </tr>
                        <tr>
                            <td><label>...</label></td>
                            <td>
                                <select id="id2">
                                  <option value="option1">option1</option>
                              <option value="option2">option2</option>
                              <option value="option3">option3</option>
                              <option value="option4">option4</option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td><label>Choose Files</label></td>
                            <td><input id="fileInput" type="file" name="uploadingFiles" onchange="updateSize();" multiple>
                                selected files: <span id="fileNum">0</span>;
                                total size: <span id="fileSize">0</span></td>
                        </tr>
                        <tr>
                            <td><input type="submit" value="Upload files" onclick="uploadFiles()"></td>
                            <td><input type="button" value="Start Comparison" onclick="location.href='/startComparison'"></td>
                        </tr>
                        </table>
                </form>

               <script type="text/javascript">
                    var uploadFiles =  function () {
                    alert("inside uploadFiles");

                    var formData = new FormData();
                    formData = formData.append("file1",document.getElementById("fileInput").files[0].name);
                    alert("appended file..."+document.getElementById("fileInput").files[0].name);

                    var statusWin = CQ.Ext.Msg.wait("Processing request", "Uploading Files...");
                         CQ.Ext.Ajax.request({
                                url: CQ.HTTP.externalize("/servletpath"),
                                method: "POST",
                                processData:false,
                                contentType:false,
                                scope: this,
                                params: {
                                    "file1":document.getElementById("fileInput").files[0],
                                    "file2":document.getElementById("fileInput").files[1]
                                },
                                success:...,
                                failure: function (response) {
                                    if (statusWin.isVisible()){
                                         statusWin.hide();
                                       }
                                       alert("Response Body :: "+response);
                                       alert("Response Status :: "+response.status);
                                }
                            });
                        }
               </script>
    </div>

Servlet类:

@SlingServlet(paths="/servletPath", methods = "POST", metatype=true)
public class SampleServlet extends SlingAllMethodsServlet {

    @Reference
    private ResourceResolverFactory resolverFactory;


    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response)
            throws IOException,ServletException {

        log.info("I am inside POST method of Sample Servlet");
        log.info("SlingHTTPRequest content type is :: "+request.getContentType());
        log.info("getParamter Attached file is :: "+request.getParameter("file1"));


        }
        boolean isMultipart = org.apache.commons.fileupload.servlet.ServletFileUpload.isMultipartContent(request);

        log.info("Is this a Multipart Request :: "+isMultipart);

    }

日志输出:

  • 我在Sample Servlet的POST方法中
  • SlingHTTPRequest内容类型为:: :: application / x-www-form-urlencoded; charset = UTF-8 getParamter
  • 附加文件为:: [目标文件]
  • 这是一个分段请求:: false

我尝试了以下方法,但是没有用

  • https://helpx.adobe.com/experience-manager/using/uploading-files-aem1.html->这使用jquery ajax调用来上传文件,但是chrome不支持它。因此无法继续。

  • 试图使用document.getElementById(“ fileInput”)。files [i]从表单中获取文件,然后将其附加到FormData()对象,然后将其发送。但是仍然不知道如何从请求对象中检索文件。

问题:

  • 即使我以Multipart的形式提交请求,isMultipart检查也总是在servlet的doPOST方法中为我返回false。

  • request.getParameter(String)返回一个字符串,我不确定如何检索文件对象

  • request.getParameter(“ file2”)返回 [目标文件] 。不确定如何从该文件中检索文件

我真的很困。任何帮助或任何方向将不胜感激。由于我的整个代码都在AEM上运行,因此无法使用断点进行调试。我只能获取日志输出:(

0 个答案:

没有答案