我尝试使用Google Cloud Endpoints将图片文件上传到Blobstore。下面的代码成功地将图像存储在Blobstore中,但是没有创建实体" Image"在数据存储区中,它没有将图像引用添加到MyUser实体(我使用Objectify将实体存储在数据存储区中)。
另外,当我上传图片时,我得到了#34; 404 NOT_FOUND"错误作为响应...但图像正确存储在Blobstore中。
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>404 NOT_FOUND</title>
</head>
<body text=#000000 bgcolor=#ffffff>
<h1>Error: NOT_FOUND</h1>
</body>
</html>
我的终结点代码
@Api(
name = "imageManager"
)
public class ImageManager {
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
@ApiMethod(name = "urlToUpload", httpMethod = "get")
public Image urlToUpload(){
Image image = new Image();
image.url = blobstoreService.createUploadUrl("/imageManager/upload");
return image;
}
这是上传处理程序:
@ApiMethod(name = "upload", httpMethod = "post")
public Image upload(HttpServletRequest httpServletRequest) throws BadRequestException, NotFoundException, UnauthorizedException {
String op = httpServletRequest.getParameter("op");
if(op == null) throw new BadRequestException("op is null");
Image image = null;
if(op.equalsTo("registerUser") image = this.uploadRegisterUser(httpServletRequest);
return image;
}
UploadRegisterUser:
private Image uploadRegisterUser(HttpServletRequest httpServletRequest) throws NotFoundException {
Long userId = Long.parseLong(httpServletRequest.getParameter("userId"));
MyUser user = ofy().load().type(MyUser.class).filter("id",userId).first().now();
if(user == null) throw new NotFoundException("User not found");
Ref<Image> imageRef = this.saveImage(httpServletRequest, wUser, null);
user.profileImage = imageRef;
ofy().save().entity(wUser).now();
return imageRef.getValue();
}
SaveImage:
private Ref<Image> saveImage(HttpServletRequest httpServletRequest, MyUser user, String label){
List<BlobKey> blobs = blobstoreService.getUploads(httpServletRequest).get("file");
Image image = new Image();
image.key = blobs.get(0).getKeyString();
image.user = Ref.create(user);
image.date = new Date();
image.label = label;
return Ref.create(ofy().save().entity(image).now());
}
我哪里错了?提前谢谢。
答案 0 :(得分:1)
我认为根本不会调用你的处理程序。云端点路径始终位于/ _ah / api / ....之后。因此,您的上传处理程序根本未映射到blobstore上载路径。您必须创建一个单独的servlet,该servlet映射到/ upload并将其添加到Web.xml。至少到目前为止这是我的解决方案。
创建一个像这样的独立servlet:
public class UploadgimageServlet extends HttpServlet {
private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
BlobKey blobKey = blobs.get(“file");
if (blobKey == null) {
res.sendRedirect("/");
} else {
res.sendRedirect("http://localhost:8888/getimage?blob-key=" + blobKey.getKeyString());
}
}
}
将这些添加到您的web.xml
<servlet>
<servlet-name>Uploadgimage</servlet-name>
<servlet-class>com.yourcomp.UploadgimageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Uploadgimage</servlet-name>
<url-pattern>/imageManager/upload</url-pattern> //Based on your original URL
</servlet-mapping>
祝你好运!