博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
网络_HttpURLConnection_原始类
阅读量:4290 次
发布时间:2019-05-27

本文共 3465 字,大约阅读时间需要 11 分钟。

// get 请求,取得响应
public class GetRunnable implements Runnable {	private Handler myhand;	public MyRunnable(Handler hand) {		this.myhand = hand;	}	@Override	public void run() {		// http://192.168.1.29:8080/itheima83/servlet/LoginServlet?username=sdafsad&pwd=fsadf		String message = "";		try {			URL url = new URL("http://192.168.1.29:8080/itheima83/servlet/LoginServlet?username=sdafsad&pwd=fsadf");			HttpURLConnection http = (HttpURLConnection) url.openConnection();			http.setConnectTimeout(10000);			http.setRequestMethod("GET");			int status = http.getResponseCode();			if (status < 300) {				InputStream is = http.getInputStream();				ByteArrayOutputStream bos = new ByteArrayOutputStream();				byte[] buffer = new byte[1024 * 10];				int len = 0;				while ((len = is.read(buffer)) != -1) {					bos.write(buffer, 0, len);				}				message = bos.toString();				bos.close();			}			Message msg = new Message();			msg.obj = message;			myhand.sendMessage(msg);		} catch (Exception e) {			e.printStackTrace();		}	}}
// post 请求,取得响应
URL url = new URL("http://192.168.1.29:8080/itheima83/servlet/LoginServlet");HttpURLConnection http = (HttpURLConnection) url.openConnection();http.setConnectTimeout(10000);http.setRequestMethod("POST");http.setDoOutput(true);//下面两行,不加同样可以被服务器解析。最好加上。http.setRequestProperty("Content-type", "application/x-www-form-urlencoded");//  multipart/form-datahttp.setRequestProperty("Content-Length", content.length()+"");http.getOutputStream().write("username=sdafsad&pwd=fsadf".getBytes());int status = http.getResponseCode();if (status < 300) {		//获得输入流,进行读取使用}
// post 提交文件
一般不用这个,因为post提交的标准请求体格式不好包装。尤其是 multipart类型的包装。
这个包装包括请求头的 content的长度,编码等内容。。
当然,如果自己写服务端,就无所谓了。
//下载。这个没什么总结的。通过无论什么请求,服务器把响应返回。
//响应包含响应头,里面有文件大小等信息
//单线程下载,多线程下载
//多线程的 Range 请求。和 content-Length的平均startIndex,endIndex。
//
断点续传,记录下载的位置点
class MyRunnable implements Runnable{	private String url_st;	private String file_path;	private int start;	private int end;	public MyRunnable(String url_st, String file_path, int start, int end) {		this.url_st = url_st;		this.file_path = file_path;		this.start = start;		this.end = end;		System.out.println(">>>>>>>>>开启一个新的线程。start:"+start+" end:"+end);	}	@Override	public void run() {		if(cacheFile.exsit()){			FileInputStream fileInputStream = new FileInputStream(file3);			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));			lastSaveIndex = Integer.parseInt(bufferedReader.readLine(););		}		try {			URL url = new URL(url_st);			HttpURLConnection http = (HttpURLConnection) url.openConnection();			http.setRequestMethod("GET");			http.setRequestProperty("Range", "bytes="+start+"-"+end);			http.setConnectTimeout(10000);			//开始写			InputStream is = http.getInputStream();			RandomAccessFile raf = new RandomAccessFile(file_path,"rwd");			raf.seek(start);			byte[] buffer = new byte[1024*10];			int len =0;			while((len = is.read(buffer)) != -1){				raf.write(buffer, 0, len);				//标记写的位置,可以用来续传				File file2 = new File( getdownloadPath()+threadId+".txt");				RandomAccessFile randomAccessFile2 = new RandomAccessFile(file2, "rwd");				randomAccessFile2.write(String.valueOf(currentThreadDownloadPosition).getBytes());				randomAccessFile2.close();				//利用 progressbard 的引用。改变 progressbar 的进度。				progressBar2.setProgress(progress);			}			raf.close();			http.disconnect();					} catch (MalformedURLException e) {			e.printStackTrace();		}catch(IOException e){			e.printStackTrace();		}	}}

转载地址:http://zdegi.baihongyu.com/

你可能感兴趣的文章
网易云基础服务郭忆:谈谈数据库的跨机房容灾
查看>>
文件或者文件夹压缩辅助类ZipUtility
查看>>
整理关于java实现二维码的生成和解析代码供大家参考
查看>>
java时间还在用date和calender?换LocalDateTime吧!
查看>>
优雅的缓存开发,看这一篇文章就可以了「干货」
查看>>
solr笔记
查看>>
集群架构及常见集群特性介绍
查看>>
Java互联网架构-如何设计服务接口API限流功能
查看>>
Java互联网架构-Mysql分库分表订单生成系统实战分析
查看>>
Java开发大型互联网企业微服务架构简介及罕见的问题点
查看>>
初探Java源码之ArrayList
查看>>
Mysql性能优化实战:数据库锁的介绍与索引查找原理
查看>>
Java程序员该如何提升让自己成为高薪架构师?
查看>>
「mysql优化专题」这大概是一篇最好的mysql优化入门文章(1)
查看>>
Java虚拟机体系结构由几部分组成?
查看>>
用分布式日志优化单机数据库系统将成未来标配?
查看>>
Java互联网架构-深入理解MQ实现分布式事务
查看>>
Spring boot整合Springfox在线生成restful的api doc
查看>>
简单MySQL教程二
查看>>
mysql学习之 explain
查看>>