本文主要是介绍网络下载-HttpURLConnection,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
// get 请求,取得响应public class GetRunnable implements Runnable {private Handler myhand;public MyRunnable(Handler hand) {this.myhand = hand;}@Overridepublic void run() {// http://192.168.1.29:8080/itheima83/servlet/LoginServlet?username=sdafsad&pwd=fsadfString 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);}@Overridepublic 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();}}
}
这篇关于网络下载-HttpURLConnection的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!