如何使用ftp在android上传图像

时间:2012-10-29 04:52:30

标签: android ftp

任何人都可以帮助将图像从SD卡上传到服务器目录。如果目录存在,我需要上传,否则必须在该路径中创建目录,并且必须使用ftp上传图像。

2 个答案:

答案 0 :(得分:0)

使用Apache FTP客户端

http://commons.apache.org/net/download_net.cgi

示例代码:

public static FTPClient uploadingFilestoFtp() throws IOException
    {
    FTPClient ftpClient = new FTPClient();
    ftpClient.connect(InetAddress.getByName(host));
    ftpClient.login(username,password);
        ftpClient.changeWorkingDirectory(workdir);
        return ftpClient;
    }


class UploadVideo extends AsyncTask<String,Integer,String>
    {

        @Override
        protected String doInBackground(String... params) 
        {
            // TODO Auto-generated method stub
            try
            {
            ftpClient=uploadingFilestoFtp();
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            ftpClient.enterLocalPassiveMode();
            File file = new File(params[0]);
            videoFileSize=file.length();
            BufferedInputStream buffIn = new BufferedInputStream(
                    new FileInputStream(file));
            ProgressInputStream progressInput = new ProgressInputStream(buffIn);
            String randomid=UUID.randomUUID().toString();
            OutputStream os=ftpClient.storeFileStream(randomid+"-"+preferences.getStudentId()+".mp4");


            byte data[] = new byte[1024];
            long total = 0;
            int count;
            while ((count = progressInput.read(data)) != -1) {
                total += count;
                publishProgress((int) (total * 100 / videoFileSize));
                os.write(data, 0, count);
            }

            os.flush();
            os.close();
            progressInput.close();
            buffIn.close();

            }
            catch(IOException e)
            {
                e.printStackTrace();

            }
            return null;
        } 
        @Override
        protected void onPreExecute() 
        {
            super.onPreExecute();
            dialog.show();
        }

        @Override
        protected void onPostExecute(String result)
        {
            // TODO Auto-generated method stub
            super.onPostExecute(result);

        }
    }

答案 1 :(得分:0)

您可以使用名为SimpleFTP的库。将simpleftp.jar添加到类路径中,并将包导入到您需要的任何位置。

import org.jibble.simpleftp.*;

try
{
    SimpleFTP ftp = new SimpleFTP();

    // Connect to an FTP server on port 37.
    ftp.connect("ftp.someurl.net", 37, "username", "password");

    // Set binary mode(used to prevent file corruption etc)
    ftp.bin();

    // Change to a new working directory on the FTP server.
    ftp.cwd("mywebdir");

    // Upload some files.
    ftp.stor(new File("pic1.png"));
    ftp.stor(new File("pic2.png"));
    ftp.stor(new File("pic3.png"));

    // or use an InputStream like so:
    ftp.stor(new FileInputStream(new File("file1.png")), "file1.png");
    ftp.stor(mySocket.getInputStream(), "file2.png");

    // disconnect from the FTP server.
    ftp.disconnect();
}
catch (IOException e)
{
    e.printStackTrace();
}

在此处下载jar文件: http://www.jibble.org/files/simpleftp.jar