我正在尝试删除使用FileService存储的文件。在少数情况下删除成功但在大多数情况下它没有成功,我没有找到一个模式。我在本地服务器上的Windows 7上使用eclipse。我该如何删除文件?
编辑:当我上传到appengine时它会起作用。
这是存储代码:
try
{
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.createNewBlobFile(content_type, fileName);
boolean lock = true;
FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);
byte[] b1 = new byte[BUFFER_SIZE];
int readBytes1;
while ((readBytes1 = is.read(b1)) != -1)
{
writeChannel.write(ByteBuffer.wrap(b1, 0, readBytes1));
}
writeChannel.closeFinally();
blobKey = fileService.getBlobKey(file);
item_image_blob_key = blobKey.getKeyString();
}
catch (Exception e)
{
System.out.println(e.getLocalizedMessage());
e.printStackTrace(response.getWriter());
}
这是删除尝试:
@Override
public long deleteItem(Long id)
{
System.out.println(LOG +" Trying to delete item with this id: " + id);
Key parent = KeyFactory.createKey("MffItems", MFF_ITEM_ROOT_KEY);
Key key = KeyFactory.createKey(parent, "Item", id);
try
{
Entity e = datastore.get(key);
String image_key = (String) e.getProperty("image_blob_key");
BlobKey blobKey = new BlobKey(image_key);
final AppEngineFile f = fileService.getBlobFile(blobKey);
if(f.isReadable())
{
System.out.println(LOG + " file is readable");
if(f.hasFinalizedName())
{
System.out.println(LOG + " file has finalized name: " + f.getNamePart());
fileService.delete(f); // Problematic line
datastore.delete(key);
return id;
}
}
}
catch(Exception e)
{
System.out.println(LOG + " " + e.toString() + " " + e.getMessage());
}
// TODO Auto-generated method stub
return -1;
}
这是我得到的错误:
I_MFF_ItemService尝试删除具有此ID的项目:2 I_MFF_ItemService文件是可读的 I_MFF_ItemService文件已完成名称:TXP5bVCmBVugDOxktBGv_w 2013年6月30日下午12:24:03 com.google.appengine.api.blobstore.dev.LocalBlobstoreService $ 1运行 警告:无法删除blob: java.io.IOException:无法删除:C:\ Users \ Gideon \ Desktop \ dev \ workspace2 \ ItemManager1.41 \ war \ WEB-INF \ appengine-generated \ TXP5bVCmBVugDOxktBGv_w 在com.google.appengine.api.blobstore.dev.FileBlobStorage.deleteBlob(FileBlobStorage.java:79) 在com.google.appengine.api.blobstore.dev.LocalBlobstoreService $ 1.run(LocalBlobstoreService.java:153) at java.security.AccessController.doPrivileged(Native Method) 在com.google.appengine.api.blobstore.dev.LocalBlobstoreService.deleteBlob(LocalBlobstoreService.java:146) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) 在com.google.appengine.tools.development.ApiProxyLocalImpl $ AsyncApiCall.callInternal(ApiProxyLocalImpl.java:521) 在com.google.appengine.tools.development.ApiProxyLocalImpl $ AsyncApiCall.call(ApiProxyLocalImpl.java:475) 在com.google.appengine.tools.development.ApiProxyLocalImpl $ AsyncApiCall.call(ApiProxyLocalImpl.java:452) at java.util.concurrent.Executors $ PrivilegedCallable $ 1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.util.concurrent.Executors $ PrivilegedCallable.call(Unknown Source) at java.util.concurrent.FutureTask $ Sync.innerRun(Unknown Source) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source) 在java.lang.Thread.run(未知来源)
I_MFF_ItemService java.io.IOException:Blobstore失败Blobstore失败 2013年6月30日12:24:27 com.google.appengine.api.datastore.dev.LocalDatastoreService $ PersistDatastore persist 信息:持续数据存储的时间:20毫秒