Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PUT请求Java将文本文件上载到Swagger_Java_Rest_Swagger_Httpurlconnection_Apache Httpclient 4.x - Fatal编程技术网

使用PUT请求Java将文本文件上载到Swagger

使用PUT请求Java将文本文件上载到Swagger,java,rest,swagger,httpurlconnection,apache-httpclient-4.x,Java,Rest,Swagger,Httpurlconnection,Apache Httpclient 4.x,我使用ApacheHttpConnection将带有PUT请求的文本文件发送到Swagger上的服务器主机。似乎我遗漏了一些东西,因为作为响应,我得到了“内部服务器错误”。 客户端代码: String url = "http://xxx.xxx.xx.xx:XXXX/warehouse-ui/api/v2/external-file/IMPORT_RATIO?businessUnit=MBA&sourceSystem=AVALOQ_MBA&effectiveFrom=20

我使用ApacheHttpConnection将带有PUT请求的文本文件发送到Swagger上的服务器主机。似乎我遗漏了一些东西,因为作为响应,我得到了“内部服务器错误”。 客户端代码:

    String url = "http://xxx.xxx.xx.xx:XXXX/warehouse-ui/api/v2/external-file/IMPORT_RATIO?businessUnit=MBA&sourceSystem=AVALOQ_MBA&effectiveFrom=2011-12-21&effectiveTo=MAX&threshold=WARNING";       
    String encoding = Base64.getEncoder().encodeToString((usrPassPair).getBytes());

    InputStream is = new FileInputStream(myLocalFile);
    HttpPut request = new HttpPut(url);
    HttpEntity entity = MultipartEntityBuilder.create().addBinaryBody("test", is, ContentType.create("multipart/form-data"), "asd.txt").build();
    request.setHeader("Authorization", "Basic " + encoding);
    request.setEntity(entity);

    HttpClient client = HttpClientBuilder.create().build();
    HttpResponse response = client.execute(request);
    int code = response.getStatusLine().getStatusCode()
服务器站点上的代码(招摇过市):

@Path(“/{descriptor}”)
@放
@使用({MediaType.MULTIPART\u FORM\u DATA})
@ApiOperation(position=1,value=“根据给定的描述符在指定的生效日期上载文件并传输数据。传输是同步完成的。”)
@生成(“应用程序/json”)
公共响应上载文件(
@QueryParam(“businessUnit”)@ApiParam(required=true)字符串businessUnitName,
@QueryParam(“sourceSystem”)@ApiParam(required=true,value=“源系统”)字符串sourceSystemName,
@PathParam(“描述符”)@ApiParam(required=true,value=“文件的描述符”)字符串描述符,
@QueryParam(“effectiveFrom”)@ApiParam(required=true,value=“ISO格式的日期,例如2011-12-21”)字符串effectiveFromString,
@QueryParam(“effectiveTo”)@ApiParam(value=“ISO格式的日期”或“MAX”。例如,2011-12-21”,defaultValue=“MAX”)字符串effectiveToString,
@ApiParam(value=“file to upload”,required=true)@FormDataParam(“file”)InputStream InputStream,
@ApiParam(value=“file detail”,required=true)@FormDataParam(“file”)formdatacontentdispositionfiledetail,
@QueryParam(“阈值”)@ApiParam(必需=true,value=“记录阈值”,allowableValues=“消息,通知,警告,错误”,defaultValue=“警告”)严重性阈值)
抛出IOException、Persistence.Exception、BusinessException、ConfigurationException{
ExternalFile ExternalFile=new VirtualExternalFile(fileDetail.getFileName(),ByteStreams.toByteArray(inputStream));
TaskExecutionJob TaskExecutionJob=prepareTransfer(businessUnitName、sourceSystemName、descriptor、,
EndPointHelper.getEffectivePeriodFrom(effectiveFromString,effectiveToString),外部文件,阈值);
返回EndPointHelper.createResponseFor(taskExecutionJob.executeSynchronous());

是否有我未包含的参数导致了此错误?

我想如果有人遇到类似问题,我会发布我的工作答案,也许有人更了解REST体系结构,可以解释为什么需要这样做:

    URIBuilder builder = new URIBuilder()
            .setScheme("http")
            .setHost("localhost:9090")
            .setPath("warehouse-ui/api/v2/external-file/"+context.get("descriptor").toString())
            .setParameter("businessUnit", context.get("businessUnit").toString())
            .setParameter("sourceSystem", context.get("sourceSystem").toString())
            .setParameter("effectiveFrom", context.get("effectiveFrom").toString())
            .setParameter("effectiveTo", context.get("effectiveTo").toString())
            .setParameter("threshold", context.get("threshold").toString());

    String encoding = Base64.getEncoder().encodeToString((usernamePasswordPair).getBytes());
    File isF = new File(pathToFile);
    FileInputStream fis = null;
    fis = new FileInputStream(isF);

    HttpPut request = new HttpPut(builder.toString());
    HttpEntity entity = MultipartEntityBuilder.create().addPart("file", new InputStreamBody(fis, isF.getName())).build();
    request.setHeader("Authorization", "Basic " + encoding);
    request.setEntity(entity);
    HttpClient client = HttpClientBuilder.create().build();
    HttpResponse response = client.execute(request);
    int code = response.getStatusLine().getStatusCode();
    System.out.println(code);
    System.out.println(response.getStatusLine().getReasonPhrase());
这是从Talend tJava组件粘贴的代码,所以在代码中您可能需要处理异常

    URIBuilder builder = new URIBuilder()
            .setScheme("http")
            .setHost("localhost:9090")
            .setPath("warehouse-ui/api/v2/external-file/"+context.get("descriptor").toString())
            .setParameter("businessUnit", context.get("businessUnit").toString())
            .setParameter("sourceSystem", context.get("sourceSystem").toString())
            .setParameter("effectiveFrom", context.get("effectiveFrom").toString())
            .setParameter("effectiveTo", context.get("effectiveTo").toString())
            .setParameter("threshold", context.get("threshold").toString());

    String encoding = Base64.getEncoder().encodeToString((usernamePasswordPair).getBytes());
    File isF = new File(pathToFile);
    FileInputStream fis = null;
    fis = new FileInputStream(isF);

    HttpPut request = new HttpPut(builder.toString());
    HttpEntity entity = MultipartEntityBuilder.create().addPart("file", new InputStreamBody(fis, isF.getName())).build();
    request.setHeader("Authorization", "Basic " + encoding);
    request.setEntity(entity);
    HttpClient client = HttpClientBuilder.create().build();
    HttpResponse response = client.execute(request);
    int code = response.getStatusLine().getStatusCode();
    System.out.println(code);
    System.out.println(response.getStatusLine().getReasonPhrase());