【发布时间】:2017-06-28 23:10:36
【问题描述】:
我正在尝试将图像列表压缩到单个 zip 文件中。
public void compressZip(List<Image> lstImage) {
//Abrimos una ventana JFileChooser
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int seleccion = fileChooser.showSaveDialog(laminaComicPrincipal);
if (seleccion == JFileChooser.APPROVE_OPTION)
{
File fichero = fileChooser.getSelectedFile();
try {
ZipOutputStream os = new ZipOutputStream(new FileOutputStream(fichero.getAbsolutePath()+"file.zip"));
int numeroImagen = 0;
for(Image imagen: lstImage){
ZipEntry entrada = new ZipEntry(numeroImagen+".jpg");
os.putNextEntry(entrada);
ImageInputStream fis = ImageIO.createImageInputStream(imagen);//THis sentences return null
byte [] buffer = new byte[1024];
int leido=0;
while (0 < (leido=fis.read(buffer))){ //FAIL SENTENCES --> fis=null
os.write(buffer,0,leido);
}
fis.close();
os.closeEntry();
numeroImagen++;
}
os.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
ImageIO.createImageInputStream(image n) 返回null。有什么问题??如果我先将图像保存在硬盘中,然后使用FileInputStream 读取文件,它会工作吗?但我不喜欢创建临时文件。
谢谢你所做的一切。
【问题讨论】:
标签: java image zip javax.imageio