Download pictures to a simple instance of the client and server based on the URL _java

Source: Internet
Author: User

1. Save to server

Save from the path to the server on which the project resides.

String imgurl= "";//Picture address
    try {
      //construct URL url url
      = new URL (imgurl);
      Open connection
      urlconnection con = url.openconnection ();
      Input stream
      InputStream is = Con.getinputstream ();
      1K data buffering
      byte[] bs = new byte[1024];
      The length of data read
      int len;
      Output file stream
      outputstream os = new FileOutputStream ("c:\\image.jpg");//save path
      //start read while (
      len = Is.read ( BS))!=-1) {
        os.write (BS, 0, Len);
      }
      Complete, close all links
      os.close ();
      Is.close ();
    } catch (Malformedurlexception e) {
      e.printstacktrace ();
    } catch (FileNotFoundException e) {
      E.printstacktrace ();
    } catch (IOException e) {
      e.printstacktrace ();
    }

2. Save to Local

Saved to local as a browser download.

String Imgurl= "";//url address string fileName = imgurl.substring (Imgurl.lastindexof ('/') + 1);
    Bufferedinputstream is = null;
    Bufferedoutputstream OS = null;
      try {URL url = new URL (imgurl); 
      This.getservletresponse (). setContentType ("application/x-msdownload;"); This.getservletresponse (). SetHeader ("Content-disposition", "attachment; 
      Filename= "+ New String (Filename.getbytes (" Utf-8 ")," iso8859-1 "));      
      This.getservletresponse (). SetHeader ("Content-length", String.valueof (Url.openconnection (). GetContentLength ()));
      is = new Bufferedinputstream (Url.openstream ()); 
      OS = new Bufferedoutputstream (This.getservletresponse (). Getoutputstream ()); 
      byte[] buff = new byte[2048]; 
      int bytesread; 
      while ( -1!= (bytesread = is.read (buff, 0, buff.length))) {os.write (buff, 0, bytesread); 
      } if (is!= null) is.close ();
    if (OS!= null) os.close ();
     catch (Malformedurlexception e) { E.printstacktrace ();
    catch (Unsupportedencodingexception e) {e.printstacktrace ();
    catch (IOException e) {e.printstacktrace (); }

The above is based on the URL to download pictures to the client, server simple example is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.