File upload with Jersey : FormDataContentDisposition is null

ε祈祈猫儿з 提交于 2019-12-06 01:39:57

问题


I'm trying to implement file upload with Jersey so I followed this example : http://www.mkyong.com/webservices/jax-rs/file-upload-example-in-jersey/ which worked well with an HTML page. Now I adapted it to my application, here is code :

  public Response uploadFile(
    @FormDataParam("file") InputStream uploadedInputStream,
    @FormDataParam("file") FormDataContentDisposition fileDetail)
    throws IOException {

Response.Status respStatus = Response.Status.OK;

if (fileDetail == null) {
    respStatus = Response.Status.INTERNAL_SERVER_ERROR;
} else {
    try {
    initPath();
    if (fileDetail.getSize() > OntoWebStudioUtil
        .getUploadFileLimit()) {
        respStatus = Response.Status.NOT_ACCEPTABLE;
        return Response.status(respStatus).build();
    }

    writeToFile(uploadedInputStream, tempDirectory);
    } catch (Exception e) {
    respStatus = Response.Status.INTERNAL_SERVER_ERROR;
    e.printStackTrace();
    }
}
return Response.status(respStatus).build();
}

But with debug view, once I uploaded my picture and pushed the button send and then get here, uploadedInputStream and fileDetail are null. So I can do nothing... I am a beginner with Servlet and then REST, so please be indulgent.

Thank you.


回答1:


I found why it wasn't working : It is because the name you choose after the FormDataParameter("myForm") has to be the same as the name you choosed in your HTML form (name = "myForm")

So,

@FormDataParam("myForm") InputStream uploadedInputStream,
@FormDataParam("myform") FormDataContentDisposition fileDetail)

And the form has to be like

    <form action=".../rest/fileupload" method="post" enctype="multipart/form-data">
   <p>
    Select a file : <input type="file" name="myForm"/>
   </p>
   <input type="submit" value="Upload It" />
</form>

Hope it will help some other beginners like me :)



来源:https://stackoverflow.com/questions/23083583/file-upload-with-jersey-formdatacontentdisposition-is-null

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