java Apache Camel multipart HTTP post(文件上传)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2415708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Apache Camel multipart HTTP post (file upload)
提问by Kai Sternad
How can I do multipart file uploads using the Apache Camel HTTP component ?
如何使用 Apache Camel HTTP 组件进行分段文件上传?
采纳答案by Henryk Konsek
I don't know is it possible to send multipart forms using the HTTP component.
我不知道是否可以使用 HTTP 组件发送多部分表单。
If you need the workaround, you can create POJO Spring Bean that uses the Apache Http Client (and its MultipartPostMethod). Then you can route your message to that bean:
如果您需要解决方法,您可以创建使用 Apache Http 客户端(及其MultipartPostMethod)的POJO Spring Bean 。然后你可以将你的消息路由到那个 bean:
from("activemq:uploadQueue").to("bean:myApacheHttpClientBean?method=sendMultiPart")
回答by pegli
As long as your message body is in multipart/form-data format, you can use the Camel http component to POST it to another server. The trick is to set your Content-Type properly and set the request method to be POST:
只要您的消息正文是 multipart/form-data 格式,您就可以使用 Camel http 组件将其 POST 到另一台服务器。诀窍是正确设置您的 Content-Type 并将请求方法设置为 POST:
<route>
<from uri="direct:start"/>
<setBody>
<![CDATA[
--__MyCoolBoundary__
Content-Disposition: form-data; name="name"
Paul Mietz Egli
--__MyCoolBoundary__
Content-Disposition: form-data; name="email"
[email protected]
--__MyCoolBoundary__--
]]>
</setBody>
<setHeader headerName="Content-Type">
<constant>multipart/form-data; boundary="__MyCoolBoundary__"</constant>
</setHeader>
<setHeader headerName="CamelHttpMethod">
<constant>POST</constant>
</setHeader>
<to uri="http://www.example.com/mywebservice.php"/>
</route>
Obviously, the example body above isn't that useful because it's all static data. There are a number of ways you can construct the body -- I've used XSLT outputting in text mode, a scripted expression (e.g. <groovy>...</groovy>), and a Spring bean. XSLT works well when your incoming message body is already an XML document:
显然,上面的示例正文没有那么有用,因为它都是静态数据。有多种方法可以构造主体——我使用了文本模式下的 XSLT 输出、脚本表达式(例如 <groovy>...</groovy>)和 Spring bean。当您的传入消息正文已经是 XML 文档时,XSLT 运行良好:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
--__MyCoolBoundary__
Content-Disposition: form-data; name="name"
<xsl:value-of select="//name"/>
--__MyCoolBoundary__--
</xsl:stylesheet>
You do need to be careful about extra whitespace, however. Hope this helps!
但是,您确实需要小心额外的空格。希望这可以帮助!
回答by Aliti
I had working on a web project by below features:
我曾通过以下功能从事网络项目:
Login Form: people login and can upload the file; (Camel: Jetty, Http, JDBC)
Upload Form; upload servlet: if people can login; can upload xml file to ftp or web server; (Camel: file)
登录表单:人们登录并可以上传文件;(骆驼:Jetty、Http、JDBC)
上传表格;上传servlet:如果人们可以登录;可以上传xml文件到ftp或web服务器;(骆驼:文件)
3.File is validated by my .xsd file; (Camel: Validator)
3.文件由我的.xsd文件验证;(骆驼:验证器)
- File is checked by my .xsl schema file; (Camel: XSLT)
- 文件由我的 .xsl 架构文件检查;(骆驼:XSLT)
I was create web project by my favorite IDE (IntelliJ IDEA by Jetbrains); I describe part of my scenario with source code and hope this is useful ?
我使用我最喜欢的 IDE(Jetbrains 的IntelliJ IDEA )创建了 web 项目;我用源代码描述了我的部分场景,希望这有用吗?
1) index.html
1) index.html
<form action="http://0.0.0.0:8080/hello" method="post">
<fieldset title="Login" >
username:<input type="text" id="user" name="user"/>
password:<input type="password" id="pass" name="pass" />
<input type="submit" id="submit" value="submit"/>
</fieldset>
First you have to create database and login table; then add some sample data; for example add these files:
首先你必须创建数据库和登录表;然后添加一些示例数据;例如添加这些文件:
2) schema.sql
2)schema.sql
DROP TABLE IF EXISTS CONTACT;
CREATE TABLE CONTACT (
ID INT NOT NULL AUTO_INCREMENT
, NAME VARCHAR(40) NOT NULL
, USERNAME VARCHAR(40) NOT NULL
, PASSWORD VARCHAR(60) NOT NULL
, VERSION INT NOT NULL DEFAULT 0
, UNIQUE UQ_CONTACT_1 (USERNAME)
, PRIMARY KEY (ID));
3) test-data.sql
3) 测试数据.sql
insert into contact (name, username, password) values ('ali', 'aliti', '123');
insert into contact (name, username, password) values ('shahab', 'shahab', '147');
insert into contact (name, username, password) values ('farhad', 'farhad', '159');
4) config spring-context.xml
4) 配置 spring-context.xml
Then, you can use embedded databases like derby, H2, mysql or others. Add below config to your spring config file:
然后,您可以使用嵌入式数据库,如 derby、H2、mysql 或其他。将以下配置添加到您的 spring 配置文件中:
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:schema.sql"/>
<jdbc:script location="classpath:test-data.sql"/>
</jdbc:embedded-database>
5) camel-context.xml
5) 骆驼上下文.xml
Now, you can run your project; before do that you have to add this route to your camel context:
现在,您可以运行您的项目了;在此之前,您必须将此路线添加到您的骆驼上下文中:
<route>
<from uri="jetty:http://0.0.0.0:8080/hello"/>
<setBody>
<simple>
select * from contact where USERNAME = '${in.header.user}' and PASSWORD = '${in.header.pass}'
</simple>
</setBody>
<to uri="jdbc:dataSource"/>
<process ref="loginProcessor"/>
<log message=">>>header: ${in.header.name}"/>
<choice>
<when>
<simple>${in.header.name} == null</simple>
<to uri="jetty://http://localhost:9090/fail.html?bridgeEndpoint=true"/>
</when>
<otherwise>
<to uri="jetty://http://localhost:9090/file.html?bridgeEndpoint=true"/>
</otherwise>
</choice>
When you run our project; index.html page was shown and you can put the username and password text boxes and send your form.
当您运行我们的项目时;显示了 index.html 页面,您可以输入用户名和密码文本框并发送表单。
Actually Camel was listening to this jetty port and got your post information. You can get these information by Camel's header like '${in.header.user}'.
实际上,Camel 正在侦听此码头端口并获取您的帖子信息。您可以通过 Camel 的标头(如“${in.header.user}”)获取这些信息。
As you can see, I set my select query in Camel's Body, Thus the select result is also store in Camel's Body. I want to read my result and got some decisions, for this reason I add Camel processor as below:
如您所见,我在 Camel's Body 中设置了我的选择查询,因此选择结果也存储在 Camel's Body 中。我想阅读我的结果并做出一些决定,因此我添加了 Camel 处理器,如下所示:
6) LoginProcessor.java
6) 登录处理器.java
public class LoginProcessor implements Processor {
public void process(Exchange exchange) throws Exception {
int size = ((ArrayList) exchange.getIn().getBody()).size();
if (size > 0) {
Object name = ((LinkedHashMap) (((ArrayList) exchange.getIn().getBody()).get(0))).get("NAME");
System.out.println("welcome user: " + name);
exchange.getOut().setHeader("name",name);
} else {
System.out.println("user or pass are invalid. ");
exchange.getOut().setHeader("name",null);
}
}
}
}
In LoginProcessor I checked the body and if input username and password are valid; Add Camel's header property and named by ‘name' by field name of table. Otherwise set null value in Camel's header property.
在 LoginProcessor 中,我检查了正文以及输入的用户名和密码是否有效;添加 Camel 的 header 属性并按表的字段名称命名为 'name'。否则在 Camel 的 header 属性中设置空值。
Back to Camel context xml file and continue the route. If Camel's header is null; redirect user to fail.html page; otherwise redirect to page that get file from user(file.html).
返回 Camel 上下文 xml 文件并继续路由。如果 Camel 的 header 为 null;将用户重定向到 fail.html 页面;否则重定向到从用户(file.html)获取文件的页面。
Note: bridgeEndpoint property You are setting the http endpoint to be bridgeEndpoint which means the request url will be updated with request URI.
注意:bridgeEndpoint 属性您将 http 端点设置为 bridgeEndpoint,这意味着请求 url 将使用请求 URI 进行更新。
回答by Archer
Could you please provide more details how do you want multipart form reach apache camel?
您能否提供更多详细信息,您希望多部分表单如何到达 apache 骆驼?
Should it be some form on a webpage that send directly to Camel route? Or AMQ queue? I'd suggest you checking Apache HTTPand Apache Jettycomponents.
它应该是直接发送到 Camel 路由的网页上的某种形式吗?还是AMQ队列?我建议您检查Apache HTTP和Apache Jetty组件。
回答by JoseK
Does it have to be using Camel?
它必须使用骆驼吗?
Apache Fileupload does this quite simply http://commons.apache.org/fileupload/using.html
Apache Fileupload 这样做非常简单http://commons.apache.org/fileupload/using.html

