如何将文件从SD卡上传到Android中的SFTP服务器?

时间:2012-04-04 06:32:01

标签: java android android-intent jsch

我使用JSch库将文件从SD卡上传到SFTP服务器,但是它产生了这样的错误

04-04 14:29:02.609: W/System.err(10706): 3: Permission denied
04-04 14:29:02.609: W/System.err(10706):    at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2289)
04-04 14:29:02.609: W/System.err(10706):    at com.jcraft.jsch.ChannelSftp._put(ChannelSftp.java:485)
04-04 14:29:02.609: W/System.err(10706):    at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:389)

我的代码是

public class FileUploadActivity extends Activity {
/** Called when the activity is first created. */
Channel channel = null;
ChannelSftp c = null;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.downloadmain);

    final String host="192.***.*.**";
    final String username="*****123";
    final String password="****123";
    try {
        Session session=connectSFTP(host, username, password);
        channel = session.openChannel("sftp");
        channel.connect();
        c = (ChannelSftp) channel;
        c.cd("/home/*****11/doc");
        String inputFileName =Environment.getExternalStorageDirectory().getAbsolutePath()+"/demo/enck.pdf";
        File f = new File(inputFileName);  
        c.put(inputFileName, "/home/*****11/doc/enck.pdf");
    } catch (Exception e) {
        // TODO: handle exception
        System.err.println("Unable to connect to FTP server."
                + e.toString());
        e.printStackTrace();
    }
}
public Session connectSFTP(String host,String username,String password){
    Session session= null;
    try{
        JSch js = new JSch();
        session = js.getSession(username, host, 22);
        session.setPassword(password);
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();          
    }catch (Exception e) {
        // TODO: handle exception
        Toast.makeText(getBaseContext(), "Unable to connect Your SFTP Serve"+e, Toast.LENGTH_SHORT);
    }
    return session;
}
}

1 个答案:

答案 0 :(得分:1)

要检查的明显事项:

  • 您的申请是否有权从SD卡读取?
  • 您登录的用户是否有权写入该目的地?

从例外的位置,我推断您确实有权访问互联网,但这也值得仔细检查。