我正在尝试使用Google Cloud Endpoints将图像文件上传到Blobstore . 下面的代码成功地将图像存储在Blobstore中,但是没有在数据存储区中创建实体“图像”,因此它不会将图像引用添加到MyUser实体(我使用Objectify将实体存储在数据存储区中) .
另外,当我上传图像时,我得到“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>
My Endpoints Code
@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();
}
保存图片:
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());
}
哪里我错了?提前致谢 .
1 回答
我认为根本不会调用你的处理程序 . Cloud endpoints 路径始终位于/ _ah / api / ....之后 . 因此,您的上传处理程序根本未映射到blobstore上载路径 . 您必须创建一个单独的servlet,该servlet映射到/ upload并将其添加到Web.xml . 至少到目前为止这是我的解决方案 .
创建一个像这样的独立servlet:
将这些添加到您的web.xml
祝好运!