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): файл или каталог не может быть найден.тип хранимого файла - байт-код. Это означает, что это не файл изображения.
Кто может дать мне любую идею или пример?