国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

正常下載與URLConnection 六(64)

2019-11-15 00:14:01
字體:
來源:轉載
供稿:網友
正常下載與URLConnection 六(64)

一 正常下載

服務使用斷點下載時,響應的信息是206。

UrlConnection - HttpurlConnection。-通過URL來獲取urlconnection實例。

正常下載示例

package cn.demo;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.math.BigDecimal;import java.net.HttpURLConnection;import java.net.URL;public class CommonDown {    public static void main(String[] args) throws Exception {        String path = "http://localhost:6666/day22_cos/up/video.avi";        URL url = new URL(path);        HttpURLConnection con = (HttpURLConnection) url.openConnection();        con.setRequestMethod("GET");        con.setDoInput(true);        con.connect();        int code = con.getResponseCode();        System.err.PRintln(code);        if (code == 200) {            //獲取文件大小            long size = con.getContentLength();            System.err.println("總大小是:"+size);            //聲明下載到的字節            long sum=0;            BigDecimal bd = new BigDecimal(0D);            double already = 0D;            InputStream in = con.getInputStream();            byte[] b = new byte[1024];            int len = -1;            OutputStream out = new FileOutputStream("d:/a/video.avi");            while ((len = in.read(b)) != -1) {                out.write(b, 0, len);                sum=sum+len;                double percent = ((double)sum)/((double)size);                percent*=100;                bd = new BigDecimal(percent);                bd = bd.divide(new BigDecimal(1),0,BigDecimal.ROUND_HALF_UP);                if(bd.doubleValue()!=already){                    System.err.println(bd.intValue()+"%");                    already=bd.doubleValue();                }            }            out.close();        }    }}

二 URLConnection

此類用于在java代碼中模擬瀏覽器組成http協議向服務發請求(get/post)。

代碼:package cn.hx;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class OneServlet extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse resp)            throws ServletException, IOException {        String name = request.getParameter("name");        System.err.println("這是get、、、、"+name);        resp.setContentType("text/html;charset=UTF-8");        resp.getWriter().print("你好:"+name);    }    public void doPost(HttpServletRequest request, HttpServletResponse resp)            throws ServletException, IOException {        request.setCharacterEncoding("UTF-8");        String name = request.getParameter("name");        System.err.println("這是post請求......."+name);        resp.setContentType("text/html;charset=UTF-8");        resp.getWriter().print("你好:"+name);    }}

用urlconnection訪問oneSerlvet

package cn.demo;import java.io.File;import java.io.InputStream;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.URL;import org.junit.Test;public class Demo {    /**     * 發送get請求     * @throws Exception     */    @Test    public void testConn() throws Exception{        //第一步:聲明url        String urlPath = "http://localhost:6666/day22_cos/OneServlet?name=Jack";        //第二步:聲明URL對象        URL url = new URL(urlPath);        //第三步:從url上獲取連接        HttpURLConnection con=  (HttpURLConnection) url.openConnection();         //第四步:設置訪問的類型        con.setRequestMethod("GET");         //第五步:設置可以向服務器發信息。也可以從服務器接收信息        con.setDoInput(true); //也可以從服務器接收信息        con.setDoOutput(true); //設置可以向服務器發信息        //第六步:連接        con.connect();        //7:檢查連接狀態        int code = con.getResponseCode();         if(code==200){            //8:從服務器讀取數據            InputStream in = con.getInputStream();            byte[] b = new byte[1024];            int len = 0;            while((len=in.read(b))!=-1){                String s = new String(b,0,len,"UTF-8");                System.err.print(s);            }        }        //9:斷開        con.disconnect();    }    /**     * 以下發送post請求     */    @Test    public void post() throws Exception{        //第一步:聲明url        String urlPath = "http://localhost:6666/day22_cos/OneServlet";        //第二步:聲明URL對象        URL url = new URL(urlPath);        //第三步:從url上獲取連接        HttpURLConnection con=  (HttpURLConnection) url.openConnection();        //第四步:設置訪問的類型        con.setRequestMethod("POST");        con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");         //第五步:設置可以向服務器發信息。也可以從服務器接收信息        con.setDoInput(true);//設置可以向服務器發信息        con.setDoOutput(true);//也可以從服務器接收信息        //第六步:發信息        //獲取輸出流        OutputStream out = con.getOutputStream();        out.write("name=張三".getBytes("UTF-8"));                         //7:檢查連接狀態        int code = con.getResponseCode();        if(code==200){            //8:從服務器讀取數據            InputStream in = con.getInputStream();            byte[] b = new byte[1024];            int len = 0;            while((len=in.read(b))!=-1){                String s = new String(b,0,len,"UTF-8");                System.err.print(s);            }        }        //9:斷開        con.disconnect();    }}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 门头沟区| 新沂市| 静宁县| 宁明县| 大丰市| 扬中市| 乐业县| 桂东县| 泸水县| 丰城市| 雷山县| 津市市| 邵武市| 塔城市| 民丰县| 宁津县| 安乡县| 清远市| 娄烦县| 农安县| 黄大仙区| 德江县| 子洲县| 陈巴尔虎旗| 雷波县| 惠来县| 兴和县| 珲春市| 四会市| 呼图壁县| 晋江市| 泌阳县| 疏附县| 乡宁县| 神池县| 滦南县| 甘肃省| 漳浦县| 东平县| 兰西县| 岳西县|