0

I have a rest class which upload an image to a folder. By the time I try to test it using postman, my rest class does not fire.

Here is my rest class -

package uploadRest;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;

@Path("/file")
public class UploadFileService {

    @POST
    @Path("/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadFile(
            @FormDataParam("file") InputStream uploadedInputStream,
            @FormDataParam("file") FormDataContentDisposition fileDetail) {
try {
    System.out.println("Hi ");
    String uploadedFileLocation = "d://uploaded/" + fileDetail.getFileName();

    // save it
    writeToFile(uploadedInputStream, uploadedFileLocation);

    String output = "File uploaded to : " + uploadedFileLocation;

    return Response.status(200).entity(output).build();
}
catch (Exception e){
    e.printStackTrace();
}

        return null;
    }

    // save uploaded file to new location
    private void writeToFile(InputStream uploadedInputStream,
                             String uploadedFileLocation) {

        try {
            OutputStream out = new FileOutputStream(new File(
                    uploadedFileLocation));
            int read = 0;
            byte[] bytes = new byte[1024];

            out = new FileOutputStream(new File(uploadedFileLocation));
            while ((read = uploadedInputStream.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            out.flush();
            out.close();
        } catch (IOException e) {

            e.printStackTrace();
        }

    }

}

I try to test it via postman but it doesn't print "Hi" which is in System.out.println()

Here is what is printed in console: -

cache-control: no-cache
Postman-Token: 35942cde-5bcb-487d-896e-b772e9430e55
Content-Type: multipart/form-data
User-Agent: PostmanRuntime/6.1.6
Accept: */*
accept-encoding: gzip, deflate
Connection: keep-alive
Transfer-Encoding: chunked


]] Root cause of ServletException.
java.lang.NullPointerException
    at com.sun.jersey.multipart.impl.MultiPartReaderClientSide.unquoteMediaTypeParameters(MultiPartReaderClientSide.java:245)
    at com.sun.jersey.multipart.impl.MultiPartReaderClientSide.readMultiPart(MultiPartReaderClientSide.java:172)
    at com.sun.jersey.multipart.impl.MultiPartReaderServerSide.readMultiPart(MultiPartReaderServerSide.java:80)
    at com.sun.jersey.multipart.impl.MultiPartReaderClientSide.readFrom(MultiPartReaderClientSide.java:158)
    at com.sun.jersey.multipart.impl.MultiPartReaderClientSide.readFrom(MultiPartReaderClientSide.java:85)
    Truncated. see log file for complete stacktrace
> 

This is how I am sending request using postman -

This is postman screenshot

Vikas Sachdeva
  • 5,633
  • 2
  • 17
  • 26
AFF
  • 1,515
  • 4
  • 21
  • 35
  • [Good exceptions are excellent search keywords](https://www.google.com/search?q=java.lang.NullPointerException+at+com.sun.jersey.multipart.impl.MultiPartReaderClientSide.unquoteMediaTypeParameters&oq=java.lang.NullPointerException+at+com.sun.jersey.multipart.impl.MultiPartReaderClientSide.unquoteMediaTypeParameters). – BalusC Aug 23 '17 at 07:29
  • Exception states that request is not correct. Most probably request body is not valid multipart request – Vikas Sachdeva Aug 23 '17 at 14:35
  • @Vikas Sachdeva how can I test it? I am using postman for uploading an image. I set Content-Type= multipart/form-data in header request – AFF Aug 27 '17 at 08:39
  • 1
    I think setting `Content-Type= multipart/form-data` in header is problem. While uploading file using postman, you should not specify `Content-Type= multipart/form-data` header. postman will do it by itself. – Vikas Sachdeva Aug 27 '17 at 13:34
  • @Vikas Sachdeva by the time I remove Content-Type= multipart/form-data from header, I get this message in postman:Unsupported Media Type – AFF Aug 28 '17 at 04:28
  • 1
    @NGS can you add postman screenshots. FYI, check this answer - https://stackoverflow.com/questions/16015548/tool-for-sending-multipart-form-data-request#41435972 . It also states that content-type header should not be set while uploading any file. So, I am wondering is there any other problem. – Vikas Sachdeva Aug 28 '17 at 05:37
  • @Vikas Sachdeva please check postman screenshot link – AFF Aug 28 '17 at 07:15

1 Answers1

0

problem was solved by sending data as form-data instead of binary. I changed the method of sending data in postman body from binary to form-data and the problem was solved. Thanks @Vikas Sachdeva because of sharing helpful link :) Tool for sending multipart/form-data request

AFF
  • 1,515
  • 4
  • 21
  • 35