0

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

4

2 回答 2

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

        }
    }
于 2012-10-29T05:06:26.377 回答
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

于 2012-10-29T05:06:26.523 回答