[英]Read file from disk through URL - java/mule
我有一個m子程序,我想從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類。 您的應用程序將偵聽HTTP請求。 因此,您可以在瀏覽器中輸入以下URL:
“本地主機/ file_to_be_loaded”
您的HttpServer將收到一個URL的GET請求(localhost / file_to_be_loaded)
解析請求,加載文件並響應瀏覽器。
它可能看起來很復雜,但是非常簡單。 以下帖子中的第一個答案幾乎可以完成所有工作:
僅使用Java SE API的Java中的簡單HTTP服務器
祝好運!
為此,您需要使用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文檔。 這將使如何處理這種情況更加清晰。
我的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) {
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.