Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将Excel转换为CSV | SFTP |大文件_Java_Spring Boot_Java 8 - Fatal编程技术网

Java 将Excel转换为CSV | SFTP |大文件

Java 将Excel转换为CSV | SFTP |大文件,java,spring-boot,java-8,Java,Spring Boot,Java 8,我正在尝试将大型文件从Excel转换为CSV(200 MB+),但根据我的实现逻辑,我发现了性能问题和内存问题。任何从FTP读取大型excel文件并将CSV文件写回FTP的指针都将非常有用 代码摘要: 来源:Excel是从SFTP服务器读取的 目标:CSV文件被推送到SFTP目录 以下是转换器的实现: public class ExcelConvertServiceImpl implements ExcelConvertService { private static final Lo

我正在尝试将大型文件从Excel转换为CSV(200 MB+),但根据我的实现逻辑,我发现了性能问题和内存问题。任何从FTP读取大型excel文件并将CSV文件写回FTP的指针都将非常有用

代码摘要: 来源:Excel是从SFTP服务器读取的 目标:CSV文件被推送到SFTP目录

以下是转换器的实现:

public class ExcelConvertServiceImpl implements ExcelConvertService {

    private static final Logger logger = Logger.getLogger(ExcelConvertServiceImpl.class.getName());
    private StringBuffer data = new StringBuffer();
    private FTPOperationsImpl ftpOperationsImpl = new FTPOperationsImpl();
    private Map<String, StringBuffer> cSVFiles = new HashedMap<String, StringBuffer>();


    public void createCSV(Sheet sheet) throws HandledException {
        try {
            // Row row = null;
            Cell cell = null;
            int lastCellNum = 0;
            this.data.setLength(0);

            for (Row row : sheet) {
                lastCellNum = row.getLastCellNum() - 1;
                for (int cn = 0; cn <= lastCellNum; cn++) {
                    cell = row.getCell(cn, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                    this.data.append(StringEscapeUtils.escapeCsv(cell.toString().replaceAll("\\s+", "")));
                    if (cn != lastCellNum) {
                        this.data.append(",");
                    }
                }
                this.data.append("\n");
            }
        } catch (Exception ex) {
            logger.log(Level.SEVERE, null, ex);
            throw new HandledException(ex.getClass().getCanonicalName(), ex.getMessage(), ex);
        }
    }


    public Map<String, StringBuffer> readXLSX(InputStream inputStream, String fileName, ChannelSftp channelSftp)
            throws HandledException {
        try {
            cSVFiles.clear();
            String writeFileName = null;
            Workbook wb = WorkbookFactory.create(inputStream);
            for (int i = 0; i < wb.getNumberOfSheets(); i++) {
                logger.log(Level.INFO, "---------Reading Sheet : " + wb.getSheetAt(i).getSheetName() + "---------");
                createCSV(wb.getSheetAt(i));
                writeFileName = fileName.substring(0, fileName.lastIndexOf(".")) + "_" + wb.getSheetAt(i).getSheetName()
                        + ".csv";
                this.cSVFiles.put(writeFileName, this.data);
            }
            inputStream.close();
            wb.close();
            return cSVFiles;
            // uploadFilesToFTP(channelSftp);
        } catch (IOException ex) {
            logger.log(Level.SEVERE, "IOException", ex);
            throw new HandledException(ex.getClass().getCanonicalName(), ex.getMessage(), ex);
        } catch (Exception ex) {
            logger.log(Level.SEVERE, "Exception", ex);
            throw new HandledException(ex.getClass().getCanonicalName(), ex.getMessage(), ex);
        } finally {
            try {
                inputStream.close();
                return cSVFiles;
            } catch (IOException ex) {
                logger.log(Level.SEVERE, null, ex);
            }
        }
    }       
}
谢谢

public List<FileList> excelToCSVSingle(Request input) throws HandledException {
        List<FileList> fileList = new ArrayList<FileList>();
        logger.log(Level.INFO, "******************** Started Excel Coverter Instance ********************");
        ChannelSftp channelSftp = null;
        FTPOperations ftpOperations = new FTPOperationsImpl();
        try {
            channelSftp = this.fTPSessionService.createJSCHsession();
            channelSftp.cd(input.getfTPSourceDirectory());
            logger.log(Level.INFO, "---------FTP Directory : " + input.getfTPSourceDirectory() + "---------");
            //String sourceFileName = input.getFileList().stream().findFirst().get().getFileName();
            String sourceFileName = input.getFileName();
            Map<String, StringBuffer> cSVFiles = this.excelConvertService.readXLSX(
                    channelSftp.get(input.getfTPSourceDirectory() + sourceFileName), sourceFileName, channelSftp);
            ftpOperations.uploadFilesToFTP(cSVFiles, channelSftp, input.getfTPTargetDirectory());
            fileList.addAll(cSVFiles.keySet().stream().map(m -> new FileList(m)).collect(Collectors.toList()).stream().sequential().collect(Collectors.toList()));
            logger.log(Level.INFO, "---------Completed converting file : " + sourceFileName + "---------");

            logger.log(Level.INFO, "******************** Completed Excel Coverter Instance ********************");
            return fileList;
        } catch (Exception ex) {
            logger.log(Level.SEVERE, "Exception", ex);
            throw new HandledException(ex.getClass().getCanonicalName(), ex.getMessage(), ex);
        } finally {
            if (channelSftp != null) {
                this.fTPSessionService.disconnect(channelSftp);
            }
        }

    }
public void pushFileToFTP(StringBuffer data, String fileName, ChannelSftp channelSftp, String targetDirectory) {
        try {
            InputStream stream = new ByteArrayInputStream(data.toString().getBytes());
            String writeDir = targetDirectory + fileName;
            logger.log(Level.INFO,"---------Pushed file to FTP : " + writeDir);
            channelSftp.put(stream, writeDir);
        } catch (SftpException ex) {
            logger.log(Level.SEVERE, "SftpException", ex);
        } catch (Exception ex) {
            logger.log(Level.SEVERE, "Exception", ex);
        }
    }