java - How to get size of file in rest api jersey -
i have made rest api in working fine want read size of file , have used below code 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 got size of file -1 .
can 1 suggest me how can read actual size of file .
but using
system.out.println(" data name ="+ filepart.getcontentdisposition().getfilename());
i got correct name of file .
hope wanted. have verified in system. prints size of 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 writetofile(uploadedinputstream, uploadedfilelocation); file file = new file(uploadedfilelocation); system.out.println(file.length() + " in bytes"); string output = "file uploaded : " + uploadedfilelocation; return response.status(200).entity(output).build(); } // save uploaded file 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 have following dependency in pom.xml
<dependency> <groupid>org.glassfish.jersey.media</groupid> <artifactid>jersey-media-multipart</artifactid> <version>2.13</version> </dependency>
also add application class extends resource config. registers class jersey having multipart content.
super(yourclass.class, multipartfeature.class);
Comments
Post a Comment