[英]How to identify and write an excel file with .xls extension which has .xlsx content
我正在將內容從一個DMS(Filenet)遷移到另一個DMS(Webcenter Content),在此過程中,我在Filenet中遇到了一個擴展名為.xls的excel文件(內容類型為application / vnd.ms-excel)。
我得到了文件的InputStream並將其寫入(文件IO操作),因為將其推送到Webcenter Content。
問題是當我下載並打開excel文件F1.xls的Filenet版本時,它以不匹配的文件格式和擴展名預熱提示我,但仍然打開文件並顯示內容。
但是我推送到Webcenter Content(WCC.xls)中的版本的行為並不相同。
它會提示相同的消息,但是在忽略提示后,它會顯示垃圾字符,並且如果我將WCC.xls的擴展名更改為WCC.xlsx,則顯示會很好。
我該怎么辦才能在運行時識別這種情況,我們將不勝感激。
這是我本地的代碼片段
InputStream initialStream;
try {
initialStream = new FileInputStream(
new File("C:\\Users\\xxxxxx\\Desktop\\Utils\\YYYYY\\FN1.xls"));
FileOutputStream oStream = null;
oStream = new FileOutputStream("C:\\Users\\xxxxxx\\Desktop\\Utils\\YYYYY\\WCC1.xls");
FilenetConnectionUtil fCU = new FilenetConnectionUtil();
fCU. writeFileToTempLocation("xx","xx",initialStream,oStream);
oStream.flush();
oStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/* Write Method*/
public void writeFileToTempLocation(String filename, String filepath, InputStream inStream,FileOutputStream oStream) throws IOException {
// FileOutputStream oStream = new FileOutputStream(filepath);
byte[] buffer = new byte[1024];
int n;
if(inStream != null) {
while ((n = inStream.read(buffer)) != -1) {
oStream.write(buffer);
}
}
oStream.flush();
}
謝謝,Rahul Dumpala
由於.xslx是zip格式,因此前兩個字節(魔術cookie)應為“ PK”。
Path path = Paths.get(
"C:\\Users\\xxxxxx\\Desktop\\Utils\\YYYYY\\FN1.xls"));
// Or: Path path = file.toPath();
boolean isXlsx(Path path) {
try (InputStream in = Files.newInputStream(path)) {
byte[] magicCookie = new byte[2];
return in.read(magicCookie) == 2
&& magicCookie[0] == 'P'
&& magicCookie[1] == 'K';
} catch (IOException) {
return false;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.