当前位置: 代码迷 >> Android >> Android 单HTTP链接多文件上载
  详细解决方案

Android 单HTTP链接多文件上载

热度:33   发布时间:2016-05-01 17:16:36.0
Android 单HTTP链接多文件下载

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 block			e.printStackTrace();		}		return null;	}
?
  相关解决方案