java - Download file from REST service using JAX-RS client -


i trying download file rest service using jax-rs. code invokes download sending request:

private response invokedownload(string authtoken, string url) {     // creates http client object , makes http request specified url     client client = clientbuilder.newclient();     webtarget target = client.target(url);      // sets header , makes request     return target.request().header("x-tableau-auth", authtoken).get(); } 

however facing problems converting response actual file object. did following:

public file downloadworkbook(string authtoken, string siteid, string workbookid, string savepath)         throws ioexception {     string url = operation.download_workbook.geturl(siteid, workbookid);     response response = invokedownload(authtoken, url);      string output = response.readentity(string.class);     string filename;  // code retrieve filename headers     path path = files.write(paths.get(savepath + "/" + filename), output.getbytes());     file file = path.tofile();     return file; } 

the file created not valid, debugged code , noticed output contains string (much larger):

pk ͢�f���� �[ superstore.twb�ysi�7����ߡ���d�m3��f���

looks binary. there wrong code.

how http response body string response object?



edit: quote rest api reference http response:

response body

one of following, depending on format of workbook:

the workbook's content in .twb format (content-type: application/xml)
workbook's content in .twbx format (content-type: application/octet-stream)

as noticed yourself, you're dealing binary data here. shouldn't create string response. better input stream , pipe file.

response response = invokedownload(authtoken, url); inputstream in = response.readentity(inputstream.class); path path = paths.get(savepath, filename); files.copy(in, path); 

Comments