Java 使用 Jersey 上传文件:FormDataContentDisposition 为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23083583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
File upload with Jersey : FormDataContentDisposition is null
提问by user3249592
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 :
我正在尝试使用 Jersey 实现文件上传,所以我遵循了这个示例:http: //www.mkyong.com/webservices/jax-rs/file-upload-example-in-jersey/与 HTML 页面配合良好。现在我将它改编为我的应用程序,这里是代码:
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.
但是在调试视图中,一旦我上传了我的图片并按下按钮发送然后到达这里,uploadedInputStream 和 fileDetail 为空。所以我无能为力......我是Servlet的初学者,然后是REST,所以请放纵。
Thank you.
谢谢你。
采纳答案by user3249592
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")
我找到了它不起作用的原因:这是因为您在 FormDataParameter("myForm") 之后选择的名称必须与您在 HTML 表单中选择的名称相同 (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 :)
希望它能帮助像我这样的其他初学者:)