java - Send file inside JSONObject to REST WebService -


i want send file webservice, need send more informations, want send them json. when put file inside jsonobject error saying isn't string. question is, should take file , convert string, put inside json , on web service take , convert string file? or there simple way?

here code:

client:

private void send() throws jsonexception{     clientconfig config = new defaultclientconfig();     client client = client.create(config);     client.addfilter(new loggingfilter());     webresource service = client.resource("http://localhost:8080/proj/rest/file/upload_json");      jsonobject my_data = new jsonobject();     file file_upload = new file("c:/hi.txt");     my_data.put("user", "beth");     my_data.put("date", "22-07-2013");     my_data.put("file", file_upload);      clientresponse client_response = service.accept(mediatype.application_json).post(clientresponse.class, my_data);      system.out.println("status: "+client_response.getstatus());      client.destroy(); } 

webservice

@post @path("/upload_json")  @consumes(mediatype.application_json) @produces("text/plain")  public string receive(jsonobject json) throws jsonexception {      //here i'll save file , make antoher things..     return "ok"; } 

after answers, here code - everyone:

webservice

import java.io.file; import java.io.fileoutputstream; import java.io.ioexception; import javax.ws.rs.consumes; import javax.ws.rs.post; import javax.ws.rs.path; import javax.ws.rs.produces; import javax.ws.rs.core.mediatype; import org.codehaus.jettison.json.jsonexception; import org.codehaus.jettison.json.jsonobject; import com.sun.jersey.core.util.base64;  @path("/file") public class receivejsonwebservice {      @post     @path("/upload_json")      @consumes(mediatype.application_json)     @produces(mediatype.application_json)      public jsonobject receivejson(jsonobject json) throws jsonexception, ioexception {         convertfile(json.getstring("file"), json.getstring("file_name"));         //prints json object         return json;     }      //convert base64 string , create file     private void convertfile(string file_string, string file_name) throws ioexception{         byte[] bytes = base64.decode(file_string);         file file = new file("local_path/"+file_name);         fileoutputstream fop = new fileoutputstream(file);         fop.write(bytes);         fop.flush();         fop.close();     } } 

client

import java.io.file; import java.io.ioexception; import java.nio.file.files; import javax.ws.rs.core.mediatype; import org.codehaus.jettison.json.jsonexception; import org.codehaus.jettison.json.jsonobject; import com.sun.jersey.api.client.client; import com.sun.jersey.api.client.clientresponse; import com.sun.jersey.api.client.webresource; import com.sun.jersey.api.client.config.clientconfig; import com.sun.jersey.api.client.config.defaultclientconfig; import com.sun.jersey.api.client.filter.loggingfilter; import com.sun.jersey.core.util.base64; import com.sun.jersey.multipart.formdatamultipart; import com.sun.jersey.multipart.file.filedatabodypart; import com.sun.jersey.multipart.impl.multipartwriter;  public class myclient {       public static void main(string[] args) throws jsonexception, ioexception      {         myclient my_client = new myclient();         file file_upload = new file("local_file/file_name.pdf");         my_client.sendfilejson(file_upload);     }       private void sendfilejson(file file_upload) throws jsonexception, ioexception{          clientconfig config = new defaultclientconfig();         client client = client.create(config);         client.addfilter(new loggingfilter());         webresource service = client.resource("my_rest_address_path");         jsonobject data_file = new jsonobject();         data_file.put("file_name", file_upload.getname());         data_file.put("description", "something file....");         data_file.put("file", convertfiletostring(file_upload));          clientresponse client_response = service.accept(mediatype.application_json).post(clientresponse.class, data_file);          system.out.println("status: "+client_response.getstatus());          client.destroy();      }       //convert file base64 string     private string convertfiletostring(file file) throws ioexception{         byte[] bytes = files.readallbytes(file.topath());            return new string(base64.encode(bytes));     }  } 

you should convert file data base64 encoding , transfer it, e.g. this:

byte[] bytes = files.readallbytes(file_upload.topath()); dados.put("file", datatypeconverter.printbase64binary(bytes)); 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -