0

这是使用 FTP 在 android 中上传图像的快速有效的方法。我可以在其中创建图像的文件夹重命名文件,请发布 code.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 错误

4

2 回答 2

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");        
    }
于 2012-11-29T10:40:13.777 回答
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);

于 2013-03-19T07:28:24.107 回答