使用Jersey上传文件:FormDataContentDisposition为null [英] File upload with Jersey : FormDataContentDisposition is null
问题描述
我正在尝试使用Jersey实现文件上传,所以我按照这个例子:
http://www.mkyong.com/webservices/jax-rs/file-upload-example-in-jersey/
哪个适用于HTML页面。现在我将它改编为我的应用程序,这里是代码:
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();
}
但是使用调试视图,一旦我上传了我的图片并按下按钮发送和然后到这里,uploadedInputStream和fileDetail都为null。所以我无能为力......
我是Servlet的初学者,然后是REST,所以请放纵。
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.
谢谢。
推荐答案
我发现为什么它无法正常工作:
这是因为你在FormDataParameter之后选择的名字( myForm)必须与您在HTML表单中选择的名称相同(name =myForm)
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")
所以,
@FormDataParam("myForm") InputStream uploadedInputStream,
@FormDataParam("myform") FormDataContentDisposition fileDetail)
表格必须类似于
<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 :)
这篇关于使用Jersey上传文件:FormDataContentDisposition为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!