Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions java/org/webservice/fotolia/FotoliaApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,49 @@ public void downloadMedia(final String download_url, final String output_file) t
if (output_file != null) {
stream.close();
}
}

/**
* Download a media and get the InputStream with its binary
*
* @param download_url URL as returned by getMedia()
*/
public InputStream getMediaInputStream(final String download_url) throws FileNotFoundException, IOException, FotoliaApiException
{
DefaultHttpClient client;
HttpResponse response;
StatusLine statusLine;
HttpEntity entity;
JSONObject obj;
InputStream inputStream = null;
String error_msg;
int error_code;

client = this._getHttpClient(true);
response = client.execute(new HttpGet(download_url));

statusLine = response.getStatusLine();
entity = response.getEntity();
if (statusLine.getStatusCode() != 200) {
if (entity == null) {
throw new FotoliaApiException(statusLine.getStatusCode(),
statusLine.getReasonPhrase());
} else {
obj = (JSONObject) JSONValue.parse(EntityUtils.toString(entity));
error_msg = (String) obj.get("error");
if (obj.get("code") != null) {
error_code = Integer.parseInt((String) obj.get("code"));
} else {
error_code = statusLine.getStatusCode();
}

throw new FotoliaApiException(error_code, error_msg);
}

}

inputStream = new ByteArrayInputStream(EntityUtils.toByteArray(entity));
return inputStream;
}

/**
Expand Down