java - How can receive multiple files in InputStream and process it accordingly? -


i want receive multiple files uploaded client-side. uploaded multiple files , request server-side (java) using jax-rs(jersey).

i have following code,

@post     @consumes(mediatype.multipart_form_data)     public void upload(@context uriinfo uriinfo,             @formdataparam("file") final inputstream is,             @formdataparam("file") final formdatacontentdisposition detail) { fileoutputstream os = new fileoutputstream("path/to/save/" + appropriatefilename);     byte[] buffer = new byte[1024];             int length;             while ((length = is.read(buffer)) > 0) {                 os.write(buffer, 0, length);             } } 

how can write files separately in server side uploaded in client side.

for eg. uploaded files such my_file.txt, my_file.png, my_file.doc. need write same above my_file.txt, my_file.png, my_file.doc in server side.

how can achieve this?

you try this:

@post @consumes(mediatype.multipart_form_data) public void upload(formdatamultipart formparams) {     map<string, list<formdatabodypart>> fieldsbyname = formparams.getfields();      // each value in fieldsbyname list of length 1.     // assuming each field in form file, loop through them.      (list<formdatabodypart> fields : fieldsbyname.values())     {         (formdatabodypart field : fields)         {             inputstream = field.getentityas(inputstream.class);             string filename = field.getname();              // todo: save file here              // if want media type validation, it's field.getmediatype()         }     } } 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -