ユーザーが圧縮ファイル (.7z または .zip) をアップロードできるプログラムを作成する必要があります。このファイルを保存する必要がないので、その内容を読み取って拡張子を確認する必要があります。この時点まで、私は到達しました。ファイルが .xls ファイルの場合、このファイルを 'HSSFWorkbook' オブジェクトに渡し、特定のシートの内容を確認します。これは私が書いたコードです:
private void getNumberOfItemsInArchive(FormFile uploadedFile) throws Exception
{
File archiveFilename = new File(uploadedFile.getFileName());
OutputStream os = new FileOutputStream(archiveFilename);
InputStream is = new BufferedInputStream(uploadedFile.getInputStream());
int count;
byte buf[] = new byte[4096];
while ((count = is.read(buf)) > -1)
{
os.write(buf, 0, count);
}
RandomAccessFile randomAccessFile = null;
ISevenZipInArchive inArchive = null;
try
{
randomAccessFile = new RandomAccessFile(uploadedFile.getFileName(), "r");
logger.info("After generating randomAccessFile"+randomAccessFile);
inArchive = SevenZip.openInArchive(null, // autodetect archive type
new RandomAccessFileInStream(randomAccessFile));
ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface();
logger.info("Count of items in archive: "+inArchive.getNumberOfItems());
logger.info("-----------------------------");
for (ISimpleInArchiveItem item : simpleInArchive.getArchiveItems())
{
String filename = item.getPath();
logger.info("Filename::"+filename);
String extension = filename.substring(filename.lastIndexOf(".") + 1, filename.length());
String excel = "xls";
if (extension.matches(excel) && inArchive.getNumberOfItems() == 1)
{
logger.info("Valid file");
// what should be written here?
read(??,"63","247");
}
else
{
logger.info("Invalid File");
}
}
.
.
.
.
}
public boolean read(FileInputStream inputFile,String appid,String templateid) throws IOException
{
logger.info("Inside Excel reader class"+ inputFile);
.
.
.
if(versionFlag.equals("true"))
{
try
{
// code needed for here
HSSFWorkbook workBook = new HSSFWorkbook(inputFile);
logger.info("the file path in work book is"+workBook);
int numberOfSheets=workBook.getNumberOfSheets();
logger.info("the number of sheets are"+numberOfSheets);
HSSFSheet newSheet=workBook.getSheet("VersionDetails");
if(newSheet==null)
{
result=false;
}
logger.info("the file path in newsheet is"+newSheet);
//int y=newSheet.getPhysicalNumberOfRows();
HSSFRow row=newSheet.getRow(17);
int numberofcell=row.getPhysicalNumberOfCells();
.
.
.
}
}