5

I have made a rest api in this it is working fine but i want to read size of file and i have used below code to read size of file

@POST
@Path("/test")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response upload( FormDataMultiPart form ){
    System.out.println(" size of file="+ filePart.getContentDisposition().getSize());
}

but i got the size of file -1 .

Can any one suggest me how can i read actual size of file .

But using

System.out.println(" data name ="+ filePart.getContentDisposition().getFileName());

i got correct name of file .

soufrk
  • 825
  • 1
  • 10
  • 24
Anuj Dhiman
  • 1,550
  • 3
  • 26
  • 51
  • Inject `HttpServletRequest` in your method using `@Context HttpServletRequest request` and try `request.getContentLength()`. – cassiomolin Apr 25 '16 at 08:05

3 Answers3

2

First of all, I think the best way to restrict data size is setting on the server configuration. Please see the related ref.

Secondly, since it comes with the Stream, it goes til to meet EOF or similar one. That means you cannot find the size at the first place.

Third, however, one of the alternative way for judging size in jersey is using the contents size of the HTTP header. The server gets 'Content-Length' header at the first place to know the size of the body. @Context HttpServletRequest request has request.getContentLength() method. But since it has multiparts, you need to be careful that the body size has sum of the data with protocol overheads (seperator/contents infos)

Good Luck

Kwang-Chun Kang
  • 351
  • 3
  • 12
1

Hope this is what you wanted. I have verified it in my system. This prints the size of the file in bytes.

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail) {

    String uploadedFileLocation = "/home/Desktop/" + fileDetail.getFileName();
    // save it
    writeToFile(uploadedInputStream, uploadedFileLocation);
    File file = new File(uploadedFileLocation);
    System.out.println(file.length() + " in bytes");
    String output = "File uploaded to : " + uploadedFileLocation;
    return Response.status(200).entity(output).build();
}

// 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();
    }
}

make sure you have the following dependency in your pom.xml

<dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>2.13</version>
</dependency>

also add this to your application class which extends resource config. This registers your class with jersey as having multipart content.

super(YourClass.class, MultiPartFeature.class);
Abhishek
  • 2,485
  • 2
  • 18
  • 25
0

Try using HttpServletRequest . Kindly see the docs for more details.

   @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response putFile(@Context HttpServletRequest request){
      Part part = request.getPart("filename");
      long fileSize = part.getSize();
    }
mubeen
  • 813
  • 2
  • 18
  • 39