2012-11-29 84 views
0

這是使用FTP.where我可以能夠建立重命名文件夾的圖像的文件請張貼code.SFTP代碼上傳圖像中的Android快速和有效的方式: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錯誤

+0

請格式化您的代碼。 – njzk2

回答

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");   
    } 
+0

ChannelSftp其中lib我可以看到這個ChannelSftp類和JSch –

+0

在jsch-0.1.48.jar lib你有它,如果沒有,然後從這裏下載http://www.jcraft.com/jsch/ –

+0

什麼是這裏的頻道 –

0

在下面的代碼,我可以創建會話,但上載服務器上的文件,然後我的代碼使Ë RROR

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(在,uploadFilePath.getName(),NULL);