如何将上传的图像文件写入url位置

时间:2017-01-05 10:23:15

标签: java rest java-io

我是rest web services的新手。我需要将图像文件写入删除位置,即安全的URL(https)。我有java.io.InputStream的图像文件输入。以下是我粗略/错误的代码:

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 "";
    }

无需遵循上述代码......因为我知道它的错误..只是提供建议,如果有的话......

请提供一些支持。我被卡住了。

0 个答案:

没有答案