2010-05-20 28 views
1

這是我)我有另一個問題。我想從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:&#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> 

更新2:我從網上另一個文件(不是localhost) - 和一切工作正常! ))

由於BalusC說,我的應用程序只是沒有找到文件的URL:http://localhost:8080/sfront/files/test.txt

我不知道爲什麼我不能使用本地文件。

任何想法?

+0

你需要一些代碼格式... – mosg 2010-05-20 13:08:21

+0

我格式化一些代碼。 @Elena:請在編輯信息時注意右欄中的格式化規則,並利用頁面底部的預覽部分來檢查一切是否正常。 – BalusC 2010-05-20 13:19:54

+0

感謝您的幫助))) 您的代碼是什麼格式的?在我的瀏覽器(Opera 10.53)中,一切看起來都不錯。對於源代碼,我做了4個空格(甚至更多)。你能告訴我,什麼是錯的,我該如何解決這個問題? – Elena 2010-05-20 13:35:14

回答

1

如果你真的想把它包含在你的JSF頁面中,那麼我建議你使用JSTLc:import這個。

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

更容易。但是,只有在JSP上使用JSF時纔有效。這不適用於Facelets上的JSF,它(不幸)也不提供simliar設施。

至於你的實際問題:我不知道,因爲所描述的問題是在遠遠發佈的代碼信息的範圍之外引起的,或者你沒有運行你期望它運行的代碼(重新啓動web服務器以確保對Java代碼的最新更改進行編譯)。至少this.filename返回了不正確的值,我發現自己的網頁的網址未被評論。也許你改變了這一點,但hotdeploy失敗或服務器在測試之前沒有重新啓動。

此外,我看到您只打印BufferedReader的每第二行,並忽略每個第一個交替行。

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

這是行不通的。假設你想在一個大的String的文件,那麼你應該遵循以下的成語使用正確BufferedReader#readLine()

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(); 
+0

嗨,BalusC!感謝您的快速回答! 我會在幾分鐘內嘗試您的建議。 現在我將嘗試解釋我需要什麼 - 我需要一個方法,它將接收某個文件(它可能是txt文件,xls,sql)的URL作爲參數,我想讀取該文件中的信息並進行處理它後來。 但是,而不是該文件中的信息,我收到當前頁面的源代碼。 – Elena 2010-05-20 13:28:13

+0

是的,我已經明白了。這只是表示URL是錯誤的。檢查'this.filename'的值。或者重新啓動服務器。 – BalusC 2010-05-20 13:39:24

相關問題