java - Spring FTP throwing Server replied with 450 Requested file action not taken - Stack Overflow

admin2025-04-17  4

I'm trying to connect to an remote FTP server and trying to upload a file to the server but I get this error

Failed to write to 'testfile.txt'. Server replied with: 450 Requested file action not taken

Following is my code:

DynamicSessionFactory.java

public class DynamicFtpSessionFactory {

    private final ApplicationContext context;

    public DynamicFtpSessionFactory(@NonNull final ApplicationContext context) {
        this.context = context;
    }

    public void registerFtpSessionFactory(@NonNull final String beanName,
                                          @NonNull final String host,
                                          final int port,
                                          @NonNull final String username,
                                          @NonNull final String password) {
        final BeanDefinitionRegistry registry = (BeanDefinitionRegistry) ((AnnotationConfigApplicationContext) context).getBeanFactory();

        final BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(DefaultFtpSessionFactory.class);
        builder.addPropertyValue("host", host);
        builder.addPropertyValue("port", port);
        builder.addPropertyValue("username", username);
        builder.addPropertyValue("password", password);
        builder.addPropertyValue("fileType", FTP.ASCII_FILE_TYPE);

        registry.registerBeanDefinition(beanName, builder.getBeanDefinition());

        // Optionally wrap in a CachingSessionFactory
        final DefaultFtpSessionFactory ftpSessionFactory = (DefaultFtpSessionFactory) context.getBean(beanName);
        registry.registerBeanDefinition(beanName, BeanDefinitionBuilder.genericBeanDefinition(CachingSessionFactory.class)
                .addConstructorArgValue(ftpSessionFactory)
                .getBeanDefinition());
    }
}

Service:

public void uploadFileForGroupId(@NonNull final String groupId, @NonNull final Resource reportFile) throws IOException {
        final CachingSessionFactory session = getFtpSessionFactory(groupId);

        // Upload report file to FTP server
        try (InputStream inputStream = reportFile.getInputStream()) {
            session.getSession().write(inputStream, reportFile.getFilename());
            log.info("File {} uploaded successfully for group Id {}.", reportFile.getFilename(), groupId);
        } catch (IOException e) {
            log.error("Failed to upload file: ", e);
        } finally {
            session.getSession().close();
        }
    }

private CachingSessionFactory getFtpSessionFactory(@NonNull final String groupId) {
        final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.refresh();  // Initialize the context without any configuration class
        final String ftpBeanName = groupId + "Caching";
        if (context.containsBean(ftpBeanName)) {
            return context.getBean(ftpBeanName, CachingSessionFactory.class);
        }
        final DynamicFtpSessionFactory dynamicFtpSessionFactory = new DynamicFtpSessionFactory(context);

        final AnfDirectoryEntity anfDirectory = getFtpConnectionDetails(groupId);
        dynamicFtpSessionFactory.registerFtpSessionFactory(ftpBeanName,
                anfDirectory.getSftpConnection().getSftpServer(),
                FTP_PORT,
                anfDirectory.getSftpConnection().getSftpUser(),
                anfDirectory.getSftpConnection().getSftpPassword());

        return context.getBean(ftpBeanName, CachingSessionFactory.class);
}

I was able to upload file using ftp command from my machine to the FTP server. So, if I'm able to upload through FTP command, why not from java application?

I'm trying to connect to an remote FTP server and trying to upload a file to the server but I get this error

Failed to write to 'testfile.txt'. Server replied with: 450 Requested file action not taken

Following is my code:

DynamicSessionFactory.java

public class DynamicFtpSessionFactory {

    private final ApplicationContext context;

    public DynamicFtpSessionFactory(@NonNull final ApplicationContext context) {
        this.context = context;
    }

    public void registerFtpSessionFactory(@NonNull final String beanName,
                                          @NonNull final String host,
                                          final int port,
                                          @NonNull final String username,
                                          @NonNull final String password) {
        final BeanDefinitionRegistry registry = (BeanDefinitionRegistry) ((AnnotationConfigApplicationContext) context).getBeanFactory();

        final BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(DefaultFtpSessionFactory.class);
        builder.addPropertyValue("host", host);
        builder.addPropertyValue("port", port);
        builder.addPropertyValue("username", username);
        builder.addPropertyValue("password", password);
        builder.addPropertyValue("fileType", FTP.ASCII_FILE_TYPE);

        registry.registerBeanDefinition(beanName, builder.getBeanDefinition());

        // Optionally wrap in a CachingSessionFactory
        final DefaultFtpSessionFactory ftpSessionFactory = (DefaultFtpSessionFactory) context.getBean(beanName);
        registry.registerBeanDefinition(beanName, BeanDefinitionBuilder.genericBeanDefinition(CachingSessionFactory.class)
                .addConstructorArgValue(ftpSessionFactory)
                .getBeanDefinition());
    }
}

Service:

public void uploadFileForGroupId(@NonNull final String groupId, @NonNull final Resource reportFile) throws IOException {
        final CachingSessionFactory session = getFtpSessionFactory(groupId);

        // Upload report file to FTP server
        try (InputStream inputStream = reportFile.getInputStream()) {
            session.getSession().write(inputStream, reportFile.getFilename());
            log.info("File {} uploaded successfully for group Id {}.", reportFile.getFilename(), groupId);
        } catch (IOException e) {
            log.error("Failed to upload file: ", e);
        } finally {
            session.getSession().close();
        }
    }

private CachingSessionFactory getFtpSessionFactory(@NonNull final String groupId) {
        final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.refresh();  // Initialize the context without any configuration class
        final String ftpBeanName = groupId + "Caching";
        if (context.containsBean(ftpBeanName)) {
            return context.getBean(ftpBeanName, CachingSessionFactory.class);
        }
        final DynamicFtpSessionFactory dynamicFtpSessionFactory = new DynamicFtpSessionFactory(context);

        final AnfDirectoryEntity anfDirectory = getFtpConnectionDetails(groupId);
        dynamicFtpSessionFactory.registerFtpSessionFactory(ftpBeanName,
                anfDirectory.getSftpConnection().getSftpServer(),
                FTP_PORT,
                anfDirectory.getSftpConnection().getSftpUser(),
                anfDirectory.getSftpConnection().getSftpPassword());

        return context.getBean(ftpBeanName, CachingSessionFactory.class);
}

I was able to upload file using ftp command from my machine to the FTP server. So, if I'm able to upload through FTP command, why not from java application?

Share asked Jan 31 at 12:43 pri05pri05 993 silver badges15 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

450 is a FTP response code that indicates that a transient (4xxx) filesystem (x5x) error has occured. It's hard to say what the underlying problem is- could be a file permission thing on the server, or it ran out of space. I don't think your code is the problem, though it's a little hard to say since we don't know what's the implementation of that ftp client library.

转载请注明原文地址:http://anycun.com/QandA/1744861440a88656.html