【问题标题】:How can I load txt file from internet into my jsf app?如何将 txt 文件从互联网加载到我的 jsf 应用程序中?
【发布时间】:2010-05-20 13:05:40
【问题描述】:

又是我)我还有一个问题。我想从网络加载文件(例如 - 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 的方法。 这很奇怪——我用谷歌搜索过,没有人遇到过这个问题。

也许我做错了什么,也许我们有一种不使用网络 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:我从网络(不是本地主机)获取另一个文件 - 一切正常!很抱歉为此发疯)))

正如 BalusC 所说,我的应用程序只是没有通过 URL 找到文件:http://localhost:8080/sfront/files/test.txt

我不知道为什么我不能使用本地文件。

有什么想法吗?

【问题讨论】:

  • 你需要一些代码格式化...
  • 我格式化了一些代码。 @Elena:编辑消息时请注意右侧栏中的格式规则,并利用页面底部的预览部分检查一切是否正常。
  • 感谢您的帮助)))您格式化了什么代码?在我的浏览器(Opera 10.53)中,一切看起来都不错。对于源代码,我做了 4 个空格(甚至更多)。你能告诉我,出了什么问题,我该如何解决?

标签: java jsf urlconnection bufferedreader


【解决方案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,而且(不幸的是)它也不提供类似的功能。

至于您的实际问题:我不知道,因为所描述的问题是在迄今为止发布的代码信息范围之外引起的,或者您没有运行您期望它正在运行的代码(重新启动网络服务器以确保Java 代码的最新更改被编译)。至少 this.filename 返回了不正确的值,我看到您已将自己网页的 URL 注释掉。也许您更改了此设置,但热部署失败或在测试之前未重新启动服务器。

此外,我发现您只打印来自 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();

【讨论】:

  • 嗨,BalusC!感谢您的快速答复!我会在几分钟内尝试你的建议。现在我将尝试解释我需要什么 - 我需要一个方法,它将接收某个文件(可能是 txt 文件、xls、sql)的 URL 作为参数,我想读取该文件中的信息并稍后处理它.但我收到的不是该文件中的信息,而是当前页面的源代码。
  • 是的,我已经明白了。这只是意味着 URL 是错误的。检查this.filename 的值。或者重启服务器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-02
  • 1970-01-01
  • 1970-01-01
  • 2018-11-13
  • 2010-09-05
  • 2014-10-18
相关资源
最近更新 更多