【问题标题】:File Overwrite issue when trying to transfer file using FTP尝试使用 FTP 传输文件时出现文件覆盖问题
【发布时间】:2012-08-06 13:18:32
【问题描述】:

我有一个只有 List 和 Put 权限的 FTP 服务器。但没有删除、覆盖和重命名权限。

现在,当我尝试使用以下实现使用简单 FTP 传输文件时

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

在上面的代码中,文件是使用文件对象实例在远程创建的。后来我试图用缓冲流写文件。在这里,系统就像正在写入已经创建的文件一样,并且我的服务器没有任何覆盖权限,引发以下错误。

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)

请告诉我如何使用文件对象处理文件传输,这样文件创建和写入流应该同时发生。

【问题讨论】:

    标签: ftp ftp-client splfileobject


    【解决方案1】:

    我已经解决了这个问题。

    它非常简单。在下面的代码中

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

    我首先使用 FileObject 创建一个文件,然后尝试将 BOS 写入文件。

    在将 BOS 写入文件系统时,我们认为我们正在尝试将数据添加到已经存在的文件中(因为我分两个单独的步骤来执行此操作,创建文件并将数据写入相同的文件)并返回错误

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

    我已经删除了

    fo.createFile()
    

    因为 BOS 在写入数据时会以任何方式创建一个文件(如果不可用)。

    感谢您的宝贵时间。
    Purushotham 雷迪

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-26
      • 1970-01-01
      相关资源
      最近更新 更多