Java Jersey 2 分段上传客户端
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24637038/
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
Jersey 2 Multipart upload Client
提问by Nicklas2751
I want to write a simple jersey 2 client to upload a file. I'm using Jersey 2.10.1 and wrote following server code:
我想编写一个简单的 jersey 2 客户端来上传文件。我正在使用 Jersey 2.10.1 并编写了以下服务器代码:
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response uploadFile(
@FormDataParam("file") InputStream aUploadedInputStream,
@FormDataParam("file") FormDataContentDisposition aFileDetail) {
UploadedFile uploadedFile = new UploadedFile();
uploadedFile.setOriginalFileName(aFileDetail.getFileName());
uploadedFile.setFileSize(aFileDetail.getSize());
saveToFile(aUploadedInputStream, aFileDetail.getType(), uploadedFile);
databaseHelper.saveInDatabase(uploadedFile);
return Response.status(200).build();
}
("UploadedFile" is an custom class to save the information of the file in a database)
(“UploadedFile”是一个自定义类,用于将文件信息保存在数据库中)
And this is my client code:
这是我的客户端代码:
private static final String TARGET_URL = "http://localhost:49158/rest/service/upload";
public Slimclient() {
Client client = ClientBuilder.newBuilder()
.register(MultiPartFeature.class).build();
WebTarget webTarget = client.target(TARGET_URL);
MultiPart multiPart = new MultiPart();
FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("file",
new File("C:/Users/Nicklas2751/Desktop/test.txt"), MediaType.APPLICATION_OCTET_STREAM_TYPE);
multiPart.bodyPart(fileDataBodyPart);
Response response = webTarget.request(
MediaType.MULTIPART_FORM_DATA).post(
Entity.entity(multiPart, multiPart.getMediaType()));
System.out.println(response.getStatus()+" "+response.getStatusInfo()+" "+response);
}
public static void main(String[] args) {
new Slimclient();
}
The server code runs without any problems but when i run the client i get the following error:
服务器代码运行没有任何问题,但是当我运行客户端时,出现以下错误:
415 Unsupported Media Type InboundJaxrsResponse{ClientResponse{method=POST, uri=http://localhost:49158/rest/service/upload, status=415, reason=Unsupported Media Type}}
I searched the web for a good tutorial for jersey 2 and multipart fileupload but i can only find tutorials and examples for jersey 1 or with an HTML-Form as "Client". I hope sombody can help me :)
我在网上搜索了一个很好的关于球衣 2 和多部分文件上传的教程,但我只能找到球衣 1 或 HTML 表单作为“客户端”的教程和示例。我希望有人可以帮助我:)
采纳答案by Nicklas2751
I've found my problem. I've missed to set the MediaType
of the MultiPart
and with the .request(MediaType.MULTIPART_FORM_DATA)
I've set the expected MediaType
of the response to MULTIPART_FORM_DATA
. Here is the working code:
我找到了我的问题。我已经错过了设置MediaType
的MultiPart
,并与.request(MediaType.MULTIPART_FORM_DATA)
我所设定的预期MediaType
应对的MULTIPART_FORM_DATA
。这是工作代码:
public class Slimclient {
private static final String TARGET_URL = "http://localhost:49158/rest/service/upload";
public Slimclient() {
Client client = ClientBuilder.newBuilder()
.register(MultiPartFeature.class).build();
WebTarget webTarget = client.target(TARGET_URL);
MultiPart multiPart = new MultiPart();
multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("file",
new File("C:/Users/Nicklas/Desktop/aab.txt"),
MediaType.APPLICATION_OCTET_STREAM_TYPE);
multiPart.bodyPart(fileDataBodyPart);
Response response = webTarget.request(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.entity(multiPart, multiPart.getMediaType()));
System.out.println(response.getStatus() + " "
+ response.getStatusInfo() + " " + response);
}
public static void main(String[] args) {
new Slimclient();
}
}