PARAMファイルとしてjava.net.HttpURLConnectionの使用(バイナリストリームとして)POSTファイル= <ファイルのバイナリストリーム>

デヴァルジャイナ教:

私はjava.net.HttpURLConnectionの使用してエンドポイントにファイルを(POST)をアップロードしようとしていますが、私はHTTPコード400(不正な要求)を得続けます。

私はを参照のアンドロイドAPI 23にHttpURLConnectionの付いたファイルとパラメータにサーバーを送ります

しかし、問題は、私は、リクエストボディのparam(ファイル=)としてこのファイルを送信する必要があるということです。

注:私はメモリに完全にそれを読んでいますので、ファイルは小さいサイズのみ(4〜5メガバイト)のものであろう。

カール要求を対応する次のとおりです。

カール-X POST "API" -H "のContent-Type:マルチパート/フォームデータ" -F "ファイル="


私が使っていることをコードの抜粋:

    Proxy webproxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(" 
                                <proxy host>", <proxy_port>));
    HttpURLConnection http_conn = (HttpURLConnection) 
                                     url.openConnection(webproxy);
    String authorization = getAuthorization(access_token);
    http_conn.setRequestMethod("POST");
    http_conn.setRequestProperty("Accept-Charset", "UTF-8");        
    http_conn.setRequestProperty("Authorization", authorization);
    http_conn.setRequestProperty("Connection", "Keep-Alive");
    http_conn.setRequestProperty("Content-Type", "multipart/form-data);  
    http_conn.setDoOutput(true);
    http_conn.setDoInput(true);
    DataOutputStream outputStream;
    outputStream = new DataOutputStream(http_conn.getOutputStream());
    File file_obj = new File(this.file);                                
    byte[] allBytes = new byte[(int) file_obj.length()];
    FileInputStream fileInputStream = new FileInputStream(file_obj);                
    outputStream.write("file=".getBytes("UTF-8")); <---Trying to add file param here
    fileInputStream.read(allBytes);
    outputStream.write(allBytes);

私はコードの一部(異なるGET要求に対して罰金を作品)の下に使用して応答を読むことポスト:

    InputStream inputStream = http_conn.getInputStream();            
        BufferedReader bufferedReader = new BufferedReader(new 
    InputStreamReader(inputStream));
    String line = "";            
    while ((line = bufferedReader.readLine()) != null) {
        data = data + line;                
    }

注:私はめったにので、あなたの応答に記述してくださいそれに非常に精通していない午前のJavaを使用していません。

デヴァルジャイナ教:

私は別のパッケージ(okhttp3)に切り替えるように、上記のdidntは私のために働いた、ここに私のために働いていたものです:

    File file_obj = new File(this.file); 
    String authorization = "my authorization string";
    Proxy webproxy = new Proxy(Proxy.Type.HTTP, new 
          InetSocketAddress("proxy", <port>));

    OkHttpClient client = new OkHttpClient.Builder().proxy(webproxy).build();      

    RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("file", "filename",
        RequestBody.create(MediaType.parse("application/octet-stream"), file_obj)).build();

    Request request = new Request.Builder().header("Authorization", authorization).url(this.url).post(requestBody).build();

    try (Response response = client.newCall(request).execute()){
        if(!response.isSuccessful()) return "NA";
        return (response.body().string());
    }

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=227606&siteId=1