包含垃圾值的JSch SFTP文件下载

时间:2017-09-14 03:27:01

标签: java sftp jsch

我正在使用JSch程序将文件上传并下载到我的SFTP服务器。当我下载文件时,该文件包含垃圾值。

以下是我写的代码:

public ChannelSftp createSession(String sftpUserName, String sftpHost, int sftpPort) {
    JSch jsch = new JSch();
    Channel channel = null;
    ChannelSftp c = null;
    String privateKey = "~/.exec/id_rsa";

    try {
        if (session == null) {
            session = jsch.getSession(sftpUserName, sftpHost, sftpPort);
            //session.setPassword(sftpPassword);
            jsch.addIdentity(privateKey);
            session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password");
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
        }
        channel = session.openChannel("sftp");
        channel.connect();
        c = (ChannelSftp) channel;

    } catch (Exception e) {
        e.printStackTrace();

    }
    return c;
}
public Integer downloadFile(ChannelSftp channelSftp, String FileToBeCopied, String folderPath) {
    int fileDownloaded = -1;

    try {

        //channelSftp.cd(folderPath);
        OutputStream output = new FileOutputStream("/Users/" + System.getProperty("user.name") + "/Downloads/" + FileToBeCopied);
        channelSftp.setFilenameEncoding("UTF-8");
        channelSftp.get(FileToBeCopied, output);

        fileDownloaded = 0;
    } catch (FileNotFoundException e) {
        fileDownloaded = 1;
        e.printStackTrace();
    } catch (SftpException e) {
        fileDownloaded = 2;
        e.printStackTrace();
    } catch (Exception e) {
        fileDownloaded = 3;

    }
    return fileDownloaded;
}
public boolean uploadFiles(ChannelSftp channelSftp, String workingFile, String ftpRemoteDefaultDirectory, String workingDirectory) {
    boolean flag = false;
    if (workingFile != null) {
        if (!workingFile.equalsIgnoreCase("")) {
            try {

                channelSftp.cd(ftpRemoteDefaultDirectory);
                try {
                    File f = new File(workingDirectory + workingFile);
                    //channelSftp.setFilenameEncoding("UTF-8");
                    channelSftp.put(new FileInputStream(f), f.getName());
                    flag = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                destroySession(channelSftp);
            } catch (Exception e) {

                e.printStackTrace();
            }
        }
    }
    return flag;
}

我正在使用MAC。当我在网上查看时,提到OutputStream将使用" UTF-8"编码文件。我们不必再做了。

1 个答案:

答案 0 :(得分:0)

我不确定您在哪里获得OutputStream转换为UTF-8的信息。没有OutputStream这样做,那是OutputStreamWriter的用途。此外,OutputStream是一个抽象类,因此当您持有一个只知道它是OutputStream的实例时,您根本不知道该流对数据的作用。在您的代码中,您使用FileOutputStream进行撰写并使用FileInputStream进行阅读,因此不应对所有正在下载或上传的数据进行转换。

因此,如果此后数据看起来不对,原始数据似乎不是UTF-8编码。

BTW:channelSftp.setFilenameEncoding("UTF-8");与数据的字符集/编码无关。正如方法名称所示,它控制文件名的编码方式,因此如果文件名包含非ASCII字符,则需要在下载和上传时设置它,以确保它们不会显示已损坏。

相关问题