Base64 transcoding and Web image download

Source: Internet
Author: User

import Sun.misc.BASE64Encoder; 

Import Java.io.File;

Import Java.io.fileinputstream;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.inputstream;import Java.net.url;import java.net.URLConnection;

/**

* @Title: ImgMakeBase64* @Description: Image to Base64 code* @param filePath Network path full name

* @param fName file name

* @return String return type* @author: Konanconan* Create at:2015 November 13 4:54:29*/private string ImgMakeBase64 (String filepath,string fName) {Properties props = null;try {Props = Propertiesloaderutils.loadallproperties ("configs.properties");} catch (IOException e) {Loggerutils.hr11loginfo ("Configuration file configs.properties Read failed", e);}String path = (string) props.get ("filepath");String filepath1= "";try {filePath1 = Download (filepath,fname,path+fname.substring (Fname.lastindexof ("/") +1,fname.length ()));} catch (Exception E1) {TODO auto-generated Catch blockE1.printstacktrace ();}File Imgfile = new file (filePath1);//Picture to be processedInputStream in = null;byte[] data = null;Reading a picture byte arraytry {in = new FileInputStream (imgfile);data = new byte[in.available ()];In.read (data);In.close ();} catch (IOException e) {E.printstacktrace ();} file F = new file (filePath1); F.delete ();BASE64 encoding of byte arraysBase64encoder encoder = new Base64encoder ();return Encoder.encode (data);//returns BASE64 encoded byte array stringThe public string download (String urlstring, String filename,string savepath) throws Exception {Creates a URL object based on the string form.URL url = new URL (urlstring);A URLConnection object that is used to read and write resources referenced by this URLURLConnection con = url.openconnection ();Get an input streamInputStream is = Con.getinputstream ();An output object is actually listedFileOutputStream fos = new FileOutputStream (Savepath);An byte[] array that reads multiple bytes at a timebyte[] bt = new byte[200];The number of bytes to receive per readint b = 0;Cyclic judgment, if the number of reads B is empty, then the Is.read () method returns-1, please refer to the InputStream read ();while ((b = Is.read (BT))! =-1) {Writes an object to the corresponding fileFos.write (BT, 0, B);}Refresh StreamFos.flush ();Close the streamFos.close ();Is.close ();return savepath; }

Base64 transcoding and Web image download

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.