通过 URL - java/mule 从磁盘读取文件
Read file from disk through URL - java/mule
我有一个 mule 程序,我想从我的 PC 读取文件并在本地主机上的浏览器中显示内容。
我让它为一个硬编码的文件工作,如下所示。
public class ReadFile extends AbstractMessageTransformer {
/**
* loads the content of the file specified in the parameter
*
* @param filename
* the name of the file
* @return the content of the file
*/
public String readFile(String filename) {
File file;
file = new File("O:\test.txt");
StringBuilder builder = new StringBuilder();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String line = null;
while ((line = reader.readLine()) != null)
builder.append(line);
} catch (IOException e) {
e.printStackTrace();
} finally {
closeQuietly(reader);
}
return builder.toString();
}
public String getFileName(MuleMessage message) {
Path p = Paths.get("O:\test.txt");
String file = p.getFileName().toString();
return file;
}
public String setPayload(MuleMessage message, String outputEncoding) {
message.setPayload(outputEncoding);
return null;
}
private void closeQuietly(Closeable c) {
if (c != null) {
try {
c.close();
} catch (IOException ignored) {
}
}
}
@Override
public Object transformMessage(MuleMessage message, String outputEncoding)
throws TransformerException {
String filename = getFileName(message);
String content = readFile(filename);
setPayload(message, content);
return message;
}
}
我如何指定它读取我通过 URL 输入的任何文件,而不仅仅是我的硬编码文件?
您可以为此使用 HttpServer class。您的应用程序将侦听 HTTP 请求。因此,您可以在浏览器中输入以下URL:
"localhost/file_to_be_loaded"
您的 HttpServer 将收到 URL (localhost/file_to_be_loaded)
的 GET 请求
解析请求,加载文件并响应浏览器。
看似复杂,其实很简单。下面 post 中的第一个答案几乎完成了所有工作:
祝你好运!
为此你需要使用Mule表达式语言。按照以下步骤操作
1) 在 HTTP 之后使用下面的会话组件。这会将来自 HTTP url 的文件路径存储在会话变量中。
<set-session-variable variableName="Filepath" value="#[message.inboundProperties.'http.query.params'.Filepath]" doc:name="Session Variable"/>
2) 在您的 java 代码中,使用以下代码行读取会话变量。然后将文件路径传递给你的硬编码方法
String filePath = eventContext.getMessage().getProperty("Filepath", PropertyScope.SESSION);
3) 最后使用查询参数
触发服务,如下面的 HTTP url
http://localhost:8083/?Filepath=C:\test.txt
我建议,阅读 Mule 表达式语言的 mule 文档。这将更清楚地说明如何处理此类场景。
我的Xml代码
<flow name="filetestFlow1">
<http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
<logger message="--- Service triggred --" level="INFO" doc:name="Logger"/>
<set-session-variable variableName="Filepath" value="#[message.inboundProperties.'http.query.params'.Filepath]" doc:name="Session Variable"/>
<component class="filetest.ReadFile" doc:name="Java"/>
</flow>
Java 代码,在这里你可以观察到我实现了 Callable 接口
public class ReadFile implements Callable {
@Override
public Object onCall(MuleEventContext eventContext) throws Exception {
// TODO Auto-generated method stub
System.out.println("Service triggred in java");
String filePath = eventContext.getMessage().getProperty("Filepath", PropertyScope.SESSION);
MuleMessage msg = eventContext.getMessage();
String filename = getFileName(msg,filePath);
String content = readFile(filename,filePath);
setPayload(msg, content);
return msg;
}
/**
* loads the content of the file specified in the parameter
*
* @param filename
* the name of the file
* @return the content of the file
*/
public String readFile(String filename,String filePath) {
File file;
file = new File(filePath);
StringBuilder builder = new StringBuilder();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String line = null;
while ((line = reader.readLine()) != null)
builder.append(line);
} catch (IOException e) {
e.printStackTrace();
} finally {
closeQuietly(reader);
}
return builder.toString();
}
public String getFileName(MuleMessage message,String filePath) {
Path p = Paths.get(filePath);
String file = p.getFileName().toString();
return file;
}
public String setPayload(MuleMessage message, String outputEncoding) {
message.setPayload(outputEncoding);
//String payload1 = "#[ReadFile]";
return null;
}
private void closeQuietly(Closeable c) {
if (c != null) {
try {
c.close();
} catch (IOException ignored) {
}
}
}
}
我有一个 mule 程序,我想从我的 PC 读取文件并在本地主机上的浏览器中显示内容。
我让它为一个硬编码的文件工作,如下所示。
public class ReadFile extends AbstractMessageTransformer {
/**
* loads the content of the file specified in the parameter
*
* @param filename
* the name of the file
* @return the content of the file
*/
public String readFile(String filename) {
File file;
file = new File("O:\test.txt");
StringBuilder builder = new StringBuilder();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String line = null;
while ((line = reader.readLine()) != null)
builder.append(line);
} catch (IOException e) {
e.printStackTrace();
} finally {
closeQuietly(reader);
}
return builder.toString();
}
public String getFileName(MuleMessage message) {
Path p = Paths.get("O:\test.txt");
String file = p.getFileName().toString();
return file;
}
public String setPayload(MuleMessage message, String outputEncoding) {
message.setPayload(outputEncoding);
return null;
}
private void closeQuietly(Closeable c) {
if (c != null) {
try {
c.close();
} catch (IOException ignored) {
}
}
}
@Override
public Object transformMessage(MuleMessage message, String outputEncoding)
throws TransformerException {
String filename = getFileName(message);
String content = readFile(filename);
setPayload(message, content);
return message;
}
}
我如何指定它读取我通过 URL 输入的任何文件,而不仅仅是我的硬编码文件?
您可以为此使用 HttpServer class。您的应用程序将侦听 HTTP 请求。因此,您可以在浏览器中输入以下URL:
"localhost/file_to_be_loaded"
您的 HttpServer 将收到 URL (localhost/file_to_be_loaded)
的 GET 请求解析请求,加载文件并响应浏览器。
看似复杂,其实很简单。下面 post 中的第一个答案几乎完成了所有工作:
祝你好运!
为此你需要使用Mule表达式语言。按照以下步骤操作
1) 在 HTTP 之后使用下面的会话组件。这会将来自 HTTP url 的文件路径存储在会话变量中。
<set-session-variable variableName="Filepath" value="#[message.inboundProperties.'http.query.params'.Filepath]" doc:name="Session Variable"/>
2) 在您的 java 代码中,使用以下代码行读取会话变量。然后将文件路径传递给你的硬编码方法
String filePath = eventContext.getMessage().getProperty("Filepath", PropertyScope.SESSION);
3) 最后使用查询参数
触发服务,如下面的 HTTP urlhttp://localhost:8083/?Filepath=C:\test.txt
我建议,阅读 Mule 表达式语言的 mule 文档。这将更清楚地说明如何处理此类场景。
我的Xml代码
<flow name="filetestFlow1">
<http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
<logger message="--- Service triggred --" level="INFO" doc:name="Logger"/>
<set-session-variable variableName="Filepath" value="#[message.inboundProperties.'http.query.params'.Filepath]" doc:name="Session Variable"/>
<component class="filetest.ReadFile" doc:name="Java"/>
</flow>
Java 代码,在这里你可以观察到我实现了 Callable 接口
public class ReadFile implements Callable {
@Override
public Object onCall(MuleEventContext eventContext) throws Exception {
// TODO Auto-generated method stub
System.out.println("Service triggred in java");
String filePath = eventContext.getMessage().getProperty("Filepath", PropertyScope.SESSION);
MuleMessage msg = eventContext.getMessage();
String filename = getFileName(msg,filePath);
String content = readFile(filename,filePath);
setPayload(msg, content);
return msg;
}
/**
* loads the content of the file specified in the parameter
*
* @param filename
* the name of the file
* @return the content of the file
*/
public String readFile(String filename,String filePath) {
File file;
file = new File(filePath);
StringBuilder builder = new StringBuilder();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String line = null;
while ((line = reader.readLine()) != null)
builder.append(line);
} catch (IOException e) {
e.printStackTrace();
} finally {
closeQuietly(reader);
}
return builder.toString();
}
public String getFileName(MuleMessage message,String filePath) {
Path p = Paths.get(filePath);
String file = p.getFileName().toString();
return file;
}
public String setPayload(MuleMessage message, String outputEncoding) {
message.setPayload(outputEncoding);
//String payload1 = "#[ReadFile]";
return null;
}
private void closeQuietly(Closeable c) {
if (c != null) {
try {
c.close();
} catch (IOException ignored) {
}
}
}
}