0

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.

JPG
  • 1,247
  • 5
  • 31
  • 64
  • What do you have on the server side to receive the stream and write it to a file? – Yash Capoor Jan 05 '17 at 10:27
  • its an image file uploaded and converted into InputStream – JPG Jan 05 '17 at 10:29
  • You can check this similar discussion [http://stackoverflow.com/questions/17173435/send-image-file-using-java-http-post-connections](http://stackoverflow.com/questions/17173435/send-image-file-using-java-http-post-connections) – Noman Khan Jan 05 '17 at 10:32

0 Answers0