zoukankan      html  css  js  c++  java
  • ftp下载乱码问题

    转载  出处:http://blog.csdn.net/u010166206/article/details/47000729 

    在网上Google了一些资料, FTP协议里面,规定文件名编码为iso-8859-1,所以目录名或文件名需要转码。

         所以网上很多人的解决方法为: 

                      将中文的目录或文件名转为iso-8859-1编码的字符。参考代码:

                      String name="目录名或文件名";

                      name=new String(name.getBytes("GBK"),"iso-8859-1");

               很多人改为上述操作后,发现上传后中文不再乱码了,就以为解决了问题

               还有人处理方法为:

                     ftpClient.setControlEncoding("GBK");

                     FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);

                     conf.setServerLanguageCode("zh");  

    上述的处理方法,我都试过,发现在我自己搭建的FTP服务器上,上传的文件中文是正常的,不是乱码,我当时以为中文问题就解决了,在将文件上传到其他搭建的FTP服务器上时,文件中文路径仍然是乱码,所以上述的解决方法是错误的。 

              上面的方法之所以错误的原因是因为是没有考虑ftp服务器的编码格式。我搭建的Ftp服务器(windows2003 server)支持GBK编码方式,所以上述的解决方法可以,但是测试部的同事搭建的Ftp服务器(serv-u)是支持UTF-8格式的,所以此时在客户端的编码方式是GBK的,而搭设的ftp服务器中的设置就已经是utf-8的编码,所以肯定还是会出现乱码的问题。

             那么正确的解决方法时是什么呢,我们可以仿照FlashFXP、FileZilla等ftp 客户端连接工具,看看他们是如何实现的,下面的两张图就是FileZilla的配置信息和连接时的命令信息。

       

                                                                                  

                图1:FileZilla配置信息 

                                                                                        图2:FileZilla连接时信息 

    从图2中我们可以看到原来它向服务器发送了OPTS UTF8 ON命令,来开启服务器对UTF-8的支持。所以我们也可以仿照FileZilla那样向服务器发送该命令。如果服务器支持UTF-8我们就用UTTF-8,否则我们就用本地编码(GBK)来处理中文文件名。

    下面是Java代码:

    /** 本地字符编码 */
    private static String LOCAL_CHARSET = "GBK";
     
    // FTP协议里面,规定文件名编码为iso-8859-1
    private static String SERVER_CHARSET = "ISO-8859-1";
     
    private void connectFtpServer() {
    if (ftpClient == null) {
    ftpClient = new FTPClient();
    }
    if (ftpClient.isConnected()) {
    return;
    }
    String host = getConfigValue(ADDRESS);
    int port = Integer.valueOf(getConfigValue(PORT));
    String user = getConfigValue(USER);
    String password = getConfigValue(PASSWORD);
    try {
    ftpClient.connect(host, port);
    if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
    if (ftpClient.login(user, password)) {
    if (FTPReply.isPositiveCompletion(ftpClient.sendCommand(
    "OPTS UTF8", "ON"))) {// 开启服务器对UTF-8的支持,如果服务器支持就用UTF-8编码,否则就使用本地编码(GBK).
    LOCAL_CHARSET = "UTF-8";
    }
    ftpClient.setControlEncoding(LOCAL_CHARSET);
    ftpClient.enterLocalPassiveMode();// 设置被动模式
    ftpClient.setFileType(getTransforModule());// 设置传输的模式
    return;
    } else {
    throw new FileStorageException(
    "Connet ftpServer error! Please check user or password");
    }
    }
    } catch (IOException e) {
    disConnectServer();
    throw new FileStorageException(
    "Connet ftpServer error! Please check the Configuration");
    }
    }
    上传文件时,文件名称需要做编码转换
    fileName = new String(fileName.getBytes(LOCAL_CHARSET),
    SERVER_CHARSET);

    通过上述方法,就能解决了中文乱码的问题

    -----------------------------------------------------------------------------以上为转载,出处:http://www.linuxidc.com/Linux/2014-10/107585.htm-----------------

    servlet+jsp用FileZilla Server服务器完成文件上传及下载。

    需注意的地方:上传:1.后台获取前台jsp页面选择的文件路径(包含中文)时,要对路径进行编码

    《部分代码》 String filePath = request.getParameter("fileName");
                String name = filePath.substring(filePath.lastIndexOf("\")+1);//获得文件名
               System.out.println(name+"-----------"+filePath);
                //为中文文件设置编码格式,正确解析
                name = new String(name.getBytes("iso-8859-1"),"utf-8");
                filePath = new String(filePath.getBytes("iso-8859-1"),"utf-8");

     File srcFile = new File(filePath); 
                fis = new FileInputStream(srcFile); 
                //设置上传目录 
                ftpClient.changeWorkingDirectory("/upload"); 
                ftpClient.setBufferSize(1024); 
                //设置文件类型(二进制) 
               ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); 
               ftpClient.storeFile(new String(name.getBytes("gbk"), "iso-8859-1") , fis); 


    // 下载文件的路径
    String path = localPath + "" + fileName;

    /*
      * 使用FTP下载:把FTP服务器上面的文件下到Tomcat工程目录下,指定下载路径。
      */
    try{
    ftp.connect("127.0.0.1");
    ftp.login("shmily", "shmily");
    String remoteFileName = "/Download/"+ "" + fileName;
    ftp.changeWorkingDirectory(remoteFileName);
    fos = new FileOutputStream(path); 
    ftp.retrieveFile(new String(remoteFileName.getBytes("gbk"), "iso-8859-1"), fos);
    }catch (IOException e) { 
                e.printStackTrace(); 
                throw new RuntimeException("FTP客户端出错!", e); 
            } finally { 
                IOUtils.closeQuietly(fos); 
                try { 
                    ftp.disconnect(); 
                } catch (IOException e) { 
                    e.printStackTrace(); 
                    throw new RuntimeException("关闭FTP连接发生异常!", e); 
                } 
            } 

  • 相关阅读:
    主机名与域名
    Screen 状态栏配置
    linux 修改时间
    vsftpd2.3.2安装、配置详解
    mysql 二进制日志
    linux下mysql操作的命令
    Apache加载PHP.ini顺序
    页面异步加载javascript文件
    一段实现页面上的图片延时加载的js
    如何在MySql中记录SQL日志
  • 原文地址:https://www.cnblogs.com/ZqNote/p/7667746.html
Copyright © 2011-2022 走看看