BlobstoreService blobstoreService = BlobstoreServiceFactory
.getBlobstoreService();
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
UploadOptions uploadOptions = UploadOptions.Builder
.withGoogleStorageBucketName("photobucket11")
.maxUploadSizeBytes(1048576);
String blobUploadUrl = blobstoreService.createUploadUrl("/upload",
uploadOptions);
// String blobUploadUrl = blobstoreService.createUploadUrl("/uploaded");
resp.setStatus(HttpServletResponse.SC_OK);
resp.setContentType("text/plain");
PrintWriter out = resp.getWriter();
out.print(blobUploadUrl);
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
doGet(req, resp);
}
Bitmap bmp = BitmapFactory.decodeFile(imagePath);
ByteArrayOutputStream out = new ByteArrayOutputStream();
bmp.compress(CompressFormat.JPEG, 75, out);
byte[] imgByte = out.toByteArray();
String encodedImage = Base64.encodeToString(imgByte,
Base64.DEFAULT);
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(
"app-url/ImgUpload");
HttpResponse response = httpClient.execute(httpGet);
HttpEntity urlEntity = response.getEntity();
InputStream in = urlEntity.getContent();
String str = "";
while (true) {
int ch = in.read();
if (ch == -1)
break;
str += (char) ch;
}
此代码使用url存储图像:
httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(str);
ByteArrayBody bab = new ByteArrayBody(imgByte, "forest.jpg");
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("uploaded", bab);
reqEntity.addPart("photoCaption", new StringBody("sfsdfsdf"));
postRequest.setEntity(reqEntity);
response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();
while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
在这里,如果检查字符串s
的值,它显示null
。这意味着它返回的是空响应。我不知道这个代码有什么问题。请指导我解决这个问题。
经过多次尝试,我解决了这个问题。要在blobstore中存储映像,首先android需要向servlet发出请求,servlet将生成上传url:
Android客户端:将请求生成url并从servlet获取url
HttpClient httpClient = new DefaultHttpClient();
//This will invoke "ImgUpload servlet
HttpGet httpGet = new HttpGet("my-app-url/ImgUpload");
HttpResponse response = httpClient.execute(httpGet);
HttpEntity urlEntity = response.getEntity();
InputStream in = urlEntity.getContent();
String str = "";
while (true) {
int ch = in.read();
if (ch == -1)
break;
str += (char) ch;
}
imgupload.java-Servlet生成url并向客户端发送响应
BlobstoreService blobstoreService = BlobstoreServiceFactory
.getBlobstoreService();
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
//"uploaded" is another servlet which will send UploadUrl and blobkey to android client
String blobUploadUrl = blobstoreService.createUploadUrl("/uploaded");
resp.setStatus(HttpServletResponse.SC_OK);
resp.setContentType("text/plain");
PrintWriter out = resp.getWriter();
out.print(blobUploadUrl);
}
//Save image to generated url
HttpPost httppost = new HttpPost(str);
File f = new File(imagePath);
FileBody fileBody = new FileBody(f);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("file", fileBody);
httppost.setEntity(reqEntity);
response = httpClient.execute(httppost); //Here "uploaded" servlet is automatically invoked
urlEntity = response.getEntity(); //Response will be returned by "uploaded" servlet in JSON format
in = urlEntity.getContent();
str = "";
while (true) {
int ch = in.read();
if (ch == -1)
break;
str += (char) ch;
}
JSONObject resultJson = new JSONObject(str);
String blobKey = resultJson.getString("blobKey");
String servingUrl = resultJson.getString("servingUrl");
BlobstoreService blobstoreService = BlobstoreServiceFactory
.getBlobstoreService();
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
try {
List<BlobKey> blobs = blobstoreService.getUploads(req).get("file");
BlobKey blobKey = blobs.get(0);
ImagesService imagesService = ImagesServiceFactory
.getImagesService();
ServingUrlOptions servingOptions = ServingUrlOptions.Builder
.withBlobKey(blobKey);
String servingUrl = imagesService.getServingUrl(servingOptions);
resp.setStatus(HttpServletResponse.SC_OK);
resp.setContentType("application/json");
JSONObject json = new JSONObject();
json.put("servingUrl", servingUrl);
json.put("blobKey", blobKey.getKeyString());
PrintWriter out = resp.getWriter();
out.print(json.toString());
out.flush();
out.close();
} catch (JSONException e) {
e.printStackTrace();
}
}
如何从Android客户端上传图像到GAE(Google App Engine)数据存储? 尚未找到教程,已尝试了7天。
我有一个问题与我的代码,我必须上传图像到Firebase存储,我需要的图像来自画廊和相机,从画廊的图像是好的,但来自相机的图像给问题,图像加载在ImageView和被发送到数据库是黑色的。有人知道如何解决这个问题吗,或者你知道任何其他加载图像的方法吗? 来自画廊 从相机
我从Firebase存储中获取图像,用相机拍照,或者从图书馆中挑选一张。当其中一个完成时,我有一个类来存储,以便在需要时使用它。 现在我需要将此图像上传到Firebase存储(修改或新建,无所谓)。Firebase允许我使用以下选项之一:或,每个选项分别需要或。 我怎么能把我的,并从它获得一个或用于上传? -- 或者,为了解决这个问题,当我从Firebase存储中检索图像时,如何获取图像的? 无论
我正在使用一个Vaadin上传组件,到目前为止,我已经成功地将一个图像上传到一个目录,并在成功上传后在面板组件中显示它。在这之后,我想做的是把它也插入到数据库中。我有一个名为Show的表,它有一个名称、日期和图像。在Show类中,我尝试将图像作为字节数组或Blob。 在上传成功的方法中,我想将文件转换为字节数组,到目前为止我已经尝试过了: 我也试过: uIP它实际上是我的上传ImagePresen
当将图像/视频上传到Google云存储时,这是获得blob_key的正确方法吗? 文档要求使用创建blob_key。文件名从何而来?在对象存储在GCS后调用的上传处理程序的post函数中是否应该包含此代码? 此外,从谷歌文档中,我对这段代码的作用或与图片的关系感到困惑:
问题内容: 我一直在寻找这个东西,对我没有任何帮助。 我正在尝试将图像从android应用上传到java servlet并将其保存在服务器中。我发现的每个解决方案都不适合我。 我的代码当前正在执行的操作:android应用程序正在将图像发送到servlet,当我尝试保存该图像时,该文件已创建,但它是空的:( 谢谢你的帮助! 我在android客户端中的代码(i_file是设备上的文件位置): 我在