Sending a file by form, but not by Postman

Asked

Viewed 490 times

1

I made a webservice with Java Jersey to send files, when I try to send files using an HTML form, it works, however, when I try to send from a client or in Postman, an error 500 is returned with nullpointer.

Uploadresource

@Path("/files")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public class UploadResource {

    @POST
    @Path("/upload")
    public Response uploadImagem(@FormDataParam("file") InputStream fileInputString, @FormDataParam("file") FormDataContentDisposition fileInputDetails) {

        String saveFolder = System.getProperty("user.home") + "/CantinaRest/usuario/produtos/img/";

        File dir = new File(saveFolder);
        dir.mkdirs();

        String fileLocation = saveFolder + fileInputDetails.getFileName();

        try {
            OutputStream out = new FileOutputStream(new File(fileLocation));
            byte[] buffer = new byte[1024];
            int bytes = 0;
            long file_size = 0;
            while ((bytes = fileInputString.read(buffer)) != -1) {
                out.write(buffer, 0, bytes);
                file_size += bytes;
            }
            out.flush();
            out.close();

            return new Response(1, "Imagem salva com sucesso: " + saveFolder);
        } catch (IOException ex) {
            ex.printStackTrace();
            return new Response(0, "Erro: " + ex.getMessage());
        }
    }
}

The error that is returned by Postman and Android client:

java.lang.NullPointerException
    br.com.mobileti.cantinarest.rest.UploadResource.uploadImagem(UploadResource.java:42)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:76)
    org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:147)
    org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:190)
    org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:243)
    org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:102)
    org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:493)
    org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:415)
    org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:104)
    org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:277)
    org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:289)
    org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:256)
    org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:703)
    org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:416)
    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:370)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:389)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:342)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:229)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Postman:

inserir a descrição da imagem aqui

As is giving the same error in Postman and Android, so I believe it is not problem with Android, but with the webservice. the Error in line String fileLocation = saveFolder + fileInputDetails.getFileName();

1 answer

0


I found the problem, the code is ok... I forgot to put the Formdataparam "file" in the key of the Postman

Browser other questions tagged

You are not signed in. Login or sign up in order to post.