Android图像上传到SFTP服务器

时间:2012-11-29 10:24:11

标签: android ftp

这是使用FTP在Android上传图像的快速有效方法。我可以创建文件夹重命名文件的图像,请发布代码.SFTP代码:

f.setHost("host");
f.setUser("user");
f.setPassword("pwd");
boolean connected=f.connect();
f.setRemoteFile("server directory");
if(connected){
  f.uploadFile("local file);
} 

获取Ftp 553错误

2 个答案:

答案 0 :(得分:0)

更改您的代码,与使用JSch lib

在服务器上传文件相同
    private File uploadFilePath;
    Session session ;
     Channel channel = null;
    ChannelSftp sftp;
    uploadFilePath=new File(Environment.getExternalStorageDirectory()
                                                     +"/temp.jpg");
    byte[] bufr = new byte[(int) uploadFilePath.length()];
    FileInputStream fis = new FileInputStream(uploadFilePath);
    fis.read(bufr);
    JSch ssh = new JSch();
   try {
        session =ssh.getSession("root", "xx.xxx.xxx.xx");
        System.out.println("JSch JSch JSch Session created.");
        session.setPassword("password");
        java.util.Properties config = new java.util.Properties(); 
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        System.out.println("JSch JSch Session connected.");
        System.out.println("Opening Channel.");
        channel = session.openChannel("sftp"); 
        channel.connect();
        sftp= (ChannelSftp)channel;
            // server path where you want to upload file
        sftp.cd("/root/temp/dir/upload/"); 
    }
    catch(Exception e){

    }
ByteArrayInputStream in = new ByteArrayInputStream(bufr);
sftp.put(in, uploadFilePath.getName(), null);
in.close();

if (sftp.getExitStatus()==-1) {
    System.out.println("file uploaded");
    Log.v("upload result", "succeeded");                                    
    } else {
    Log.v("upload faild ", "faild");        
    }

答案 1 :(得分:0)

在下面的代码中,我能够创建会话,但我的代码会出错

I am getting null pointer exception here.


public void uploaddata() throws IOException, SftpException

{
    File uploadFilePath;
    Session session ;
     Channel channel = null;
    ChannelSftp sftp = null;

    uploadFilePath = searchForFileInExternalStorage("video.3gp");  

   /* uploadFilePath=new File(Environment.getExternalStorageDirectory()
                                                     +"/video.3gp");*/
    Log.e("image file path are", uploadFilePath.getPath()+"name"+uploadFilePath.getName()+"length is"+uploadFilePath.length());

    byte[] bufr = new byte[(int) uploadFilePath.length()];
    FileInputStream fis = new FileInputStream(uploadFilePath);
    fis.read(bufr);
    JSch ssh = new JSch();
   try {
        session =ssh.getSession(username, hostName);
        System.out.println("JSch JSch JSch Session created.");
        session.setPassword(password);
        java.util.Properties config = new java.util.Properties(); 
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        System.out.println("JSch JSch Session connected.");
        System.out.println("Opening Channel.");
        channel = session.openChannel("sftp"); 
        channel.connect();
        sftp= (ChannelSftp)channel;

        Log.e("chanel is",""+channel+"sftp"+sftp);

            // server path where you want to upload file
        sftp.cd(location); 
    }
    catch(Exception e){

    }
   ByteArrayInputStream in = new ByteArrayInputStream(bufr);
   sftp.put(in, uploadFilePath.getName(), null);
in.close();

if (sftp.getExitStatus()==-1) {
    System.out.println("file uploaded");
    Log.v("upload result", "succeeded");                                    
    } else {
    Log.v("upload faild ", "faild");        
    }

以下行正在生成错误  sftp.put(in,uploadFilePath.getName(),null);