4
在我的应用程序,我需要做到以下几点:1。 与图片(JPG格式仅现在)和其他的东西一个zip文件上传到Blob存储区。 2.应用程序引擎的后端应改为从上传的ZIP条目并保存发现里面的Blob存储作为独立的文件中的所有图像。解压图像转换成Blob存储
我成功上传,解压和保存文件@ Blob存储,但图像似乎被打破。 当我从BlobStore下载它们(简单地说blobstoreService.serve它们)时,图像颜色错误,或者部分显示,或者以其他方式破坏。尝试使用ImagesService也会引发异常。我在压缩之前检查了图像的大小,并将文件的大小在写入blobstore时解压缩,并且它们看起来相同。这里是我的代码:
ZipInputStream zis = ...;
ZipEntry entry;
while ((entry =zis.getNextEntry()) !=null)
{
String fileName = entry.getName().toLowerCase();
if(fileName.indexOf(".jpg") != -1 || fileName.indexOf(".jpeg") != -1)
{
FileService fileService = FileServiceFactory.getFileService();
String mime = ctx.getMimeType(fileName);//getting mime from servlet context
AppEngineFile file = fileService.createNewBlobFile(mime, fileName);
boolean lock = true;
FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);
byte[] buffer = new byte[BlobstoreService.MAX_BLOB_FETCH_SIZE];
while(zis.read(buffer) >= 0)
{
ByteBuffer bb = ByteBuffer.wrap(buffer);
writeChannel.write(bb);
}
writeChannel.closeFinally();
BlobKey coverKey = fileService.getBlobKey(file);
....
}
}
非常感谢您的时间!
UPD:我发现了一个变通的作品,但我仍然不明白为什么第一个方案失败。
int read;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while((read = zis.read()) >= 0)
{
baos.write(read);
if(baos.size() == BlobstoreService.MAX_BLOB_FETCH_SIZE)
{
ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray());
writeChannel.write(bb);
baos = new ByteArrayOutputStream();
}
}
if(baos.size() > 0)
{
ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray());
writeChannel.write(bb);
}
尝试原始图像的实际内容比较下载一个
使用? –