泽西岛2分段上传客户端 [英] Jersey 2 Multipart upload Client

查看:155
本文介绍了泽西岛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 = "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 :)

解决方案

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 = "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();
    }
}

这篇关于泽西岛2分段上传客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆