传输到ftp服务器时文件内容会减少

时间:2017-08-23 08:36:56

标签: java binary-data ftp-client

我编写了一个java程序,使用Apache.commons.net库以递归方式将目录上传到远程ftp服务器。通过程序将所有文件按预期上传到远程ftp服务器。但是一些html,js,svg,ftl文件内容越来越少。还有一些jpeg,png,图标文件大小与原来不同。我使用meld软件上传后比较原始目录和上传的目录,以便我可以弄清楚目录内容是否已损坏。我已经尝试了BINARY和ASCII传输模式。两种模式都有同样的问题。任何人都可以提出这种情况发生的原因。这是ftp文件传输的问题

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;




public class FtpClient3 {

public static boolean uploadSingleFile(FTPClient ftpClient,
                                       String localFilePath, String   remoteFilePath) throws IOException {
    File localFile = new File(localFilePath);

    InputStream inputStream = new FileInputStream(localFile);
    String ext1 = FilenameUtils.getExtension(localFilePath);
    try {
        if(ext1== "ajx" || ext1== "am" || ext1=="asa" ||     ext1=="properties" || ext1=="asc" || ext1=="asp" || ext1=="aspx" || ext1=="awk"  || ext1=="bat" || ext1=="c" || ext1=="cdf" || ext1=="cf" || ext1=="cfg" || ext1=="cfm" || ext1=="cgi" || ext1=="cnf" || ext1=="conf" || ext1=="cpp" || ext1=="css" || ext1=="csv" || ext1=="ctl" || ext1=="dat" || ext1=="dhtml"|| ext1=="diz" || ext1=="file" || ext1=="forward" || ext1=="grp" || ext1=="h" || ext1=="hpp"|| ext1=="hqx" || ext1=="hta" || ext1=="htaccess" || ext1=="htc" || ext1=="htm" || ext1=="html" || ext1=="htpasswd" || ext1=="htt" || ext1=="htx"|| ext1=="in"|| ext1=="inc"|| ext1=="info"|| ext1=="ini"|| ext1=="ink"|| ext1=="java" || ext1=="js" || ext1=="jsp" || ext1=="log" || ext1=="logfile"|| ext1=="m3u"|| ext1=="m4a"|| ext1=="m4a"|| ext1=="mak"|| ext1=="map"|| ext1=="model"|| ext1=="msg"|| ext1=="nfo"|| ext1=="nsi" || ext1=="info"  || ext1=="old" || ext1=="pas" || ext1=="patch" || ext1=="perl" || ext1=="php"|| ext1=="php2"|| ext1=="php3" || ext1=="php4" || ext1=="php5" || ext1=="php6" || ext1=="phtml" || ext1=="pix" || ext1=="pl" || ext1=="pm" || ext1=="po"|| ext1=="pwd"|| ext1=="py" || ext1=="qmail" || ext1=="rb" || ext1=="rbl" || ext1=="rbw" || ext1=="readme" || ext1=="reg" || ext1=="rss" || ext1=="ruby" || ext1=="session" || ext1=="setup" || ext1=="sh" || ext1=="shtm"  || ext1=="shtml" || ext1=="sql" || ext1=="ssh" || ext1=="stm" || ext1=="style" || ext1=="svg" || ext1=="tcl" || ext1=="text" || ext1=="threads" || ext1=="tmpl" || ext1=="tpl" || ext1=="txt" || ext1=="ubb" || ext1=="vbs" || ext1=="xhtml" || ext1=="xml" || ext1=="xrc" || ext1=="xsl" || ext1=="rtf"){
        ftpClient.setFileType(FTP.ASCII_FILE_TYPE);
        ftpClient.setFileTransferMode(ftpClient.ASCII_FILE_TYPE);
            }
        else{
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            ftpClient.setFileTransferMode(ftpClient.BINARY_FILE_TYPE);
        }


        return ftpClient.storeFile(remoteFilePath, inputStream);
    } catch (Exception e) {
        System.out.println("error: " + e.getMessage());
        e.printStackTrace();
        throw e;
    } finally {
        inputStream.close();
    }
}







public static void uploadDirectory(FTPClient ftpClient,
                                   String remoteDirPath, String  localParentDir, String remoteParentDir)
        throws IOException {

    System.out.println("LISTING directory: " + localParentDir);

    File localDir = new File(localParentDir);
    File[] subFiles = localDir.listFiles();
    if (subFiles != null && subFiles.length > 0) {
        for (File item : subFiles) {
            String remoteFilePath = remoteDirPath + "/" + remoteParentDir
                    + "/" + item.getName();
            if (remoteParentDir.equals("")) {
                remoteFilePath = remoteDirPath + "/" + item.getName();
            }


            if (item.isFile()) {
                // upload the file
                String localFilePath = item.getAbsolutePath();
                System.out.println("About to upload the file: " + localFilePath);
                //CHANGING
                boolean uploaded = uploadSingleFile(ftpClient,
                        localFilePath, remoteFilePath);
                if (uploaded) {
                    System.out.println("UPLOADED a file to: "
                            + remoteFilePath);
                } else {
                    System.out.println("COULD NOT upload the file: "
                            + localFilePath);
                }
            } else {
                // create directory on the server
                boolean created = ftpClient.makeDirectory(remoteFilePath);
                if (created) {
                    System.out.println("CREATED the directory: "
                            + remoteFilePath);
                } else {
                    System.out.println("COULD NOT create the directory: "
                            + remoteFilePath);
                }

                // upload the sub directory
                String parent = remoteParentDir + "/" + item.getName();
                if (remoteParentDir.equals("")) {
                    parent = item.getName();
                }

                localParentDir = item.getAbsolutePath();
                uploadDirectory(ftpClient, remoteDirPath, localParentDir,
                        parent);
            }
        }
    }
}






public static void main(String args[])throws Exception{

    String server = "";
    int port = 21;
    String user = "";
    String pass = "";




    FTPClient ftpclient = new FTPClient();


   try {
        // connect and login to the server
        ftpclient.connect(server, port);
        ftpclient.login(user, pass);

        // use local passive mode to pass firewall
        ftpclient.enterLocalPassiveMode();

        System.out.println("Connected");

        String remoteDirPath = "/dist";
        String localDirPath ="/data/IOT/ase-core";


        FtpClient3.uploadDirectory(ftpclient, remoteDirPath, localDirPath, "");

        // log out and disconnect from the server
        ftpclient.logout();
        ftpclient.disconnect();

        System.out.println("Disconnected");
    } catch (IOException ex) {
        ex.printStackTrace();
    }

  }

}

0 个答案:

没有答案