我使用Camel-http4,因为我只需要一个制作人,而不是公开网络服务。我只需要将文件推送到HTTP服务器,因此这是一个客户端实现。我面临的问题是尝试发布返回错误。
CamelHttpResponseCode = 403,CamelHttpResponseText =禁止
{"时间戳":" 2018-05-03T20:36:20.571"" traceId":"",& #34;路径":" [POST] https://apm-query-svc-prod.apm-api.com/v2/t_series/upload%2520?throwExceptionOnFailure=false","错误":[{" httpStatusCode":" FORBIDDEN& #34;,"代码":" FORBIDDEN"," message":"无法解析多部分servlet请求;嵌套异常是java.io.IOException:org.apache.tomcat.util.http.fileupload.FileUploadException:请求被拒绝,因为没有找到多部分边界"," detail":null,&# 34;的requestId":空}]}
问题,有没有办法用Camel-HTTP4做到这一点?怎么做/可以这样做?谢谢!!!
这是路线
<route
id="core.pr.upload.route"
autoStartup="false" >
<from uri="{{uploadEntranceEndpoint}}" />
<process ref="customerEntitesProcessor" />
<process ref="customerTokenProcessor" />
<process ref="uploadProcessor" />
<setHeader headerName="CamelHttpUri">
<simple>${header.UPLOADURL}?throwExceptionOnFailure=false</simple>
</setHeader>
<setHeader headerName="CamelHttpMethod">
<constant>POST</constant>
</setHeader>
<to uri="http4://apm-query-svc-prod.app-api.aws-usw02-pr.io:443/v2/t_series/upload?throwExceptionOnFailure=false" />
<log message="After POSTING FILE: ${body}" loggingLevel="INFO"/>
<to uri="{{afteruploadLocation}}" />
</route>
这是类,我只需设置标题,并使用Exchange.HTTP_URI提供的URI覆盖URI。
公共类UploadProcessor实现Processor {
private static final Logger LOG = LoggerFactory.getLogger(UploadProcessor.class);
@Override
public void process(Exchange exchange) throws Exception {
LOG.info("Entering Upload Processor ..." );
final String tenant = (String)exchange.getIn().getHeader("TENANT");
final String token = (String)exchange.getIn().getHeader("TOKEN");
final String uploadUrl = (String)exchange.getIn().getHeader("UPLOADURL");
final String custKey = (String)exchange.getIn().getHeader("CUSTKEY");
LOG.info("TENANT: " + tenant);
LOG.info("TOKEN: " + token);
LOG.info("UPLOADURL: " + uploadUrl);
LOG.info("CUSTKEY: " + custKey);
LOG.info("Uploading File for CustKey: " + custKey + " and Tenant: " + tenant);
StringBuilder authHeader = new StringBuilder("Bearer ");
authHeader.append(token);
LOG.info("Authorization: " + authHeader.toString());
exchange.setProperty("CamelCharsetName", "UTF-8"); //"CamelCharsetName" Exchange.CHARSET_NAME
exchange.getIn().setHeader("CamelHttpCharacterEncoding", "UTF-8"); //"CamelHttpCharacterEncoding" Exchange.HTTP_CHARACTER_ENCODING
exchange.getIn().setHeader("CamelAcceptContentType", "application/json"); //"CamelAcceptContentType" Exchange.ACCEPT_CONTENT_TYPE
exchange.getIn().setHeader("CamelHttpUri", uploadUrl); //"CamelHttpUri" Exchange.HTTP_URI
exchange.getIn().setHeader("CamelHttpMethod", "POST"); //"CamelHttpMethod" Exchange.HTTP_METHOD
exchange.getIn().setHeader("x-ge-csvformat", "ODB");
exchange.getIn().setHeader("Tenant", tenant);
exchange.getIn().setHeader("Content-Type", "multipart/form-data"); //"Content-Type" ; boundary=
exchange.getIn().setHeader("Authorization", authHeader.toString());
LOG.info("Exiting Upload Processor: Finish " );
}
}
这是我的第一个HTTP介绍,不熟悉多部分边界,我希望有一种简单的解决方案。 :)谢谢!!!
解决方案根本不困难,这是最终根据拉尔夫的答案。谢谢大家!困难的部分是找到导入包并且它具有依赖性。 添加到POM
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.1</version>
</dependency>
import java.lang.StringBuilder;
import java.net.URLEncoder;
import java.util.Base64;
import java.io.File;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.camel.LoggingLevel;
import org.apache.http.HttpEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
public class UploadProcessor implements Processor {
private static final Logger LOG = LoggerFactory.getLogger(UploadProcessor.class);
@Override
public void process(Exchange exchange) throws Exception {
LOG.info("Entering Upload Processor ..." );
final String tenant = (String)exchange.getIn().getHeader("TENANT");
final String token = (String)exchange.getIn().getHeader("TOKEN");
final String uploadUrl = (String)exchange.getIn().getHeader("UPLOADURL");
final String custKey = (String)exchange.getIn().getHeader("CUSTKEY");
LOG.info("TENANT: " + tenant);
LOG.info("TOKEN: " + token);
LOG.info("UPLOADURL: " + uploadUrl);
LOG.info("CUSTKEY: " + custKey);
LOG.info("Uploading File for CustKey: " + custKey + " and Tenant: " + tenant);
StringBuilder authHeader = new StringBuilder("Bearer ");
authHeader.append(token);
LOG.info("Authorization: " + authHeader.toString());
exchange.setProperty("CamelCharsetName", "UTF-8"); //"CamelCharsetName" Exchange.CHARSET_NAME
exchange.getIn().setHeader("CamelHttpCharacterEncoding", "UTF-8"); //"CamelHttpCharacterEncoding" Exchange.HTTP_CHARACTER_ENCODING
exchange.getIn().setHeader("CamelAcceptContentType", "application/json"); //"CamelAcceptContentType" Exchange.ACCEPT_CONTENT_TYPE
exchange.getIn().setHeader("CamelHttpUri", uploadUrl); //"CamelHttpUri" Exchange.HTTP_URI
exchange.getIn().setHeader("CamelHttpMethod", "POST"); //"CamelHttpMethod" Exchange.HTTP_METHOD
exchange.getIn().setHeader("x-ge-csvformat", "ODB");
exchange.getIn().setHeader("Tenant", tenant);
exchange.getIn().setHeader("Authorization", authHeader.toString());
// Process the file in the exchange body
File file = exchange.getIn().getBody(File.class);
String fileName = (String) exchange.getIn().getHeader(Exchange.FILE_NAME);
LOG.info("fileName: " + fileName);
MultipartEntityBuilder entity = MultipartEntityBuilder.create();
entity.addBinaryBody("file", file);
entity.addTextBody("name", fileName);
exchange.getIn().setBody(entity.build());
LOG.info("Exiting Upload Processor: Finish " );
}
}