1

また私です)私には別の問題があります。Web からファイル (たとえば、txt) をロードしたい。マネージド 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;
  }

しかし、BufferedReader は現在のページの html コードを返します。ここでマネージド Bean のメソッドを呼び出そうとしています。それは非常に奇妙なことです-私はグーグルで検索しましたが、誰もこの問題を抱えていません.

多分私は何か間違っているかもしれません.net APIを使用せずにファイルをWeb(jsf)アプリにロードする最も簡単な方法があるかもしれません. 何か案は?

助けてくれてどうもありがとう!

更新

おそらくいくつかの 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 : Web (localhost ではない) から別のファイルを取得します。すべて正常に動作します。これについて夢中になって申し訳ありません)))

BalusC が言ったように、私のアプリは URL でファイルを見つけられませんでした: http://localhost:8080/sfront/files/test.txt

ローカルファイルを使用できなかった理由がわかりません。

何か案は?

4

1 に答える 1

1

実際にこれを JSF ページに含めたい場合は、JSTL c: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 では機能せず、(残念ながら) 同様の機能も提供していません。

実際の問題について:説明されている問題は、投稿されたコード情報の範囲外で発生したか、実行していると予想されるコードを実行しなかったため、わかりません(Webサーバーを再起動して、最新の変更がJava コードはコンパイルされます)。少なくとも はthis.filename間違った値を返しました。ご自身の Web ページの URL がアウトコメントされているようです。これを変更した可能性がありますが、hotdeploy が失敗したか、サーバーがテスト前に再起動されませんでした。

BufferedReaderさらに、最初の交互の行をすべて無視して、2行おきに印刷していることがわかります。

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

これはうまくいきません。ファイルを単一の大きStringBufferedReader#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();
于 2010-05-20T13:14:48.463 に答える