tôi đã thiết kế một dịch vụ Jersey REST của nhiều phần dữ liệu như dưới đây để nhận được yêu cầu nhiều phần dữ liệu (file upload) và lưu các tập tin trong một vị trí đĩa:Jersey Multipart Khách hàng Tải lên
@POST
@Path("/Upload")
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public String uploadFile(@FormDataParam("file") InputStream inputStream,
@FormDataParam("file") FormDataContentDisposition contentDisposition) {
System.out.println("Method Entry");
System.out.println(contentDisposition.getFileName());
String result = "not Success";
File file = null;
if (contentDisposition != null
&& contentDisposition.getFileName() != null
&& contentDisposition.getFileName().trim().length() > 0) {
try {
file = new File("xx"
+ contentDisposition.getFileName());
new File("yy").mkdirs();
file.createNewFile();
OutputStream outputStream = new FileOutputStream(file);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
outputStream.flush();
outputStream.close();
result = "success";
} catch (Exception e) {
System.out.println(e.toString());
}
}
System.out.println("Method Exit");
return result;
}
và khách hàng thử nghiệm của tôi là:
Client client = Client.create();
WebResource resource = client
.resource("xyz");
String conString = "This is the content";
FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
formDataMultiPart.field("file", "Testing.txt");
FormDataBodyPart bodyPart = new FormDataBodyPart("file",
new ByteArrayInputStream(conString.getBytes()),
MediaType.APPLICATION_OCTET_STREAM_TYPE);
formDataMultiPart.bodyPart(bodyPart);
String reString = resource.type(MediaType.MULTIPART_FORM_DATA)
.accept(MediaType.TEXT_HTML)
.post(String.class, formDataMultiPart);
System.out.println(reString);
Nhưng tôi không thể nhận phản hồi.
Nó hoạt động hoàn hảo khi tôi đang sử dụng trang web HTML làm ứng dụng khách tải lên các tệp bằng cách gọi dịch vụ REST nhưng từ máy khách REST nó không hoạt động.
Có điều gì phải được thay đổi trong ứng dụng khách không?
bất cứ ai có thể giúp tôi trong việc thêm tiêu đề mới cho khách hàng kiểm tra trên mã? – cxyz