如何使用ftp在android中上传图像

如何使用ftp在android中上传图像,android,ftp,Android,Ftp,任何人都可以帮助将图像从SD卡上载到服务器目录。如果目录存在,我需要上载到那里,其他人必须在该路径中创建目录,并且必须使用ftp上载图像。使用Apache ftp客户端 示例代码: public static FTPClient uploadingFilestoFtp() throws IOException { FTPClient ftpClient = new FTPClient(); ftpClient.connect(InetAddress.getByName(

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

使用Apache ftp客户端

示例代码:

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);

        }
    }
public static FTPClient uploadingFilestoFtp()引发IOException
{
FTPClient FTPClient=新的FTPClient();
ftpClient.connect(InetAddress.getByName(主机));
ftpClient.login(用户名、密码);
ftpClient.changeWorkingDirectory(workdir);
返回ftpClient;
}
类UploadVideo扩展异步任务
{
@凌驾
受保护的字符串doInBackground(字符串…参数)
{
//TODO自动生成的方法存根
尝试
{
ftpClient=uploadingFilestoFtp();
ftpClient.setFileType(FTP.BINARY文件类型);
ftpClient.enterLocalPassiveMode();
File File=新文件(参数[0]);
videoFileSize=file.length();
BufferedInputStream buffIn=新的BufferedInputStream(
新文件输入流(文件));
ProgressInputStream progressInput=新的ProgressInputStream(buffIn);
字符串randomid=UUID.randomUUID().toString();
OutputStream os=ftpClient.storeFileStream(randomid+“-”+preferences.getStudentId()+”.mp4”);
字节数据[]=新字节[1024];
长总计=0;
整数计数;
而((count=progressInput.read(data))!=-1){
总数+=计数;
出版进度(整数)(总计*100/视频文件大小);
操作系统写入(数据,0,计数);
}
os.flush();
os.close();
progressInput.close();
buffIn.close();
}
捕获(IOE异常)
{
e、 printStackTrace();
}
返回null;
} 
@凌驾
受保护的void onPreExecute()
{
super.onPreExecute();
dialog.show();
}
@凌驾
受保护的void onPostExecute(字符串结果)
{
//TODO自动生成的方法存根
super.onPostExecute(结果);
}
}

您可以使用名为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文件:

您正在使用哪个ftp库?它是套接字连接。如果您没有使用套接字,则忽略该部分。这只是一个例子。am获取错误10-29 10:48:06.399:W/System.err(9616):java.io.IOException:SimpleFTP在连接到FTP服务器时收到未知响应:220------------欢迎使用纯FTPd[priveSep][TLS]--------@Balajeenanamalai这是因为SimpleFTP不灵活。服务器响应应该是“220”,后跟一个空格,而在您的示例中,服务器以“220-”开头。