【发布时间】:2015-04-15 07:40:21
【问题描述】:
我目前遇到以下问题:
我想从我的客户端 Java 应用程序将 Blob 上传到 Google Blobstore。在 Blobstore 文档 (https://cloud.google.com/appengine/docs/java/blobstore/) 中描述了如何通过使用 FileChooser 上传 Blob 的表单来执行此操作。
但是,我想从我的代码中执行此操作,但我不知道如何在 Java 中将 Object 转换为 Blob。我想在HttpURLConnection 中传递生成的Blob,并且我已经在Google App Engine 上为此创建了类。
所以我的问题是:如何从客户端 Java 应用程序上传 Blob?
注意:就我而言,我想将JavaFX Image 上传为Blob,但我认为这个问题一般都可以问。
更新 1: 使用FileService 可能是解决方案,正如@TejjD 所指出的那样。但是,Files API 将被弃用,因此没有用处。
更新 2:我在服务器上使用以下代码:
@Override
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
//Check if there is a parameter for the outfit id
try {
if (!req.getParameter("id").isEmpty() && Functions.isInteger(req.getParameter("id"))) {
//Check if the requested id exists in the Cloud SQL database
Connection conn = ConnectionManager.connectToCloudSQL();
GetQueryBuilder query = DataManager.executeGet(conn, req, "outfit");
//Check result
if (query.getResultSet() == null) {
throw new Exception("The Outfit object with the requested ID is not yet present in the Cloud SQL database");
}
Map<String,List<BlobKey>> blobs = blobstoreService.getUploads(req);
BlobKey blobKey = blobs.get("image").get(0);
if (blobKey == null) {
throw new Exception("No blobkey specified");
}
else {
//First create a checksum from the file
String checkSum = this.buildCheckSum(blobKey);
PersistenceManager pm = PMF.get().getPersistenceManager();
//Create new outfit object
int outfitId = Integer.parseInt(req.getParameter("id"));
OutfitExtension outfit = new OutfitExtension();
outfit.setId(outfitId);
outfit.setImage(blobKey);
outfit.setCheckSum(checkSum);
pm.makePersistent(outfit);
}
}
else {
throw new Exception("An outfit ID needs to be added to the HTTP request and should be from type Integer only.");
}
} catch (Exception e) {
res.sendError(HttpServletResponse.SC_BAD_REQUEST, "Bad request: "+e.getMessage());
}
}
此代码在调用某个 URL 时运行(我已将其连接到 web.xml)。
正如您在代码中看到的,它大约是blobstoreService.getUploads(req)。我怎样才能直接发布Blob 被识别为上传?
【问题讨论】:
-
感谢您的评论!但是,我自己已经链接到该信息。它为我提供了关于这个问题的哪些额外信息?
标签: java google-app-engine blobstore