在文件中使用Java ByteArrayInputStream

时间:2016-09-19 09:50:08

标签: java spring ftp

我有这段代码:

public void uploadToFTP(File file) {

    try {
        final ByteArrayInputStream stream = new ByteArrayInputStream(FileUtils.readFileToByteArray(file));
        String date = dateFormat.format(new Date());
        String filename = date.replaceAll(":", "-");
        sessionFactory.getSession().write(stream, "dir/" + filename + ".txt");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

我在这种情况下得到的参数File我想上传到某个FTP,但是每次我这样做时文件实际上都是空的。当我尝试例如final ByteArrayInputStream stream = new ByteArrayInputStream("Text here".getBytes());它工作正常,并将信息存储在文件中,这可能是什么问题,我可能有问题可能将File转换为字节或?

1 个答案:

答案 0 :(得分:0)

使用thsi代码:

public List<ProcessedFile> uploadFTPFilesByCridational(List<ProcessedFile> processedFiles, String sourceDir,
            String destinationPath, String hostName, String userName, String password, String portNo, String extation,
            int fileHours, int fileMint) {
        List<ProcessedFile> processedFilesList = new ArrayList<>();
        try {
            FTPClient ftpClient = new FTPClient();

            // client FTP connection
            ftpClient = connectToFTPClient(hostName, userName, password, portNo);

            // check if FTP client is connected or not
            if (ftpClient.isConnected()) {

                if (processedFiles != null && processedFiles.size() > 0) {
                    for (ProcessedFile processedFile : processedFiles) {
                        FileInputStream inputStream = null;
                        try {
                            File file = new File(sourceDir + "/" + processedFile.getOriginalFileName());
                            inputStream = new FileInputStream(file);

                            if (!ftpClient.isConnected()) {
                                ftpClient = connectToFTPClient(hostName, userName, password, portNo);
                            }

                            ByteArrayInputStream in = null;
                            try {
                                ftpClient.changeWorkingDirectory(destinationPath);
                                ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
                                ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
                                ftpClient.enterLocalPassiveMode();
                                in = new ByteArrayInputStream(FileUtils.readFileToByteArray(file));
                                ftpClient.storeFile(file.getName(), in);
                            } catch (Exception e) {
                                logger.error(e.getMessage());
                            }

                            inputStream.close();
                            in.close();

                            processedFile.setProcessedStatus(ProcessedStatus.COMPLETED);
                            processedFilesList.add(processedFile);
                        } catch (Exception e) {
                            logger.error(e);
                            processedFile.setProcessedStatus(ProcessedStatus.FAILED);
                            processedFilesList.add(processedFile);
                        }
                    }
                }
            }
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.logout();
                    ftpClient.disconnect();
                } catch (IOException e) {
                    logger.error(e.getMessage());
                } finally {
                    try {
                        ftpClient.disconnect();
                    } catch (Exception e) {
                        logger.error(e.getMessage());
                    }
                }
            }
        } catch (Exception e) {
            logger.error("FTP not connected exception: " + e);
        }
        return processedFilesList;
    }