Accessing uploaded file in Apache Tapestry page

扶醉桌前 提交于 2020-01-05 03:06:07

问题


I'm using Apache Tapestry v5.3.7 and I already use the normal Tapestry upload component in a form. For a better user experience I try now to integrate Dropzone.js in a normal Tapestry page without any form. The JavaScript integration works fine. The uploaded file data are transferred to my server with a post request and I can access the request with all of its parameters.

My question is now how can I access the binary data of the uploaded file (maybe as InputStream) to save them in my system? I already injected the http request but getInputStream returns a empty stream.

Thanks for any suggestions

/** Code snippet of page java part */
...

@Inject
protected HttpServletRequest _request;

public void onActivate (String rowId) {
    String fileName=_request.getParameter("file");
    try {
        InputStream is=_request.getInputStream();
        // if I do read from is it returns -1
        // :-(
        doSomeSaveStuff(is); // dummy code
    }
    catch(Exception e) {
        e.printStackTrace();
    }
}

...

回答1:


Taha has a blog post here on his Tapestry Magic blog he integrates a different file uploader library with Tapestry. I'm guessing your answer will be there.




回答2:


Here's one way to do it:

In template:

<t:form t:id="testForm" class="dropzone"> </t:form>

In page.java

  @Inject
  MultipartDecoder multipartDecoder;

  @Component(id = "testForm")
  private Form testForm;

  @Inject
  RequestGlobals requestGlobals;

void onSubmitFromTestForm() throws ManagerException {
    System.out.println("test form invoked");
    HttpServletRequest r = requestGlobals.getHTTPServletRequest();
    UploadedFile u = multipartDecoder.getFileUpload("file");

The uploaded file contains what you uploaded and you can work with it the way you want.

Note: the HttpServletRequest::getParameterMap() , told me that the handle to to the file is called file which is how I know that passing file to getFileUpload makes the decoder correctly parse the multipart/post



来源:https://stackoverflow.com/questions/26015378/accessing-uploaded-file-in-apache-tapestry-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!