java如何从google webapp引擎下载图像文件?
AppEngineFile AFE = new AppEngineFile(FILESYSTEM + alist.get(0).getPath());
BlobKey bk = FileServiceFactory.getFileService().getBlobKey(AFE);
if("image/jpeg".equals(alist.get(0).getContentType()) ){
resp.setContentType("image/jpeg");
}
resp.setHeader("Content-Disposition", "attachment; filename=\"" + alist.get(0).getTitle() + "\"");
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = null;
try {
file = fileService.getBlobFile(bk);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
FileReadChannel ch = null;
try {
ch = fileService.openReadChannel(file, false);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (LockException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
byte[] barray = new byte[MAXSIZE];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ByteBuffer bb = ByteBuffer.wrap(barray);
int nRead;
while ((nRead=ch.read(bb)) != -1) {
for (int i=0; i < nRead; i++) {
try {
baos.write(barray[i]);
} catch (Exception e) {
e.printStackTrace();
}
}
bb.clear();
}
} catch (IOException e) {
e.printStackTrace();
}
resp.setContentLength(baos.size());
// resp.getOutputStream().write(baos.toByteArray()); // <= if I use it, this message, "Error 6 (net::ERR_FILE_NOT_FOUND): The file or directory could not be found.", is showed.
// out.print(baos); // <= if I use this, I can download a file but it is byte code.
baos.flush();
baos.close();
如何修复下载图像文件时的整个代码?因为如果我使用数字1,它会产生一个错误,即“错误6(net::ERR_FILE_NOT_FOUND):找不到文件或目录。”,这是一部电影。“或者,如果我使用数字2,它看起来不错,但存储的文件类型是字节码。这意味着不是图像文件
谁能给我一些想法或例子
# 1 楼答案
不久前我遇到了这个问题。无法读取随应用程序上载的文件。它们被视为静态应用程序Blob,不存在代码可以访问的任何方式
看看here,注意现在唯一可用的选项是
BLOBSTORE
。 如果希望应用程序代码能够读取某些内容,则必须将其存储在Blobstore中,或者作为数据存储中对象的BlobProperty
存储(这非常低效,如果可以,请使用Blobstore)