[Java] I/O operations-download images from image addresses

Source: Internet
Author: User
Document directory
  • Download source code:
Download the image from the image address to read the content of the given image file. Use fileinputstream
public static byte[] mReaderPicture(String filePath) {byte[] arr = null;try {File file = new File(filePath);FileInputStream fReader = new FileInputStream(file);arr = new byte[1024*100];fReader.read(arr);} catch (Exception  e) {// TODO: handle exceptione.printStackTrace();}return arr;}

Create a New Graph Based on the byte array.

Public static void mwriterpicture (string newfilename, byte [] B) {try {file = new file (newfilename); fileoutputstream fstream = new fileoutputstream (File); fstream. write (B); fstream. close (); system. out. println ("image created successfully" + B. length);} catch (exception e) {// todo: handle exception }}

Returns the byte []

Public static byte [] mreaderpicturetointernet (string strur1) {byte [] imgdata = NULL; Url URL; try {url = new URL (strur1); urlconnection connection = URL. openconnection (); int length = (INT) connection. getcontentlength (); inputstream is = connection. getinputstream (); If (length! =-1) {imgdata = new byte [length]; byte [] temp = new byte [500*1024]; int readlen = 0; int destpos = 0; while (readlen = is. read (temp)> 0) {system. arraycopy (temp, 0, imgdata, destpos, readlen); // arraycopy (Object SRC, int srcpos, object DEST, int destpos, int length) // copy an array from the specified source array, and copy from the specified position to the specified position of the target array to the end of destpos + = readlen ;}} return imgdata ;} catch (exception e) {// todo auto-generated catch blocke. printstacktrace ();} return imgdata ;}

Directly retrieve images from a specified URL

public static void DownPictureToInternet(String filePath,String strUr1){try {URL url = new URL(strUr1);InputStream fStream = url.openConnection().getInputStream();int b = 0;FileOutputStream fos = new FileOutputStream(new File(filePath));while ((b=fStream.read())!=-1) {fos.write(b);}fStream.close();fos.close();} catch (Exception e) {e.printStackTrace();}}

Application:

Public static void main (string [] ARGs) {mwriterpicture ("test/1.jpg", mreaderpicture (" Res/me.jpg "); mwriterpicture (" test/2.jpg ", mreaderpicturetointernet ("http://pic2.desk.chinaz.com/file/201209/7/qinghimingyue4_p.jpg"); downpicturetointernet ("test/download .jpg", "http://img3.100bt.com/upload/ttq/20130205/1360069663700.jpg ");}
Source code download: http://download.csdn.net/detail/oyuntaolianwu/5804447

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.