gpt4 book ai didi

java - Jersey 2 分段上传客户端

转载 作者:搜寻专家 更新时间:2023-10-30 20:58:48 24 4
gpt4 key购买 nike

我想写一个简单的 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”是一个自定义类,用于将文件信息保存在数据库中)

这是我的客户端代码:

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}}

我在网上搜索了 jersey 2 和 multipart fileupload 的教程,但我只能找到 jersey 1 的教程和示例,或者使用 HTML 表单作为“客户端”。我希望有人能帮助我 :)

最佳答案

我发现了我的问题。我错过了设置 MultiPartMediaType.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();
}
}

关于java - Jersey 2 分段上传客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24637038/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com