通过URL从磁盘读取文件 - java / mule

时间:2015-10-05 12:06:15

标签: java mule

我有一个mule程序,我想从我的电脑上读取一个文件,并在浏览器的localhost上显示内容。

我让它为一个硬编码的文件工作,如下所示。

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输入的任何文件,而不仅仅是我的硬编码文件?

3 个答案:

答案 0 :(得分:0)

您可以使用HttpServer类。您的应用程序将侦听HTTP请求。因此,您可以在浏览器中输入以下URL:

"本地主机/ file_to_be_loaded"

您的HttpServer将收到URL的GET请求(localhost / file_to_be_loaded)

解析请求,加载文件并回复浏览器。

看起来很复杂,但很简单。以下帖子中的第一个答案几乎完成了所有工作:

simple HTTP server in Java using only Java SE API

祝你好运!

答案 1 :(得分:0)

要实现这一点,您需要使用Mule表达式语言。请按照以下步骤进行操作

1)在HTTP之后使用以下会话组件。这将把来自HTTP URL的filePath存储在会话变量中。

 <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文档。这将更清楚地说明如何使用此类方案。

答案 2 :(得分:0)

我的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) {
        }
    }
}

}