FTP上传中文文件到中文路径

package com.wq.test;
import java.io.File;    
import java.io.FileInputStream;    


import org.apache.commons.net.ftp.FTPClient;    
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPReply;  


public class upftp {


  
    private  FTPClient ftp;      
    /**  
     *   
     * @param path 上传到ftp服务器哪个路径下     
     * @param addr 地址  
     * @param port 端口号  
     * @param username 用户名  
     * @param password 密码  
     * @return  
     * @throws Exception  
     */    
    private  boolean connect(String path,String addr,int port,String username,String password) throws Exception {      
        boolean result = false;      
        ftp = new FTPClient();      
        int reply;      
        ftp.connect(addr,port);      
        ftp.login(username,password);      
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE);      
        reply = ftp.getReplyCode();      
        if (!FTPReply.isPositiveCompletion(reply)) {      
            ftp.disconnect();      
            return result;      
        }      
        ftp.changeWorkingDirectory(path);      
        result = true;      
        return result;      
    }      
    /**  
     *   
     * @param file 上传的文件或文件夹  
     * @throws Exception  
     */    
    private void upload(File file) throws Exception{      
        if(file.isDirectory()){           
            ftp.makeDirectory(file.getName());                
            ftp.changeWorkingDirectory(file.getName()); 
            String[] files = file.list();             
            for (int i = 0; i < files.length; i++) {      
                File file1 = new File(file.getPath()+"\\"+files[i] );      
                if(file1.isDirectory()){
                    upload(file1);      
                    ftp.changeToParentDirectory();      
                }else{                    
                    File file2 = new File(file.getPath()+"\\"+files[i]);
                    System.out.println(files[i]);
                    System.out.println(file2.getName());
                    FileInputStream input = new FileInputStream(file2);
                    String name;
                    name=new String(file2.getName().getBytes("GBK"),"iso-8859-1");// 转换后的目录名或文件名。
                    ftp.storeFile(name, input);      
                    input.close();                            
                }                 
            }      
        }else{      
            File file2 = new File(file.getPath());      
            FileInputStream input = new FileInputStream(file2);      
            ftp.storeFile(file2.getName(), input);      
            input.close();        
        }      
    }      
   public static void main(String[] args) throws Exception{    
  upftp t = new upftp();   
  System.out.println(1111111);
  String name="公司文档/献血服务发展部/WeChat/Realse/V2.1.0.0";
  name=new String(name.getBytes("GBK"),"iso-8859-1");// 转换后的目录名或文件名。
  System.out.println(name);
      t.connect(name, "172.0.0.34", 21, "0063", "111111");    
      File file = new File(args[0]);    
      t.upload(file);    
   }    


}


关键信息,所有中文都转码就ok了

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可能是因为你在上传文件之前没有创建文件对象,或者没有正确设置上传文件路径。以下是一个简单的Java FTP上传文件的示例代码,可以参考一下: ```java import java.io.*; import org.apache.commons.net.ftp.*; public class FTPUpload { public static void main(String[] args) { String server = "ftp.example.com"; int port = 21; String user = "username"; String pass = "password"; String localFilePath = "C:\\myfiles\\file.txt"; String remoteFilePath = "/path/to/remote/file.txt"; try { FTPClient ftpClient = new FTPClient(); ftpClient.connect(server, port); ftpClient.login(user, pass); ftpClient.enterLocalPassiveMode(); File localFile = new File(localFilePath); InputStream inputStream = new FileInputStream(localFile); ftpClient.makeDirectory(remoteFilePath); ftpClient.changeWorkingDirectory(remoteFilePath); ftpClient.storeFile(localFile.getName(), inputStream); inputStream.close(); ftpClient.logout(); ftpClient.disconnect(); System.out.println("File uploaded successfully."); } catch (IOException ex) { System.out.println("Error: " + ex.getMessage()); ex.printStackTrace(); } } } ``` 在这个示例中,我们首先建立与FTP服务器的连接,然后登录并进入被动模式。接下来,我们创建本地文件对象和输入流,然后使用FTPClient的makeDirectory()方法创建远程文件夹。注意,如果文件夹已经存在,就不需要再次创建。然后,我们使用FTPClient的changeWorkingDirectory()方法切换到远程文件夹,并使用storeFile()方法上传本地文件。最后,我们关闭输入流、注销并断开连接。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值