我真的希望你能帮助我!
目标是能够通过我的(java - 谷歌应用引擎)应用程序上传数据文件(.tsv)并获取其内容并将其写入mysql数据库。
经过一番研究,blobstore似乎是最好的选择,因为文件大约是70-100MB。
所以我几天前开始使用这个示例项目让我开始https://github.com/crhym3/java-blobstore-gcs-sample。
我的项目启动并运行没问题。由于我想访问内容而不是下载文件(这是示例项目正在做的事情),我尝试使用BlobstoreInputStream执行此操作,如多个来源中所建议的那样。因此,我从示例项目中更改的唯一文件是ServeBlobServlet。我尝试通过BlobstoreInputStream获取第一行,而不是提供整个blob。
public class ServeBlobServlet extends HttpServlet {
BlobstoreService blobstore = BlobstoreServiceFactory.getBlobstoreService();
private static final Logger log =
Logger.getLogger(ServeBlobServlet.class.getName());
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String[] parts = req.getRequestURI().split("/");
String gsObjectName = URLDecoder.decode(parts[parts.length - 1], "UTF-8");
log.info("Serving GCS object: " + gsObjectName);
BlobKey blobKey = blobstore.createGsBlobKey(gsObjectName);
//blobstore.serve(blobKey, resp);
System.out.println(getFirstLine(blobKey));
}
private String getFirstLine(BlobKey blobkey) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(new BlobstoreInputStream(blobkey)));
String line = reader.readLine();
reader.close();
return line;
}
如你所见。我实际上只是取消注释了blobstore.serve行并添加了标准输出行(并编写了getFirstLine方法)。
虽然我使用与Blobstore.serve相同的BlobKey(工作正常),但我收到此错误。
Uncaught exception from servlet
com.google.appengine.api.blobstore.BlobstoreInputStream$BlobstoreIOException: BlobstoreInputStream received an invalid blob key: AMIfv97t_VDPdp2Qg7UXkE3oc6nRe7yrr86jo6th4-cCU2w8HnjOF5iSOYyAlsrQDkWVLsOMYMdHlbl-AE14swvD5n03N4XStmumVlNeNtNANXI8OWNpodBmJrBB4QS7Ru_AXK5rYvV4-KizTi6kPznYvGQ6gCd1oizh0xtRwO0jkuC-K4l648XpGhZF65H-eb1JJtxWDEsMlQ9LbmRzEPJZHV3tfZ5YRLFnlzOEGnWZxj92BpORnBq8Ly_PMTA-xSNDuFsMBAJwYzdDwG3WVwDpnkxxkqvTLfTRxXKm1WgMZEtlP-0w4NbVwuJ8QNaTAzsDLCs14PFFvWV4pVMd9coXvF3KmC-Z6T4S8yC0nGK9ST0x8o0-CNI
at com.google.appengine.api.blobstore.BlobstoreInputStream.<init>(BlobstoreInputStream.java:121)
at com.google.appengine.api.blobstore.BlobstoreInputStream.<init>(BlobstoreInputStream.java:92)
at com.google.appengine.api.blobstore.BlobstoreInputStream.<init>(BlobstoreInputStream.java:104)
at com.moviemap.server.ServeBlobServlet.getFirstLine(ServeBlobServlet.java:43)
at com.moviemap.server.ServeBlobServlet.doGet(ServeBlobServlet.java:39)
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.apphosting.utils.servlet.ParseBlobUploadFilter.doFilter(ParseBlobUploadFilter.java:125)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.runtime.jetty.SaveSessionFilter.doFilter(SaveSessionFilter.java:37)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.utils.servlet.JdbcMySqlConnectionCleanupFilter.doFilter(JdbcMySqlConnectionCleanupFilter.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 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.apphosting.runtime.jetty.AppVersionHandlerMap.handle(AppVersionHandlerMap.java:260)
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 com.google.apphosting.runtime.jetty.RpcRequestParser.parseAvailable(RpcRequestParser.java:78)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at com.google.apphosting.runtime.jetty.JettyServletEngineAdapter.serviceRequest(JettyServletEngineAdapter.java:148)
at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.run(JavaRuntime.java:469)
at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:437)
at com.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:444)
at com.google.tracing.CurrentContext.runInContext(CurrentContext.java:234)
at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:308)
at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:300)
at com.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:441)
at com.google.apphosting.runtime.ThreadGroupPool$PoolEntry.run(ThreadGroupPool.java:235)
at java.lang.Thread.run(Thread.java:745)
我尝试了几种不同的方法和BlobKey构造函数,但没有任何作用。同样的错误不断回归。
提前感谢任何花时间帮助的人!欣赏它!
答案 0 :(得分:1)
以下是答案
konqi对上述问题的评论绝对正确。 我开始使用的项目(链接在问题中)不适合直接上传和读取blob。正如konqi所说,我所做的(项目的作用)是将blob上传到谷歌云存储。
所以回答:如果你想将blob上传到谷歌blobstore并直接阅读,你应该按照这里的教程https://cloud.google.com/appengine/docs/java/blobstore/#Java_Using_the_Blobstore,而不是提供blob(参见教程中的段落),你可以这样读:
String keyString = req.getParameter("blob-key");
BlobKey blobkey = new BlobKey(keyString);
BufferedReader reader = new BufferedReader(new InputStreamReader(new BlobstoreInputStream(blobkey)));
String line = null;
while((line = reader.readLine()) != null) {
//Do whatever you like with the line
}
reader.close();
希望这有助于某人