Hi all! It's me again) I have another problem. I want to load file (for example - txt) from web. I tried to use the next code in my managed bean:
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;
}
But the BufferedReader returns the html code of the current page, where i am trying to call managed bean's method. It's very strange thing - I have googled and none have had this problem.
Maybe I do something wrong, maybe there us a simplest way to load file into web (jsf) app not using net API. Any ideas?
Thanks very much for help!
Update:
Maybe some jsf code will be useful:
<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>
Update 2: I take another file from web (not localhost) - and everything works fine! Sorry for beying crazy about this)))
As BalusC said, my app just didnt find file by URL: http://localhost:8080/sfront/files/test.txt.
I dont know why I could not use local files.
Any ideas?