星空网 > 软件开发 > Java

正常下载与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();  }}

 




原标题:正常下载与URLConnection 六(64)

关键词:URL

URL
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

操作指南:JUMIA主要站点的禁售产品清单:https://www.ikjzd.com/articles/97152
亚马逊店铺排名规则详解!:https://www.ikjzd.com/articles/97153
拼多多的多多国际店铺怎么批量上传海淘商品?:https://www.ikjzd.com/articles/97154
亚马逊Review破解新的出单难吗?:https://www.ikjzd.com/articles/97158
亚马逊review这么重要,究竟有什么意义?:https://www.ikjzd.com/articles/97159
如何写一条老外都爱搜的标题?:https://www.ikjzd.com/articles/9716
云蒙山风景区介绍-云蒙山风景区游玩攻略:https://www.vstour.cn/a/407228.html
一年拿下两轮融资,宠物大卖在细分市场狂飙!:https://www.kjdsnews.com/a/1841039.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流