java.io.FileNotFoundException:C:\ Users \ admin \ Desktop \ abcd(访问被拒绝)

时间:2015-03-17 06:09:22

标签: java ssh jsch

我正在尝试将目录传输到sftp远程服务器。

import java.io.File;
import java.io.FileInputStream;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class FolderTransfer {

    public FolderTransfer() {
    }

    public static void main(String[] args) {
        String SFTPHOST = "hostname";
        int SFTPPORT = 22;
        String SFTPUSER = "admin";
        String SFTPPASS = "password";
        String SFTPWORKINGDIR = "/cygdrive/c/Users/admin";

        Session session = null;
        Channel channel = null;
        ChannelSftp channelSftp = null;

        try {
            JSch jsch = new JSch();
            session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
            session.setPassword(SFTPPASS);
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
            channel = session.openChannel("sftp");
            channel.connect();
            channelSftp = (ChannelSftp) channel;
            channelSftp.cd(SFTPWORKINGDIR);
            File f = new File("C:\\Users\\admin\\Desktop\\abcd\\");
            channelSftp.put(new FileInputStream(f), f.getName());
            System.out.println("done....");
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

}

但我得到的错误就像 java.io.FileNotFoundException:C:\ Users \ admin \ Desktop \ abcd(访问被拒绝) 我怎么解决这个问题?

1 个答案:

答案 0 :(得分:0)

abcd不是文件,它是一个目录。压缩目录并作为文件发送或获取所有文件并逐个发送

File folder = new File("your/path");
File[] listOfFiles = folder.listFiles();

for (int i = 0; i < listOfFiles.length; i++) {
  if (listOfFiles[i].isFile()) {
    System.out.println("File " + listOfFiles[i].getName());
  } else if (listOfFiles[i].isDirectory()) {
    System.out.println("Directory " + listOfFiles[i].getName());
  }
}