我有一个java服务,它接受一个字节数组,以便将其转换为一个或多个pdf文件或jpg文件。我知道这项服务是有效的,因为它是从另一个正确发送文件而没有问题的java系统调用的。现在我需要从一个角度js系统调用这个服务,字节数组一旦到达java应用程序,它首先转换为ByteArrayInputStream而没有问题,然后ByteArrayInputStream转换为ZipInputStream但失败了。我怀疑问题是数组的编码类型。
这是我的代码:
public static Hashtable<String, ByteArrayOutputStream> unzipFile(InputStream inputStream){
logger.info("Unzip del File");
Hashtable<String, ByteArrayOutputStream> fileOutputTable = new Hashtable<String, ByteArrayOutputStream>();
try{
byte[] buf = new byte[1024];
ZipEntry zipentry;
ByteArrayInputStream bis = (ByteArrayInputStream)inputStream;
ZipInputStream zipinputstream = new ZipInputStream(bis); // here conversion fail
while((zipentry = zipinputstream.getNextEntry()) != null){
String entryName = zipentry.getName();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int n = 0;
while((n = zipinputstream.read(buf, 0, 1024)) > -1)
baos.write(buf, 0, n);
baos.close();
fileOutputTable.put(entryName, baos);
zipinputstream.closeEntry();
}
zipinputstream.close();
}catch(Exception e){
logger.error("Errore nel tentativo di unzip del file");
e.printStackTrace();
}
logger.info("RETURN: " + fileOutputTable.toString());
return fileOutputTable;
}
public static Hashtable<String, ByteArrayOutputStream> unzipFile(InputStream inputStream){
logger.info("Unzip del File");
Hashtable<String, ByteArrayOutputStream> fileOutputTable = new Hashtable<String, ByteArrayOutputStream>();
try{
byte[] buf = new byte[1024];
ZipEntry zipentry;
ByteArrayInputStream bis = (ByteArrayInputStream)inputStream;
ZipInputStream zipinputstream = new ZipInputStream(bis); // here conversion fail
while((zipentry = zipinputstream.getNextEntry()) != null){
String entryName = zipentry.getName();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int n = 0;
while((n = zipinputstream.read(buf, 0, 1024)) > -1)
baos.write(buf, 0, n);
baos.close();
fileOutputTable.put(entryName, baos);
zipinputstream.closeEntry();
}
zipinputstream.close();
}catch(Exception e){
logger.error("Errore nel tentativo di unzip del file");
e.printStackTrace();
}
logger.info("RETURN: " + fileOutputTable.toString());
return fileOutputTable;
}