Android(安卓)单HTTP链接多文件下载
16lz
2021-01-24
public static void main(String[] args) {System.out.println(System.currentTimeMillis());for (int i = 1; i < 2; i++) {int nRead = 0;String j = "";if (i < 10)j = "00" + i;else if (i < 100)j = "0" + i;String sPath = sURL.replace("xxx", j);// String sName = sPath.substring(sURL.lastIndexOf("/") + 1, sPath.lastIndexOf("?"));try {URL url = new URL(sPath);// URL url = new URL(sPath + "&" + System.currentTimeMillis());HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();httpConnection.setRequestProperty("User-Agent", "Internet Explorer");httpConnection.connect();InputStream input = httpConnection.getInputStream();byte[] b = new byte[1024];RandomAccessFile file = getRandomFile();// 读取网络文件,写入指定的文件中while ((nRead = input.read(b)) != -1) {String msg = new String(b, 0, nRead, "iso-8859-1");// System.out.println(msg);if (buffer.length() > 0) {msg = buffer.toString() + msg;buffer.delete(0, buffer.length());}int pos = msg.indexOf("<!--next-->");boolean next = pos != -1;int pos_harf = msg.indexOf("<");boolean harf = (msg.length() - pos_harf) < 11 && pos_harf != -1;if (!next && !harf) {file.write(msg.getBytes("iso-8859-1"));} else if (!next && harf) {buffer.append(msg);continue;} else if (next) {file.write(msg.substring(0, pos).getBytes());file.close();buffer.append(msg.substring(pos + 11));file = getRandomFile();continue;}}httpConnection.disconnect();} catch (Exception e) {e.printStackTrace();}}System.out.println(System.currentTimeMillis());}static synchronized RandomAccessFile getRandomFile() {try {return new RandomAccessFile("D:\\Android\\LeClient\\bin\\" + System.currentTimeMillis() + ".jpg", "rw");} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}
更多相关文章
- 垂直的SeekBar
- android实现通知栏透明
- Android(安卓)VNC Server
- eclipse中查看android的SDK源代码
- Android(安卓)使用 URL 和 AsyncTask 加载网络数据
- android创建txt文件,读取txt文件内容
- Android第十二课 jni函数的静态绑定
- Android(安卓)采用pull解析xml文件