zoukankan      html  css  js  c++  java
  • org.apache.commons.net.ftp.FTPClient上传文件大小改变的解决方法

    现象:今天在用org.apache.commons.net.ftp.FTPClient的storeFile方法时,发现上传小文件如几KB的情况下,文件是无损传输,但是当上传的文件是55M以及100多兆的时候,FTP上的服务器文件的大小就会增加。

    解决方法:在连接FTP服务器的时候,加上一段这个代码ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);

    示例:

    连接FTP服务器代码:

    public static FTPClient getFtpClient(String serverIP,String userName,String password,int port) throws SocketException, IOException{
             int reply;
                 FTPClient ftpClient = new FTPClient();
                 ftpClient.connect(serverIP, port);
                 reply = ftpClient.getReplyCode();
                
                if(!FTPReply.isPositiveCompletion(reply)){
                    ftpClient.disconnect();
                    System.out.println("登陆FTP失败");
                    return null;
                }
                ftpClient.login(userName, password);
                ftpClient.setDataTimeout(2000);
                ftpClient.enterLocalPassiveMode();
                ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
              return ftpClient;
        }

    上传文件方法的代码:

    /**
         * 上传文件到文件服务器中
         * @param file  需上传的文件
         * @param ftpClient  
         * @param workDirectory   FTP服务器的相对目录
         * @return
         * @throws IOException
         */
        public static String uploadFileToBBK(File file,FTPClient ftpClient,String workDirectory) throws IOException{
            
            ftpClient.setControlEncoding("UTF-8");
            ftpClient.makeDirectory(workDirectory);
            ftpClient.changeWorkingDirectory(workDirectory);
            BufferedInputStream  fiStream =new BufferedInputStream(new FileInputStream(file));
            boolean  flag = ftpClient.storeFile(new String((file.getName()).getBytes("UTF-8"),"iso-8859-1"),fiStream);
            fiStream.close();
            if(flag){
                return "OK";
            }else{
                return "";
            }
        }

    断开服务器连接方法的代码:

    public static void closeFtp(FTPClient ftpClient){
            if(ftpClient!=null && ftpClient.isConnected()){
                try {
                    boolean isLogOut = ftpClient.logout();
                    if(isLogOut){
                        System.out.println("成功关闭ftp连接");
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    System.out.println("关闭FTP服务器异常");
                }finally{
                    try {
                        ftpClient.disconnect();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        System.out.println("关闭服务器连接异常");
                    }
                }
                
            }
        }
  • 相关阅读:
    vmware Unable to open kernel device "\.Globalvmx86": The system cannot find the file 的解决方法
    nc和telnet配合使用
    linux下批量替换文件内容
    Linux动态库的导出控制
    goang Receiver & interface
    Go与C语言的互操作 cgo
    Go fsm
    Git多账号登陆
    mysql 安装与配置、使用
    Reverse Integer
  • 原文地址:https://www.cnblogs.com/xiangpiaopiao2011/p/2487579.html
Copyright © 2011-2022 走看看