imoteb imoteb - 2 months ago 9
Java Question

Using Java ByteArrayInputStream with File

I have this code:

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


The parameter I got in this case
File
I want to upload to some FTP, but the problem each time I do this the file actually is empty. When I try for example
final ByteArrayInputStream stream = new ByteArrayInputStream("Text here".getBytes());
it is working fine, and stores the information inside the file, what could be the problem here, may I have problem maybe with converting the
File
to bytes or ?

Answer

Use thsi code :

public List uploadFTPFilesByCridational(List processedFiles, String sourceDir, String destinationPath, String hostName, String userName, String password, String portNo, String extation, int fileHours, int fileMint) { List 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;
}
Comments