자바 정지점 전송 실현

자바 의 URL 로 간단 한 정지점 속전 열 을 실현 하 였 습 니 다.다른 것 이 아 닙 니 다.정지점 속전 에 대해 잘 모 르 면 한 번 보면 알 수 있 습 니 다.
package test;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/* @className:TestDown.java 
 * @classDescription:    java    
 * @author:jiangmianyue
 * @createTime:2011-11-29
 */
public class TestDown {
	public static void main(String[] args) {
		URL url = null;
		try {
			url = new URL("http://172.16.1.33/newfile/e358beb5367341929614332e1a390dfb.apk");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		URLConnection httpConnection = null;
		try {
			//     
			httpConnection = url.openConnection();
		} catch (IOException e) {
			e.printStackTrace();
		}
		 //   ua
		 httpConnection.setRequestProperty("User-Agent","NetFox"); 
		//              100k  
		httpConnection.setRequestProperty("RANGE","bytes=102400-");
		//       
		InputStream input = null;
		try {
			input = httpConnection.getInputStream();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		//               
		RandomAccessFile oSavedFile = null;
		try {
			oSavedFile = new RandomAccessFile("d://down.apk","rw");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} 
		//   100K   
		long nPos = 102400; 
		//         nPos    
		try {
	    //            
			oSavedFile.seek(nPos);
		} catch (IOException e) {
			e.printStackTrace();
		} 
		byte[] b = new byte[1024]; 
		int nRead; 
		//           ,        
		try {
			// int n =0;
			while((nRead=input.read(b,0,1024)) > 0) 
			{ 
			oSavedFile.write(b,0,nRead); 
			/**
			 *        n   100     
			 * if(100 == n){
			 *     break;
			 * }
			 */
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	//                   
	private static void setHeader(URLConnection con) {
		con.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008092510 Ubuntu/8.04 (hardy) Firefox/3.0.3");
		con.setRequestProperty("Accept-Language", "en-us,en;q=0.7,zh-cn;q=0.3");
		con.setRequestProperty("Accept-Encoding", "aa");
		con.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
		con.setRequestProperty("Keep-Alive", "300");
		con.setRequestProperty("Connection", "keep-alive");
		con.setRequestProperty("If-Modified-Since", "Fri, 02 Jan 2009 17:00:05 GMT");
		con.setRequestProperty("If-None-Match", "\"1261d8-4290-df64d224\"");
		con.setRequestProperty("Cache-Control", "max-age=0");
		con.setRequestProperty("Referer", "http://www.dianping.com");
	}
}



좋은 웹페이지 즐겨찾기