2013-03-08 107 views
0

我正在嘗試編寫一個將返回文件的Servlet。最終目標是返回無法通過URL直接訪問的文件。我開始使用只打印Hello World的GuestbookServlet,在它的基礎上構建了一些我在這裏找到的代碼。Google App Engine - Java Servlet - 文件訪問

我使用App Engine SDK 1.7.5與赫利俄斯服務版本2

在Eclipse中我有放置在「戰爭」路徑的文件。

我使用的網址是:

http://localhost:8888/guestbook?gimme 

這裏是我的代碼:

package guestbook; 

import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 

import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

@SuppressWarnings("serial") 
public class GuestbookServlet extends HttpServlet { 

private String identifier = "gimme"; 

public void doGet(HttpServletRequest req, HttpServletResponse resp) 
     throws IOException { 
    String queryString = req.getQueryString(); 
    if (queryString != null && queryString.equals(identifier)) { 

     // String filename = URLDecoder.decode(req.getPathInfo(), "UTF-8"); 
     String filename = "pic.png"; 
     File file = new File("/files/", filename); 
     System.out.println("FILE: " + file); 
     resp.setHeader("Content-Type", 
       getServletContext().getMimeType(file.getName())); 
     resp.setHeader("Content-Length", String.valueOf(file.length())); 
     resp.setHeader("Content-Disposition", 
       "inline; filename=\"" + file.getName() + "\""); 

     BufferedInputStream input = null; 
     BufferedOutputStream output = null; 

     try { 
      input = new BufferedInputStream(new FileInputStream(file)); 
      output = new BufferedOutputStream(resp.getOutputStream()); 

      byte[] buffer = new byte[8192]; 
      for (int length = 0; (length = input.read(buffer)) > 0;) { 
       output.write(buffer, 0, length); 
      } 
     } finally { 
      if (output != null) 
       try { 
        output.close(); 
       } catch (IOException ignore) { 
       } 
      if (input != null) 
       try { 
        input.close(); 
       } catch (IOException ignore) { 
       } 
     } 

    } else { 
     resp.getWriter().println("Invalid Request"); 
    } 

} 
} 

這是我的錯誤:

Mar 7, 2013 9:12:33 PM com.google.apphosting.utils.jetty.JettyLogger warn 
WARNING: /guestbook 
java.security.AccessControlException: access denied (java.io.FilePermission \files\pic.png read) 
    at java.security.AccessControlContext.checkPermission(Unknown Source) 
    at java.security.AccessController.checkPermission(Unknown Source) 
    at java.lang.SecurityManager.checkPermission(Unknown Source) 
    at com.google.appengine.tools.development.DevAppServerFactory$CustomSecurityManager.checkPermission(DevAppServerFactory.java:289) 
    at java.lang.SecurityManager.checkRead(Unknown Source) 
    at java.io.File.length(Unknown Source) 
    at guestbook.GuestbookServlet.doGet(GuestbookServlet.java:29) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617) 
    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.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:74) 
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) 
    at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:123) 
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) 
    at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34) 
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) 
    at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:61) 
    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:125) 
    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:94) 
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) 
    at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:409) 
    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.headerComplete(HttpConnection.java:923) 
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:547) 
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212) 
    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) 

回答

3

您不能使用訪問文件java io類,因爲應用程序引擎禁用了要使用的Java io庫。所以如果你想讀一個文件,你必須把這個文件放到blob存儲區,然後從那裏讀取它。

+0

那麼這是什麼意思? https://developers.google.com/appengine/docs/java/config/appconfig?hl=zh-CN#Static_Files_and_Resource_Files – tunneling 2013-03-08 03:40:13

+2

應用程序工程中的靜態文件用於設計您的Web應用程序,而不是用於動態操作 – olyanren 2013-03-08 12:33:30