這是我)我有另一個問題。我想從web加載文件(例如 - txt)。 我嘗試使用下面的代碼在我管理的Bean:如何從互聯網加載txt文件到我的jsf應用程序?
public void run()
{
try
{
URL url = new URL(this.filename);
URLConnection connection = url.openConnection();
bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
if (bufferedReader == null)
{
return;
}
String str = bufferedReader.readLine();
while (bufferedReader.readLine() != null)
{
System.out.println("---- " + bufferedReader.readLine());
}
}
catch(MalformedURLException mue)
{
System.out.println("MalformedURLException in run() method");
mue.printStackTrace();
}
catch(IOException ioe)
{
System.out.println("IOException in run() method");
ioe.printStackTrace();
}
finally
{
try
{
bufferedReader.close();
}
catch(IOException ioe)
{
System.out.println("UOException wile closing BufferedReader");
ioe.printStackTrace();
}
}
}
public String doFileUpdate()
{
String str = FacesContext.getCurrentInstance().getExternalContext().getRequestServletPath();
System.out.println("111111111111111111111 str = " + str);
str = "http://narod.ru/disk/20957166000/test.txt.html";//"http://localhost:8080/sfront/files/test.html";
System.out.println("222222222222222222222 str = " + str);
FileUpdater fileUpdater = new FileUpdater(str);
fileUpdater.run();
return null;
}
但BufferedReader中返回當前頁面,在這裏我試圖調用託管bean的方法的HTML代碼。 這是非常奇怪的事情 - 我用Google搜索,沒有一個有這個問題。
也許我做錯了什麼,也許我們有一個最簡單的方法來加載文件到web(jsf)應用程序不使用net API。有任何想法嗎?
非常感謝您的幫助!
更新:
也許有些JSF代碼將是有益的:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:fc="http://www.fusioncharts.com"
xmlns:t="http://myfaces.apache.org/tomahawk"
template="template.xhtml">
<ui:define name="title">Add company page</ui:define>
<ui:define name="content">
<h:form id="addCompanyForm">
<h:outputText value="Add a new company"/><br/><br/><br/>
<div style="width: 400px;">
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td width="1">
Company name:
</td>
<td>
<h:inputText id="companyName" value="#{companyBean.companyName}" style="width: 100%;" required="true" />
</td>
</tr>
<tr>
<td valign="top" nowrap="nowrap">
Company description: 
</td>
<td>
<h:inputTextarea value="#{companyBean.companyDescription}" style="width: 100%;"/>
</td>
</tr>
</table><br/>
<center>
<h:commandButton value="Save company" action="#{companyBean.doInsertCompany}" style="width: 40%;"/>  
<a4j:commandButton ajaxSingle="true" value="Clear" actionListener="#{companyBean.doClear}" style="width: 40%;" reRender="addCompanyForm"/>
</center>
<br/><br/><br/>
<h:commandLink value="Return to companies page" action="companies" immediate="true" />
</div>
</h:form>
<h:form>
<br/>
<h:commandButton value="File Update" action="#{companyBean.doFileUpdate}" style="width: 10%;"/>  
</h:form>
</ui:define>
</ui:composition>
更新2:我從網上另一個文件(不是localhost) - 和一切工作正常! ))
由於BalusC說,我的應用程序只是沒有找到文件的URL:http://localhost:8080/sfront/files/test.txt。
我不知道爲什麼我不能使用本地文件。
任何想法?
你需要一些代碼格式... – mosg 2010-05-20 13:08:21
我格式化一些代碼。 @Elena:請在編輯信息時注意右欄中的格式化規則,並利用頁面底部的預覽部分來檢查一切是否正常。 – BalusC 2010-05-20 13:19:54
感謝您的幫助))) 您的代碼是什麼格式的?在我的瀏覽器(Opera 10.53)中,一切看起來都不錯。對於源代碼,我做了4個空格(甚至更多)。你能告訴我,什麼是錯的,我該如何解決這個問題? – Elena 2010-05-20 13:35:14