根据URL下载文件

来源:互联网 发布:婚礼邀请函制作软件 编辑:程序博客网 时间:2024/04/29 01:48
public String downloadNet(String downUrl, String file1) throws MalformedURLException {// 下载网络文件String path = "";int bytesum = 0;int byteread = 0;String packages = extractString(downUrl);String saveFileName = String.valueOf(System.currentTimeMillis()) + Math.round(Math.random() * 100000);URL url = new URL(downUrl);try {URLConnection conn = url.openConnection();InputStream inStream = conn.getInputStream();FileOutputStream fs = new FileOutputStream(file1 + File.separator + saveFileName + ".apk");path=file1+saveFileName  + ".apk";byte[] buffer = new byte[1204];int length;while ((byteread = inStream.read(buffer)) != -1) {bytesum += byteread;System.out.println(bytesum);fs.write(buffer, 0, byteread);}if(null!=fs){try {fs.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return path;}