泽西岛2分段上传客户端

编程入门 行业动态 更新时间:2024-10-22 10:35:56
本文介绍了泽西岛2分段上传客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想写一个简单的球衣2客户端来上传一个文件。我使用的是泽西岛2.10.1并且写下面的服务器代码: $ $ p $ $ b $ @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是一个自定义类,用于保存文件的信息一个数据库)

这是我的客户端代码:

private static final String TARGET_URL =http:// localhost:49158 / rest / service / upload; $ b公共Slimclient(){客户端客户端= 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); 响应响应= 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(); $ / code>

服务器代码运行没有任何问题,但是当我运行客户端时,以下错误:

415不受支持的媒体类型InboundJaxrsResponse {ClientResponse {method = POST,uri = http:// localhost:49158 / rest / service / upload,status = 415,reason = Unsupported Media Type}}

一个很好的教程泽西2和multipart文件上传,但我只能找到教程和示例球衣1或HTML表单作为客户端。我希望sombody能帮助我:)解决方案

我发现我的问题。我错过了设置 MultiPart 的 MediaType 和 .request(MediaType .MULTIPART_FORM_DATA)我已经将预期的 MediaType 设置为 MULTIPART_FORM_DATA 。这里是工作代码: pre $ public class Slimclient { private static final String TARGET_URL =http:// localhost :49158 / REST /服务/上传; $ b公共Slimclient(){客户端客户端= 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); 响应响应= 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(); } }

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:

@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)

And this is my client code:

private static final String TARGET_URL = "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=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 :)

解决方案

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:

public class Slimclient { private static final String TARGET_URL = "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(); } }

更多推荐

泽西岛2分段上传客户端

本文发布于:2023-11-26 17:55:39,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:客户端   泽西   上传

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!