【问题标题】:Using Java ByteArrayInputStream with File将 Java ByteArrayInputStream 与文件一起使用
【发布时间】:2016-09-19 09:50:07
【问题描述】:

我有这个代码:

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 转换为字节或时遇到问题?

【问题讨论】:

  • 您确定输入文件不为空吗?顺便说一句,您可以使用final InputStream is = new BufferedInputStream(new FileInputStream(file)) 而无需读取内存中的整个文件。
  • 是的,输入不为空,但让我试试你的建议

标签: java spring ftp


【解决方案1】:

使用这个代码:

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;
    }

【讨论】:

    猜你喜欢
    • 2011-09-24
    • 2011-04-14
    • 2015-12-11
    • 2010-09-19
    • 2017-10-04
    • 2013-02-19
    • 2014-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多