1

我的目标是尝试从我的 GWT 应用程序的 servlet 中读取位于服务器上的 csv 文件。

我最初尝试过这种方法

public String[] convertFileToStringArrays(String pathToCSV) 
{
    BufferedReader br = null;
    try 
    {
        br = new BufferedReader(new FileReader(pathToCSV));
    } 
    catch (FileNotFoundException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String currLine;
    String [] string_arrays = null;
    int i = 0;
    try {
        while ((currLine = br.readLine()) != null )
        {
            System.out.println(currLine);
            string_arrays[i] = currLine;
            i++;
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return string_arrays;
}

但是,我遇到了 Java IO 类型异常。我发现使用上述方法是不可能的,因为 GWT 应用程序无法访问 Java IO 库。所以我搜索了一下,发现人们说在服务器上的 GWT 应用程序中读取文件的方法是使用 Request Builder。所以我使用我在网上看到的一些示例尝试了以下方法。在这篇文章中,文件位于我的本地文件系统上,我通过 PathToCSV 变量传递了一个路径:

public String[] convertFileToStringArrays(final String pathToCSV)
{
     RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, pathToCSV);
     try
     {
         rb.sendRequest(null, new RequestCallback()
         {
            @Override
            public void onError(Request request, Throwable exception) 
            {
                System.out.println("Failed to load file:"+ pathToCSV);

            }

            @Override
            public void onResponseReceived(Request request,Response response) 
            {
                //TO DO currently just using this until the method is functional and can retrieve the file
                String resp = response.getText();

            }

         });
     }
     catch (Exception e)
     {
         System.out.println(e.getMessage());

     }

    return null;



}

}

但是,此代码引发了另一个异常:

异常堆栈跟踪:

Jun 26, 2012 7:29:41 AM com.google.appengine.tools.development.ApiProxyLocalImpl log

严重:javax.servlet.ServletContext 日志:调度传入 RPC 调用 com.google.gwt.user.server.rpc.UnexpectedException 时出现异常:服务方法'public abstract java.lang.String[] cs310.client.CsvFileParserService.convertFileToStringArrays(java .lang.String)' 引发意外异常:java.lang.UnsatisfiedLinkError: com.google.gwt.xhr.client.XMLHttpRequest.create()Lcom/google/gwt/xhr/client/XMLHttpRequest; 在 com.google.gwt.user.server.rpc.RPC.encodeResponseForFailure(RPC.java:385) 在 com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:588) 在 com.google .gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:208) 在 com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:248) 在 com.google.gwt.user .server.rpc。jetty.HttpConnection.handle(HttpConnection.java:404) at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409) at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)引起:java.lang.UnsatisfiedLinkError: com.google.gwt.xhr.client.XMLHttpRequest.create()Lcom/google/gwt/xhr/client/XMLHttpRequest; 在 com.google.gwt.xhr.client.XMLHttpRequest.create(Native Method) 在 com.google.gwt.http.client.RequestBuilder.doSend(RequestBuilder.java:369) 在 com.google.gwt.http.client。在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:

我想知道是否有人可以帮助我了解我是否在正确的轨道上使用此代码将 csv 文件读入我的应用程序?如果是这样,那么我该怎么做才能解决此异常。如果没有,那么是否有任何相同的代码示例?


尝试 URL 连接方法后添加新代码

public String[] convertFileToStringArrays(final String pathToCSV)
{
    String [] string_arrays = null;
     try {
            URL url = new URL(pathToCSV);
            BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
            String currLine;

            int i = 0;
            while ((currLine = br.readLine()) != null )
                {
                    System.out.println(currLine);
                    string_arrays[i] = currLine;
                    i++;
                }
            br.close();

        } 
catch (MalformedURLException e) {
            // ...
        } catch (IOException e) {
            // ...
        }
    if(string_arrays == null)
        System.out.println("CsvFilePasrserServiceImpl says that string_arrays is null");
        return string_arrays;

}

这导致了这个堆栈跟踪:

SEVERE: javax.servlet.ServletContext log: Exception while dispatching incoming RPC call
com.google.gwt.user.server.rpc.UnexpectedException: Service method 'public abstract java.lang.String[]     cs310.client.CsvFileParserService.convertFileToStringArrays(java.lang.String)' threw an     unexpected exception: java.security.AccessControlException: access denied     (java.io.FilePermission /Users/AM/Sites/new_food_vendor_locations2.csv read)
at com.google.gwt.user.server.rpc.RPC.encodeResponseForFailure(RPC.java:385)
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:588)
at     com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:208)
at     com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:248)
at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
at    com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:35)
at   org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at   com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:60)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:122)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.BackendServersFilter.doFilter(BackendServersFilter.java:97)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at    com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:78)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:369)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:938)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:755)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
Caused by: java.security.AccessControlException: access denied (java.io.FilePermission /Users/AM/Sites/new_food_vendor_locations2.csv read)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:374)
at java.security.AccessController.checkPermission(AccessController.java:546)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at com.google.appengine.tools.development.DevAppServerFactory$CustomSecurityManager.checkPermission(DevAppServerFactory.java:252)
at java.lang.SecurityManager.checkRead(SecurityManager.java:871)
at java.io.File.isDirectory(File.java:752)
at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:65)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:161)
at java.net.URL.openStream(URL.java:1010)
at cs310.server.CsvFileParserServiceImpl.convertFileToStringArrays(CsvFileParserServiceImpl.java:99)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.google.appengine.tools.development.agent.runtime.Runtime.invoke(Runtime.java:115)
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:569)
... 34 more
4

1 回答 1

2

您遇到的问题java.io不是因为 GWT,而是因为AppEngine。GWT 是客户端的,这就是为什么当您尝试RequestBuilder在服务器端使用时它现在会失败的原因。

要从 AppEngine 上的服务器端获取 URL,请使用URL Fetch 服务

于 2012-06-26T15:23:33.570 回答