这是我)我有另一个问题。我想从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