泽西岛 2 分段上传客户端

我想写一个简单的球衣2客户端来上传文件。我正在使用泽西岛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();
}

(“UploadFile”是一个自定义类,用于将文件的信息保存在数据库中)

这是我的客户端代码:

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

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

415 Unsupported Media Type InboundJaxrsResponse{ClientResponse{method=POST, uri=http://localhost:49158/rest/service/upload, status=415, reason=Unsupported Media Type}}

我在网上搜索了球衣2和多部分文件上传的好教程,但我只能找到球衣1的教程和示例,或者使用HTML表单作为“客户端”。我希望sombody可以帮助我:)


答案 1

我发现了我的问题。我错过了设置的 和 我已经设置了响应的预期 。以下是工作代码:MediaTypeMultiPart.request(MediaType.MULTIPART_FORM_DATA)MediaTypeMULTIPART_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();
    }
}

答案 2

由于服务器端使用 ,因此客户端代码可以直接使用 而不是 。MediaType.MULTIPART_FORM_DATAFormDataMultiPartMultiPart

FormDataMultiPart multiPart = new FormDataMultiPart();

推荐