zoukankan      html  css  js  c++  java
  • java ftp上载下传 遇到的问题

    1、下载文件中文乱码,和下载文件大小为0kb

     1 /**
     2      * Description: 从FTP服务器下载文件
     3      * 
     4      * @param url
     5      *            FTP服务器hostname
     6      * @param port
     7      *            FTP服务器端口
     8      * @param username
     9      *            FTP登录账号
    10      * @param password
    11      *            FTP登录密码
    12      * @param remotePath
    13      *            FTP服务器上的相对路径
    14      * @param fileName
    15      *            要下载的文件名
    16      * @param localPath
    17      *            下载后保存到本地的路径
    18      * @return
    19      */
    20     public static boolean downFile(String url, int port, String username,
    21             String password, String remotePath, String fileName,
    22             String localPath) {
    23         boolean success = false;
    24         FTPClient ftp = new FTPClient();
    25         try {
    26             int reply;
    27             ftp.connect(url, port);
    28             // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
    29             ftp.login(username, password);// 登录
    30             reply = ftp.getReplyCode();
    31             if (!FTPReply.isPositiveCompletion(reply)) {
    32                 ftp.disconnect();
    33                 return success;
    34             }
    35             //这里的remotePath一定要转码:remotePath = new String("/统计预报文件夹".getBytes("GBK"),"ISO-8859-1");
    36             ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
    37 
    38             FTPFile[] fs = ftp.listFiles();
    39             for (FTPFile ff : fs) {
    40                 ftp.setControlEncoding("gbk");
    41                 String f = new String(ff.getName().getBytes("iso-8859-1"),"gbk");
    42                 if (f.equals(fileName)) {
    43                     File localFile = new File(localPath + File.separatorChar + f);
    44                     OutputStream os = new FileOutputStream(localFile);
    45                     ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
    46                     //设置文件名为编码,如果写:ftp.retrieveFile(f, os); 下载中文文件时大小为0kb
    47                     ftp.retrieveFile(new String(f.getBytes("gbk"),"iso-8859-1"), os);
    48                     os.close();
    49                     success = true;
    50                 }
    51             }
    52 
    53             ftp.logout();
    54         } catch (IOException e) {
    55             e.printStackTrace();
    56         } finally {
    57             if (ftp.isConnected()) {
    58                 try {
    59                     ftp.disconnect();
    60                 } catch (IOException ioe) {
    61                 }
    62             }
    63         }
    64         return success;
    65     }

    2、文件上传ftp服务器,没有上传到指定 目录

     FTP服务器保存目录:path不能有:/,如果有就上传到根目录
     1 /**
     2      * Description: 向FTP服务器上传文件
     3      * 
     4      * @Version 1.0
     5      * @param url
     6      *            FTP服务器hostname
     7      * @param port
     8      *            FTP服务器端口
     9      * @param username
    10      *            FTP登录账号
    11      * @param password
    12      *            FTP登录密码
    13      * @param path
    14      *            FTP服务器保存目录:path不能有:/,如果有就上传到根目录
    15      * @param filename
    16      *            上传到FTP服务器上的文件名
    17      * @param input
    18      *            输入流
    19      * @return 成功返回true,否则返回false *
    20      */
    21     public static boolean uploadFile(String url,// FTP服务器hostname
    22             int port,// FTP服务器端口
    23             String username, // FTP登录账号
    24             String password, // FTP登录密码
    25             String path, // FTP服务器保存目录
    26             String filename, // 上传到FTP服务器上的文件名
    27             InputStream input // 输入流
    28     ) {
    29         boolean success = false;
    30         FTPClient ftp = new FTPClient();
    31         ftp.setControlEncoding("GBK");
    32         try {
    33             int reply;
    34             ftp.connect(url, port);// 连接FTP服务器
    35             // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
    36             ftp.login(username, password);// 登录
    37             reply = ftp.getReplyCode();
    38             if (!FTPReply.isPositiveCompletion(reply)) {
    39                 ftp.disconnect();
    40                 return success;
    41             }
    42             ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
    43             ftp.makeDirectory(path);
    44             ftp.changeWorkingDirectory(path);
    45             ftp.storeFile(filename, input);
    46 
    47             input.close();
    48             ftp.logout();
    49             success = true;
    50         } catch (IOException e) {
    51             e.printStackTrace();
    52         } finally {
    53             if (ftp.isConnected()) {
    54                 try {
    55                     ftp.disconnect();
    56                 } catch (IOException ioe) {
    57                 }
    58             }
    59         }
    60         return success;
    61     }
  • 相关阅读:
    Pymsql
    MySQL基础操/下
    MySQL基础操作
    前端学习之jquery/下
    前端学习之jquery
    Python之异常处理
    Python之模块和包导入
    Python之模块
    Python之面向对象上下文管理协议
    Python之面向对象slots与迭代器协议
  • 原文地址:https://www.cnblogs.com/kingxiaozi/p/4708305.html
Copyright © 2011-2022 走看看