Should inputstream be closed explicitly when uploading file in jersey multipart? -
i use jersey multipart upload file in controller
here typical code case:
@path("/file") public class uploadfileservice { @post @path("/upload") @consumes(mediatype.multipart_form_data) public response uploadfile( @formdataparam("file") inputstream uploadedinputstream, @formdataparam("file") formdatacontentdisposition filedetail) { string uploadedfilelocation = "d://uploaded/" + filedetail.getfilename(); // save writetofile(uploadedinputstream, uploadedfilelocation); 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(); } } }
i have read many example code online, , inputstream not closed.
my question should close uploadedinputstream explicitly or manually? , why?
Comments
Post a Comment