如何将以下CURL更改为JAVA中的REST Client?

问题描述

我的卷曲命令是

curl -X POST“ https:// myTestServer / knox / api / v1 / files” -H“ accept:application / json” -H “授权:基本YTM1MjM0ODpQYXNzd29yZEAzNDU =” -H“内容类型:multipart / form-data” -F “ file=@test.pdf; type = application / pdf” -F“ entity = {” name“:” PDF.22427922.02“,” size“: 160135,“ organization”:“ TEST”}“

代码为


import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.FileEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;

public class Main_test {
    public static void main(String Args[]) throws ClientProtocolException,IOException,KeyManagementException,NoSuchAlgorithmException,KeyStoreException {
      
        whenSendMultipartRequestUsingHttpClient_thenCorrect();
    }
    
    public static void whenSendMultipartRequestUsingHttpClient_thenCorrect() 
            throws ClientProtocolException,KeyStoreException,KeyManagementException {
        
        
                SSLContextBuilder builder = new SSLContextBuilder();
                builder.loadTrustMaterial(null,new TrustSelfSignedStrategy());
                SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                        builder.build());
                CloseableHttpClient client = HttpClients.custom().setSSLSocketFactory(
                        sslsf).build();
        
        
        
               HttpPost httppost = new HttpPost("https://myTestServer/knox/api/v1/files");
               httppost.addHeader("accept","application/json");
               httppost.addHeader("authorization","Basic YTM1MjM0ODpQYXNzd29yZEAzNDU=");
               httppost.setHeader("Content-type","multipart/form-data");

               HttpEntity entity = MultipartEntityBuilder.create()
                       .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
                       .addBinaryBody("file",new File("C:\\users\\6576788\\JLink_Apps\\test.pdf"),ContentType.create("application/pdf"),"test.pdf")
                       .addTextBody("entity","'name': 'PDF.20890607.02','size': 160135,'organization': 'TEST'}").build();
               

              httppost.setEntity(entity);
              

              CloseableHttpResponse response = client.execute(httppost);
              
              BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
               String response1 = reader.readLine();
               System.out.println("response:"+response1);
            System.out.println("response:"+response.toString());
              client.close();
          }

}

````````````````````````````````````````````````````````````````````````````````

Throws
response:{"message":"Cannot retrieve part of request","errorCode":"BAD_REQUEST"}
response:HttpResponseProxy{HTTP/1.1 400 Bad Request [Date: Mon,21 Sep 2020 10:35:15 GMT,Expires: 0,Cache-Control: no-cache,no-store,max-age=0,must-revalidate,X-XSS-Protection: 1; mode=block,Pragma: no-cache,X-Frame-Options: DENY,Strict-Transport-Security: max-age=31536000 ; includeSubDomains,X-Content-Type-Options: nosniff,Content-Type: application/json,Content-Length: 71,Connection: close] ResponseEntityProxy{[Content-Type: application/json,Chunked: false]}}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...