Java 如何从google webapp引擎下载图像文件? AppEngineFile AFE=新的AppEngineFile(FILESYSTEM+alist.get(0.getPath()); BlobKey bk=FileServiceFactory.getFileService().getBlobKey(AFE); if(“image/jpeg”.equals(alist.get(0.getContentType())){ 分别为setContentType(“图像/jpeg”); } resp.setHeader(“内容处置”、“附件;文件名=\”+alist.get(0.getTitle()+“\”); FileService FileService=fileservicecfactory.getFileService(); AppEngineFile file=null; 试一试{ file=fileService.getBlobFile(bk); }catch(filenotfounde异常){ e、 printStackTrace(); } FileReadChannel ch=null; 试一试{ ch=fileService.openReadChannel(文件,false); }catch(filenotfounde异常){ e、 printStackTrace(); }捕获(锁定例外){ e、 printStackTrace(); }捕获(IOE异常){ e、 printStackTrace(); } 字节[]barray=新字节[MAXSIZE]; ByteArrayOutputStream bas=新的ByteArrayOutputStream(); 试一试{ ByteBuffer bb=ByteBuffer.wrap(barray); 国际nRead; 而((nRead=ch.read(bb))!=-1){ 对于(int i=0;i

Java 如何从google webapp引擎下载图像文件? AppEngineFile AFE=新的AppEngineFile(FILESYSTEM+alist.get(0.getPath()); BlobKey bk=FileServiceFactory.getFileService().getBlobKey(AFE); if(“image/jpeg”.equals(alist.get(0.getContentType())){ 分别为setContentType(“图像/jpeg”); } resp.setHeader(“内容处置”、“附件;文件名=\”+alist.get(0.getTitle()+“\”); FileService FileService=fileservicecfactory.getFileService(); AppEngineFile file=null; 试一试{ file=fileService.getBlobFile(bk); }catch(filenotfounde异常){ e、 printStackTrace(); } FileReadChannel ch=null; 试一试{ ch=fileService.openReadChannel(文件,false); }catch(filenotfounde异常){ e、 printStackTrace(); }捕获(锁定例外){ e、 printStackTrace(); }捕获(IOE异常){ e、 printStackTrace(); } 字节[]barray=新字节[MAXSIZE]; ByteArrayOutputStream bas=新的ByteArrayOutputStream(); 试一试{ ByteBuffer bb=ByteBuffer.wrap(barray); 国际nRead; 而((nRead=ch.read(bb))!=-1){ 对于(int i=0;i,java,image,google-app-engine,servlets,download,Java,Image,Google App Engine,Servlets,Download,不久前我遇到了这个问题。您无法读取随应用程序上载的文件。它们被视为静态应用程序blob,不存在代码可以访问的任何方式 看一看,现在唯一可用的选项是BLOBSTORE。 如果希望应用程序代码能够读取某些内容,则必须将其存储在Blobstore中,或作为数据存储中对象的BlobProperty存储(这非常低效,如果可以,请使用Blobstore) AppEngineFile AFE = new AppEngineFile(FILESYSTEM + alist.get(0).getPath()); B

不久前我遇到了这个问题。您无法读取随应用程序上载的文件。它们被视为静态应用程序blob,不存在代码可以访问的任何方式

看一看,现在唯一可用的选项是
BLOBSTORE
。 如果希望应用程序代码能够读取某些内容,则必须将其存储在Blobstore中,或作为数据存储中对象的
BlobProperty
存储(这非常低效,如果可以,请使用Blobstore)

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();