Normal download and URLConnection 6 (64), urlconnection64

Source: Internet
Author: User

Normal download and URLConnection 6 (64), urlconnection64

Normal download

When the service uses breakpoint download, the response information is 206.

UrlConnection-HttpurlConnection. -Obtain the urlconnection instance through URL.

Normal download example

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) u Rl. openConnection (); con. setRequestMethod ("GET"); con. setDoInput (true); con. connect (); int code = con. getResponseCode (); System. err. println (code); if (code = 200) {// obtain the file size long size = con. getContentLength (); System. err. println ("total size:" + size); // declare the downloaded byte 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 ();}}}

 

2. URLConnection

This class is used to simulate a browser in java code to form an http protocol to send a request (get/post) to the service ).

 

Code: 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 ("this is get," + name); resp. setContentType ("text/html; charset = UTF-8"); resp. getWriter (). print ("Hello:" + name);} public void doPost (HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {request. setCharacterEncoding ("UTF-8"); String name = request. getParameter ("name"); System. err. println ("this is a post request ....... "+ name); resp. setContentType ("text/html; charset = UTF-8"); resp. getWriter (). print ("Hello:" + name );}}

 

 

 

Use urlconnection to access 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 {/*** send get request * @ throws Exception */@ Test public void testConn () throws Exception {// Step 1: declare url String urlPath = "http: // localhost: 6666/day22_cos/OneServlet? Name = Jack "; // Step 2: declare the URL object URL = new url (urlPath); // Step 3: Obtain the link HttpURLConnection con = (HttpURLConnection) URL from the url. openConnection (); // Step 4: Set the access type con. setRequestMethod ("GET"); // Step 5: send messages to the server. You can also receive information con from the server. setDoInput (true); // You can also receive information con from the server. setDoOutput (true); // you can send messages to the server. // Step 6: connect to con. connect (); // 7: Check the connection status int code = con. getResponseCode (); if (code = 200) {// 8: read data from the server 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: disconnect con. disconnect ();}/*** send post request below */@ Test public void post () throws Exception {// Step 1: declare url String urlPath = "http: // localhost: 6666/day22_cos/OneServlet "; // Step 2: declare the URL object URL = new url (urlPath); // Step 3: obtain the link HttpURLConnection con = (HttpURLConnection) url from the url. openConnection (); // Step 4: Set the access type con. setRequestMethod ("POST "); Con. setRequestProperty ("Content-Type", "application/x-www-form-urlencoded"); // Step 5: Set to send messages to the server. You can also receive information con from the server. setDoInput (true); // you can send a message con to the server. setDoOutput (true); // You can also receive information from the server. // Step 6: send information // obtain the output stream OutputStream out = con. getOutputStream (); out. write ("name = James ". getBytes ("UTF-8"); // 7: Check the connection status int code = con. getResponseCode (); if (code = 200) {// 8: read data from the server 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: disconnect con. disconnect ();}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.