Upload de arquivos consumindo serviço Rest com Jersey + GlassFish
Olá,
Criei u serviço REST para upload de arquivos utilizando Jersey. Estou utilizando GlassFish 4.1.
Fiz uma página .html para teste e o upload ocorre com sucesso para qualquer tipo de arquivo.
No entanto, não estou conseguindo fazer o upload consumindo o serviço via client JAVA.
Segue o código do Server:
Segue o código do Client:
Segue o código da página HTML que consigo realizar updload de qualquer arquivo:
Segue o erro que ocorre quando executo o Cliente java:
Exception in thread "main" javax.ws.rs.ProcessingException: org/glassfish/jersey/message/MessageUtils
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:255)
at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:683)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:424)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:679)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:435)
at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:338)
at processamento.ClientTeste.main(ClientTeste.java:47)
Criei u serviço REST para upload de arquivos utilizando Jersey. Estou utilizando GlassFish 4.1.
Fiz uma página .html para teste e o upload ocorre com sucesso para qualquer tipo de arquivo.
No entanto, não estou conseguindo fazer o upload consumindo o serviço via client JAVA.
Segue o código do Server:
@POST
@Path("upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public String uploadFile(
@FormDataParam("file") InputStream fileInputStream,
@FormDataParam("file") FormDataContentDisposition fileDisposition)
throws FileNotFoundException, IOException {
String fileName = fileDisposition.getFileName();
System.out.println("***** fileName " + fileDisposition.getFileName());
String filePath = BASE_PATH + fileName;
try (OutputStream fileOutputStream = new FileOutputStream(filePath)) {
int read = 0;
final byte[] bytes = new byte[1024];
while ((read = fileInputStream.read(bytes)) != -1) {
fileOutputStream.write(bytes, 0, read);
}
}
return "File Upload Successfully !!";
}Segue o código do Client:
final String TARGET_URL = "http://sat:8084/CIAPEx/ciap/files/upload/";
Client client = ClientBuilder.newBuilder().register(MultiPartFeature.class).build();
WebTarget webTarget = client.target(TARGET_URL);
File file = new File("C://Temp//CIAPFull.zip");
FileInputStream inputStream = new FileInputStream(file);
//set up multi part form.
FormDataMultiPart part = new FormDataMultiPart().field("file", inputStream, MediaType.MULTIPART_FORM_DATA_TYPE);
part.getField("file").setContentDisposition(FormDataContentDisposition.name("file").fileName(file.getName()).build());
Response response = webTarget.request().accept(MediaType.TEXT_PLAIN).post(Entity.entity(part, MediaType.MULTIPART_FORM_DATA), Response.class);
Segue o código da página HTML que consigo realizar updload de qualquer arquivo:
<html>
<body>
<form method="post"
enctype="multipart/form-data"
action="http://sat:8084/CIAPEx/ciap/files/upload">
File<input type="file" name="file">
<input type="submit" value="submit">
</form>
</body>
</html>
Segue o erro que ocorre quando executo o Cliente java:
Exception in thread "main" javax.ws.rs.ProcessingException: org/glassfish/jersey/message/MessageUtils
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:255)
at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:683)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:424)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:679)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:435)
at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:338)
at processamento.ClientTeste.main(ClientTeste.java:47)
Auto Pratense
Curtidas 0