上传附件到ftp服务器,显示上传成功但是服务器上面没有这个附件

 因为我们这个写的人多,之前能正常上传上去后来一段时间有时候可以有时候不行,然后排查了好久,最后发现问题是登录的位置写错了,把登录服务器那行代码挪上去就可以

例如:修改前

        String server = "ftp.example.com";
        int port = 21;
        String user = "username";
        String pass = "password";
 
        FTPClient ftp = new FTPClient();
        try {
            ftp.connect(server, port);
        
            
            // 二进制文件传输
            ftp.enterLocalPassiveMode();
            ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
            
            // 上传文件的路径
            String remoteFilePath = "/path/to/remote/wjj";//你需要上传到的服务器的目录地址
            String localFilePath = "/path/to/local/file.txt";//需要上传的附件地址
            ftp.login(user, pass);
            // 使用Apache Commons Net库的API上传文件
            boolean success = ftp.storeFile(remoteFilePath, new 
            FileInputStream(localFilePath));
            if (success) {
                System.out.println("文件上传成功");
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (ftp.isConnected()) {
                    ftp.logout();
                    ftp.disconnect();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

修改后:

        String server = "ftp.example.com";
        int port = 21;
        String user = "username";
        String pass = "password";
 
        FTPClient ftp = new FTPClient();
        try {
            ftp.connect(server, port);
            ftp.login(user, pass);//把这个从原来的地方挪上来就好了
          
            // 二进制文件传输
            ftp.enterLocalPassiveMode();
            ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
            
            // 上传文件的路径
            String remoteFilePath = "/path/to/remote/file.txt";
            String localFilePath = "/path/to/local/file.txt";
          
            // 使用Apache Commons Net库的API上传文件
            boolean success = ftp.storeFile(remoteFilePath, new FileInputStream(localFilePath));
            if (success) {
                System.out.println("文件上传成功");
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (ftp.isConnected()) {
                    ftp.logout();
                    ftp.disconnect();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Python3将文件从一个服务器输到另一个服务器,可以使用paramiko模块实现。这个模块允许你使用SSH协议连接到远程服务器并执行文件输操作。 以下是实现文件输的简单示例代码: ``` import paramiko # 设置源和目标服务器的主机名、用户名和密码 src_hostname = 'source_server' src_username = 'source_username' src_password = 'source_password' dst_hostname = 'destination_server' dst_username = 'destination_username' dst_password = 'destination_password' # 创建SSH客户端连接到源服务器 src_ssh = paramiko.SSHClient() src_ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) src_ssh.connect(src_hostname, username=src_username, password=src_password) # 创建SSH客户端连接到目标服务器 dst_ssh = paramiko.SSHClient() dst_ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) dst_ssh.connect(dst_hostname, username=dst_username, password=dst_password) # 创建SFTP客户端连接到源服务器 src_sftp = src_ssh.open_sftp() # 在源服务器上打开要输的文件 local_path = '/path/to/local/file' remote_path = '/path/to/remote/file' src_file = src_sftp.open(local_path, 'rb') # 在目标服务器上打开要输的文件 dst_sftp = dst_ssh.open_sftp() dst_file = dst_sftp.open(remote_path, 'wb') # 从源服务器读取数据并写入目标服务器 dst_file.write(src_file.read()) # 关闭文件和SFTP连接 src_file.close() dst_file.close() src_sftp.close() dst_sftp.close() # 关闭SSH连接 src_ssh.close() dst_ssh.close() ``` 这个示例代码使用paramiko模块连接到源服务器和目标服务器,并使用SFTP协议进行文件输。在示例代码中,你需要将`src_hostname`、`src_username`、`src_password`、`dst_hostname`、`dst_username`和`dst_password`设置为源服务器和目标服务器的实际信息,将`local_path`和`remote_path`设置为要输的文件的本地路径和远程路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值