File Overwrite issue when trying to transfer file using FTP

6k Views Asked by At

I have a FTP server with only List and Put permissions. But not having delete, overwrite and Rename permissions.

Now when I try to transfer a file using simple FTP using the following implementation

    private boolean sendFileStreamHelper(InputStream inputStream, String nameOfFileToStore, String filetransferDestFolder) throws FileTransferException {
    Log.info("Inside SendFile inputstream method to trasport the input stream of file " + nameOfFileToStore + " data to " + filetransferDestFolder);
    BufferedOutputStream os = null;
    FileObject fo = null;
    try {
        fo = getFileObject(nameOfFileToStore, filetransferDestFolder, ftpAuthDetails.getServerName(), ftpAuthDetails.getUsername(), ftpAuthDetails
                .getPassword(), ftpAuthDetails.getPort());
        fo.createFile();// create a file in the remote to transfer the file

        os = new BufferedOutputStream(fo.getContent().getOutputStream());

        FileUtil.readStream(inputStream, os);

        return true;
    } catch (Exception ex) {
        Log.error("File transfer exception occurred while transferrig the file " + nameOfFileToStore + " to " + filetransferDestFolder, ex);
        throw new FileTransferException(ex);
    } finally {
        if (os != null) {
            try {
                os.flush();
                os.close();
            } catch (IOException e) {
                Log.warn(getClass(), " Error while closing the buffer output stream", e);
            }
        }
        if (fo != null) {
            try {
                fo.close();
            } catch (IOException e) {
                Log.warn(getClass(), " Error while closing the File object", e);
            }
        }
        closeCache(); // Close the VFS Manager instance
    }
}

In the above code as the File is created in the remote using the File Object instance. Later to that I am trying to write the file with the Buffered stream. Here the systems acts as if it is writing to a file which is already created and as my server is not having any overwrite permission, throwing following error.

29 Jul 2012 21:03:06 [ERROR] FC_ClusteredScheduler_Worker-2(1) com.abc.filetransfer.FileTransferClient - .sendFileStreamHelper(FileTransferClient.java:170) - File transfer exception occurred while transferrig the file *******.txt to / ex-org.apache.commons.vfs2.FileSystemException: Could not write to "ftp://******:***@***.***.***.***/*********.txt"
org.apache.commons.vfs2.FileSystemException: Could not write to "ftp://******:***@***.***.***.***/*********.txt".
    at org.apache.commons.vfs2.provider.AbstractFileObject.getOutputStream(AbstractFileObject.java:1439)
    at org.apache.commons.vfs2.provider.DefaultFileContent.getOutputStream(DefaultFileContent.java:461)
    at org.apache.commons.vfs2.provider.DefaultFileContent.getOutputStream(DefaultFileContent.java:441)
    at com.abc.filetransfer.FileTransferClient.sendFileStreamHelper(FileTransferClient.java:164)
    at com.abc.filetransfer.FileTransferClient.sendFile(FileTransferClient.java:131)
    at com.abc.filetransfer.FileTransferClient.sendFile(FileTransferClient.java:103)
    at com.abc.filetransfer.client.FTPTransferClient.sendFile(FTPTransferClient.java:65)
Caused by: org.apache.commons.vfs2.FileSystemException: Cant open output connection for file "ftp://******:***@***.***.***.***/*********.txt".
 Reason: "**550 File unavailable. Overwrite not allowed by user profile**^M
    at org.apache.commons.vfs2.provider.ftp.FtpFileObject.doGetOutputStream(FtpFileObject.java:648)
    at org.apache.commons.vfs2.provider.AbstractFileObject.getOutputStream(AbstractFileObject.java:1431)

Please let me know how can I handle the file transfer using file Object, such that both File creation and writing the stream should happen at once.

1

There are 1 best solutions below

0
On

I have resolved the issue.

Its pretty straight forward. In the below code

        fo = getFileObject(nameOfFileToStore, filetransferDestFolder, ftpAuthDetails.getServerName(), ftpAuthDetails.getUsername(), ftpAuthDetails
            .getPassword(), ftpAuthDetails.getPort());
    fo.createFile();// create a file in the remote to transfer the file

    os = new BufferedOutputStream(fo.getContent().getOutputStream());

    FileUtil.readStream(inputStream, os);

I am creating a file first using the FileObject and then trying to write the BOS into the file.

Here while writing BOS to file system considers that we are trying to add data to a already existing file (as I am doing it in two separate steps, Creating a file and writing Data to the same) and returns the Error

**550 File unavailable. Overwrite not allowed by user profile*

I have removed the

fo.createFile()

as the BOS while writing the data will any way create a file if not available.

Thanks for your time.
Purushotham Reddy