文件下载
public class SyncThread implements Runnable {
    private static final String TAG = "SyncThread";
    private Context                    mContext;
    private String                     mUrl;
    private String                     mFilename;
    private NotificationManager        mNotifyManager;
    private NotificationCompat.Builder mBuilder;

    private SyncThread(Context ctx, String Url, String filename) {
        this.mContext = ctx;
        mUrl = Url;
        mFilename = filename;
        mNotifyManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        mBuilder = new NotificationCompat.Builder(mContext);
    }


    public static SyncThread getInstace(Context ctx, String url, String filename) {
        return new SyncThread(ctx, url, filename);
    }


    @Override
    public void run() {
        //下载文件
        downLoad(mUrl, mFilename);
    }

    private void updateProgress(int progress) {
        mBuilder.setContentText("下载进度" + progress + "%").setProgress(100, progress, false);
        PendingIntent pendingintent = PendingIntent.getActivity(mContext, 0, new Intent(), PendingIntent.FLAG_CANCEL_CURRENT);
        mBuilder.setContentIntent(pendingintent);
        mNotifyManager.notify(0, mBuilder.build());
    }

    private synchronized void downLoad(String murl, String filename) {
        InputStream in = null;
        FileOutputStream out = null;
        int oldProgress = 0;
        try {
            URL url = new URL(murl);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setConnectTimeout(10 * 1000);
            urlConnection.setReadTimeout(10 * 1000);
            urlConnection.setRequestProperty("Connection", "Keep-Alive");
            urlConnection.setRequestProperty("Accept-Language", "zh-CN");
            urlConnection.setRequestProperty("Charset", "UTF-8");
            //urlConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
            urlConnection.setUseCaches(false);
            urlConnection.connect();
            long bytetotal = urlConnection.getContentLength();
            long bytesum = 0;
            int byteread = 0;
            in = urlConnection.getInputStream();
            File apkFile = new File(ADConstants.getIntance().getDownloadFileDir(mContext), filename);
            out = new FileOutputStream(apkFile);
            byte[] buffer = new byte[1024];
            while ((byteread = in.read(buffer)) != -1) {
                bytesum += byteread;
                out.write(buffer, 0, byteread);
                int progress = (int) (bytesum * 100L / bytetotal);
                if (progress != oldProgress) {
                    updateProgress(progress);
                }
                oldProgress = progress;
            }
            mBuilder.setContentText("下载完毕").setProgress(0, 0, false);
            Log.d(TAG, "下载完毕。。。");
            mBuilder.setContentText("文件检验成功");
            Notification noti = mBuilder.build();
            noti.flags = Notification.FLAG_AUTO_CANCEL;
            mNotifyManager.notify(0, noti);
            mNotifyManager.cancel(0);
        } catch (Exception e) {
            mBuilder.setContentText("下载失败:" + e.getMessage());
            Log.d(TAG, "下载失败。。。:" + e.getMessage());
            Notification noti = mBuilder.build();
            noti.flags = Notification.FLAG_AUTO_CANCEL;
            mNotifyManager.notify(0, noti);
        } finally {
            StreamUtils.closeStream(out);
            StreamUtils.closeStream(in);
        }
    }
}

public class StreamUtils {
    // 关流
    public static void closeStream(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

//使用
SyncThread instace;
    ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
    instace = SyncThread.getInstace(this, Constant.Sound_URL, fileName);
    singleThreadExecutor.execute(instace);
阅读更多
文章标签: 文件下载
想对作者说点什么? 我来说一句

vb文件下载系统(设计说明)

2008年09月23日 12KB 下载

文件下载上传 文件下载上传

2009年06月02日 1.31MB 下载

Asp.net文件下载

2011年07月25日 1KB 下载

C#FTP TCP文件下载

2009年02月17日 371KB 下载

admexs.dll

2009年07月18日 29KB 下载

convlog.exe 文件下载

2009年07月18日 62KB 下载

infoctrs.dll 文件下载

2009年07月18日 9KB 下载

jawt.dll下载

2010年01月14日 1KB 下载

基于servlet的文件下载代码

2017年11月17日 3KB 下载

没有更多推荐了,返回首页

不良信息举报

文件下载

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭