文件下载

1.创建HttpURLConnection对象

2.获得一个InputSream对象

3.访问网络权限

<uses-permission android:name="android.permission.INTERNET"/>

 

代码:

设置两个按钮,一个下载文本文件,另一个下载MP3

public class MainActivity extends Activity {
    private Button textButton;
    private  Button mp3Button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textButton = (Button)findViewById(R.id.my_tvbt);
        mp3Button = (Button)findViewById(R.id.my_mp3bt);
        textButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HttpDownLoder loder = new HttpDownLoder();
                String result = loder.downLoad("");
                System.out.println(result);
            }
        });
        mp3Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HttpDownLoder loader = new HttpDownLoder();
                FileUtils utils = new FileUtils();
                int result = loader.dowLoadFile("weimiao","可念不可说.mp3","http://music.163.com/#/m/song?id=30871158&userid=286327298");
                System.out.println(result);
            }
        });
    }
}

 

 

public class FileUtils {
    public String SDPATH = null;

    public FileUtils() {
        SDPATH  = Environment.getExternalStorageState() + "/";
    }

    public String getSDPATH() {
        return SDPATH;
    }
    //创建File文件
    public File createFile(String fileName) throws  Exception{
        File file = new File(getSDPATH()+fileName);
        file.createNewFile();
        return file;
    }
    //sd卡上创建目录
    public File createSDDir(String dirName) throws Exception{
        File file = new File(getSDPATH()+ dirName);
        file.mkdir();
        return  file;
    }
    //判断文件在sd卡上是否存在
    public boolean isExit(String fileName){
        File file = new File(getSDPATH()+ fileName);
        return file.exists();
    }
    //inputStream写到sd卡中
    public File write2SdInput(String  path, String fileName, InputStream inputStream){
        OutputStream os = null;
        File file = null;
        try{
            createSDDir(path);
            file = createFile(path + fileName);
            os = new FileOutputStream(file);
            byte[] bytes = new byte[4 * 1024];
            while((inputStream.read(bytes) )!= -1){
                os.write(bytes);
            }
            os.flush();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                os.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return file;
    }

}

 

public class HttpDownLoder{
    private URL url = null;
    //下载文本文件
    public String downLoad(String urlStr){
        StringBuffer sb = new StringBuffer();
        String line = null;
        BufferedReader br = null;
        //创建url对象
        try {
            url = new URL(urlStr);
            //创建HTTP链接
            HttpURLConnection  connection = (HttpURLConnection)url.openConnection();
            //获取输入流
            br = new BufferedReader(new InputStreamReader(connection.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();
    }

    //下载mp3
    public int dowLoadFile(String path,String fileName,String urlStr){
        InputStream inputStream = null;
        try{
            FileUtils utils = new FileUtils();
            if(utils.isExit(path + fileName)){
                return 1;
            }else{
                inputStream = getInputStreamUri(urlStr);
                File resultFile =   utils.write2SdInput(path,fileName,inputStream);
                if(resultFile == null){
                    return  -1;
                }
            }
        }catch (Exception  e){
            e.printStackTrace();
        }finally {
            try {
             inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }


    //得到inputStream
    public InputStream  getInputStreamUri(String urlStr) throws IOException {
        url = new URL(urlStr);
        HttpURLConnection  connection = (HttpURLConnection)url.openConnection();
        InputStream inputStream = connection.getInputStream();
        return inputStream;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值