views:

168

answers:

3

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:&#160;
                        </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%;"/>&#160;&#160;
                    <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%;"/>&#160;&#160;
        </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?

+1  A: 

If you actually want to include this in your JSF page, then I suggest you to use JSTL c:import for this.

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<c:import url="http://narod.ru/disk/20957166000/test.txt.html" />

Much easier. This however only works if you're using JSF on JSP. This won't work with JSF on Facelets and it (unfortunately) also doesn't offer simliar facilities.

As to your actual problem: I have no idea since the described problem is caused outside the scope of the as far posted code information or you didn't run the code you expect it was running (restart the webserver to make sure that latest changes to Java code get compiled). At least the this.filename returned the incorrect value and I see that you've the URL of your own webpage outcommented. Maybe you changed this but the hotdeploy failed or the server wasn't restarted before testing.

Further I see that you're only printing every second line from BufferedReader and ignoring every first alternating line.

while (bufferedReader.readLine() != null) // You're ignoring first line.
{
   System.out.println("---- " + bufferedReader.readLine()); // You're only printing next line.

This ain't going to work. Assuming that you want the file in a single big String, then you should follow the following idiom to use BufferedReader#readLine() properly:

BufferedReader reader = null;
StringBuider builder = new StringBuilder();
try {
     reader = new BufferedReader(new InputStreamReader(someInputStream, "UTF-8"));
     for (String line = null; (line = reader.readLine()) != null;) {
         builder.append(line).append("\n"); // Append newline as well since readLine() eats them.
     }
} finally {
     if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {}
}
String content = builder.toString();
BalusC
Hi, BalusC! Thanks for quick answer!I will try your suggestion in a few minutes. Now I will try to explain what I need - I need a method, which will recieve the URL of some file (it could be txt file, xls, sql) as a parameter and I want to read information in that file and proccess it later.But instead of information in that file I receive the source code of the current page.
Elena
Yes, I already understood that. This just means that the URL is wrong. Check the value of `this.filename`. Or restart the server.
BalusC
A: 

(update moved into question)

Elena
Please do not post updates or questions as answers. Use the `edit` link. I've moved it into the question for you.
BalusC
A: 

(update/comment moved into question)

Elena
Please do not post updates or comments as answers. Use the `edit` link to add updates to the question. Use `add comment` link to add comments to answers.
BalusC