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

本文共 3474 字,大约阅读时间需要 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-data
http.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://pbegi.baihongyu.com/

你可能感兴趣的文章
大型高并发与高可用缓存架构总结
查看>>
javascript设计模式-工厂模式(4)
查看>>
javascript设计模式-组合模式(6)
查看>>
javascript设计模式-门面模式(7)
查看>>
javascript设计模式-享元模式(10)
查看>>
javascript设计模式-代理模式(11)
查看>>
Executor相关源码分析
查看>>
react之setState解析
查看>>
elasticsearch7.3版本已经不需要额外安装中文分词插件了
查看>>
【重大好消息】elasticsearch 7.3版本已经可以免费使用x-pack就可以设置账号和密码了,让你的数据不再裸奔
查看>>
解决使用logstash中jdbc导入mysql中的数据到elasticsearch中tinyint类型被转成布尔型的问题的方法
查看>>
elasticsearch7.3版本环境搭建(一)elasticsearch安装和配置
查看>>
SEO基本功:站内优化的一些基本手段
查看>>
centos6系列和7系列如何对外开放80,3306端口号或者其他端口号
查看>>
为什么您宁愿吃生活的苦,也不愿吃学习的苦?为什么你不愿意去学习呢
查看>>
解决elasticsearch7.3版本安装过程中遇到的包括内存不够、线程不够等问题
查看>>
日常项目测试用例检查点(来自一线测试人员的吐血总结)
查看>>
网站建设之域名注册和域名备案
查看>>
解决bootstrap时间输入框总被浏览器记住的记录遮挡住的问题
查看>>
git将一个分支完全覆盖另外一个分支如:dev分支代码完全覆盖某一个开发分支
查看>>