栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Android文件下载

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android文件下载

public class FileDownloadUtils {

    private String sdPath;

    public FileDownloadUtils() {
        sdPath = Environment.getExternalStorageState() + "/";
    }

    
    public File createSDFile(String fileName) throws IOException {
        File file = new File(sdPath + fileName);
        file.createNewFile();
        return file;
    }

    
    public File createSDDir(String dirName) {
        File dir = new File(sdPath + dirName);
        dir.mkdirs();
        return dir;
    }

    
    public boolean isFileExist(String fileName) {
        File file = new File(sdPath + fileName);
        return file.exists();
    }

    
    public File write2SDFromInput(String path, String fileName, InputStream inputStream) {
        File file = null;
        OutputStream outputStream = null;
        try {
            createSDDir(path);
            file = createSDFile(path + fileName);
            outputStream = new FileOutputStream(file);
            byte[] buffer = new byte[4 * 1024];
            while ((inputStream.read(buffer)) != -1) {
                outputStream.write(buffer);
            }
            outputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return file;
    }


    public String getSdPath() {
        return sdPath;
    }

    public void setSdPath(String sdPath) {
        this.sdPath = sdPath;
    }
}

public class HttpDownloader {
    URL url = null;

    public String download(String urlStr) {
        StringBuilder sb = new StringBuilder();
        String line = null;
        BufferedReader br = null;
        try {
            url = new URL(urlStr);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            br = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

    
    public int downFile(String urlStr, String path, String fileName) {
        InputStream inputStream = null;
        try {
            FileDownloadUtils fileDownloadUtils = new FileDownloadUtils();
            if (fileDownloadUtils.isFileExist(path + fileName)) {
                return 1;
            } else {
                inputStream = getInputStreamFromUrl(urlStr);
                File resultFile = fileDownloadUtils.write2SDFromInput(path, fileName, inputStream);
                if (resultFile == null) {
                    return -1;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return 0;
    }

    
    public InputStream getInputStreamFromUrl(String urlStr) throws MalformedURLException, IOException {
        url = new URL(urlStr);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        InputStream inputStream = httpURLConnection.getInputStream();
        return inputStream;
    }

}

在Activity使用:

@Override
    public void onClick(View v) {
        DownloadFileThread downloadFileThread = new DownloadFileThread(downloadUrl, FileUtil.getSDCardPath(), "html.zip");
        downloadFileThread.start();
    }

    @SuppressLint("HandlerLeak")
    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == -1) {
                Toast.makeText(FileOperateActivity.this, "下载文件出错", Toast.LENGTH_SHORT).show();
                Log.e(TAG, "下载文件出错");
            } else if (msg.what == 0) {
                Toast.makeText(FileOperateActivity.this, "下载文件成功", Toast.LENGTH_SHORT).show();
                Log.e(TAG, "下载文件成功");
            } else if (msg.what == 1) {
                Toast.makeText(FileOperateActivity.this, "代表文件已经存在", Toast.LENGTH_SHORT).show();
                Log.e(TAG, "代表文件已经存在");
            }
        }
    };

    class DownloadFileThread extends Thread {
        private final String download_file_url;
        private final String download_file_path;
        private final String download_file_fileName;

        public DownloadFileThread(String download_file_url, String download_file_path, String download_file_fileName) {
            this.download_file_url = download_file_url;
            this.download_file_path = download_file_path;
            this.download_file_fileName = download_file_fileName;
        }

        @Override
        public void run() {
            HttpDownloader httpDownloader = new HttpDownloader();
            int result = httpDownloader.downFile(download_file_url, download_file_path, download_file_fileName);
            Message message = new Message();
            message.what = result;
            mHandler.sendMessage(message);
        }
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/305768.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号