FTP文件上传问题

时间:2011-11-15 06:56:36

标签: android ftp

我需要将文件从SD卡上传到服务器(使用FTP ),但在尝试保存文件时遇到了一些问题。该文件似乎首先存在,但是当我选择播放它时......好吧,它不会(就像它在上传过程中崩溃一样无效)。关于可能出错的任何想法?我设置了扩展名.mp3,这里是代码:

holder.upload.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

        String str_useid = Recording.str_getValue;
        if (upload_or_not == true) {

            FTPClient con = new FTPClient();
            try {

                con.connect("FTP URL");
                if (con.login("USERNAME", "PASSWORD")) {

                    Handler progressHandler = new Handler();
                    con.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
                    BufferedInputStream buffIn = new BufferedInputStream(

                        new FileInputStream(

                            "/sdcard/audiometer/shanesh" + 
                            RequestId[position] + "-"
                            str_useid + ".mp3"

                    )

                );

                    con.enterLocalPassiveMode(); // important!
                    ProgressInputStream progressInput = new ProgressInputStream(

                        buffIn, 
                    progressHandler

                );

                boolean status = con.storeFile(

                    "/Rangam/shanesh" +
                        RequestId[position] + "-" + 
                    str_useid + 
                    ".mp3", 
                    progressInput

                );

                String filename = "/shanesh" + RequestId[position] +
                        "-" + str_useid + ".mp3";

                buffIn.close();
                    con.logout();
                    con.disconnect();

                    String MachineName = "DOT-NET-SERVER";
                    sendFlagToServer(RequestId[position], filename, MachineName);

                    Toast.makeText(

                    context, 
                    " :: sucessfully upload :: " + status,
                        Toast.LENGTH_LONG

                ).show();

                Toast.makeText(

                        context,
                        " :: RequestId is :: " +
                        RequestId[position],
                        Toast.LENGTH_LONG

                ).show();

                }

        } catch (Exception e) {

            e.printStackTrace();
                    }
            } else {

            Toast.makeText(
                context, 
                " :: File not Found :: ",
                    Toast.LENGTH_LONG
            ).show();

        }

        }

ProgressInputStream.java

package com.RecordingApp;

import java.io.IOException;
import java.io.InputStream;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class ProgressInputStream extends InputStream {

    /* Key to retrieve progress value from message bundle passed to handler */
    public static final String PROGRESS_UPDATE = "progress_update";

    private static final int TEN_KILOBYTES = 1024 * 10;

    private InputStream inputStream;
    private Handler handler;

    private long progress;
    private long lastUpdate;

    private boolean closed;

    public ProgressInputStream(InputStream inputStream, Handler handler) {
        this.inputStream = inputStream;
        this.handler = handler;

        this.progress = 0;
        this.lastUpdate = 0;

        this.closed = false;
    }

    @Override
        public int read() throws IOException {
            int count = inputStream.read();
            return incrementCounterAndUpdateDisplay(count);
        }

    @Override
        public int read(byte[] b, int off, int len) throws IOException {
            int count = inputStream.read(b, off, len);
            return incrementCounterAndUpdateDisplay(count);
        }

    @Override
        public void close() throws IOException {
            super.close();
            if (closed)
                throw new IOException("already closed");
            closed = true;
        }

    private int incrementCounterAndUpdateDisplay(int count) {
        if (count > 0)
            progress += count;
        lastUpdate = maybeUpdateDisplay(progress, lastUpdate);
        return count;
    }

    private long maybeUpdateDisplay(long progress, long lastUpdate) {
        if (progress - lastUpdate > TEN_KILOBYTES) {
            lastUpdate = progress;
            sendLong(PROGRESS_UPDATE, progress);
        }
        return lastUpdate;
    }

    public void sendLong(String key, long value) {
        Bundle data = new Bundle();
        data.putLong(key, value);

        Message message = Message.obtain();
        message.setData(data);
        handler.sendMessage(message);
    }
}

0 个答案:

没有答案
相关问题