I am new to rest
web services
. I needed to write a image file to a remove location i.e. a url which is secure(https). I have a input of a image file as of java.io.InputStream
. Below is my rough/incorrect code:
private String writeToFile2(InputStream uploadedInputStream) throws IOException{
URL url = new URL("https://[my_url]/api/v1/clients/67/images");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.connect();
/*List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("firstParam", paramValue1));
params.add(new BasicNameValuePair("secondParam", paramValue2));
params.add(new BasicNameValuePair("thirdParam", paramValue3));*/
try {
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
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();
}
return "";
}
no need to follow the above code... as I know its wrong.. just provide suggestions if any..
please provide some support on this. I am stuck.