javaʵÏÖͼƬÓëbase64×Ö·û´®Ö®¼äµÄת»»
package cn.com;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class Base64Test
{
public static void main(String[] args)
{
String strImg = GetImageStr();
System.out.println(strImg);
GenerateImage(strImg);
}
//ͼƬת»¯³Ébase64×Ö·û´®
public static String GetImageStr()
{//½«Í¼Æ¬Îļþת»¯Îª×Ö½ÚÊý×é×Ö·û´®£¬²¢¶ÔÆä½øÐÐBase64±àÂë´¦Àí
String imgFile = "d:\\test.jpg";//´ý´¦ÀíµÄͼƬ
InputStream in = null;
byte[] data = null;
//¶ÁȡͼƬ×Ö½ÚÊý×é
try
{
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
//¶Ô×Ö½ÚÊý×éBase64±àÂë
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);//·µ»ØBase64±àÂë¹ýµÄ×Ö½ÚÊý×é×Ö·û´®
}
//base64×Ö·û´®×ª»¯³ÉͼƬ
public static boolean GenerateImage(String imgStr)
{ //¶Ô×Ö½ÚÊý×é×Ö·û´®½øÐÐBase64½âÂë²¢Éú³ÉͼƬ
if (imgStr == null) //ͼÏñÊý¾ÝΪ¿Õ
return false;
BASE64Decoder decoder = new BASE64Decoder();
try
{
//Base64½âÂë
byte[] b = decoder.decodeBuffer(imgStr);
for(int i=0;i<b.length;++i)
{
if(b[i]<0)
{//µ÷ÕûÒì³£Êý¾Ý
b[i]+=256;
}
}
//Éú³ÉjpegͼƬ
String imgFilePath = "d:\\222.jpg";//ÐÂÉú³ÉµÄͼƬ
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
}
Ïà¹ØÎĵµ£º
¡¡1.Ê×ÏÈÊǹ¤¾ß±ÈÈçEclipseºÜ·½±ãÁË¡£
¡¡¡¡2.ÓÃwinrarÖ®ÀàµÄ¹¤¾ß£¬°Ñweb-infoĿ¼£¬¼°¸úËüͬ¼¶µÄËùÓÐĿ¼¼°Îļþ£¬´ò°ü³É zipÎļþ¾ÍÐÐÁË£¬È»ºó°ÑÀ©Õ¹Ãû¸Ä³Éwar!
¡¡¡¡3 JarÃüÁ
¡¡¡¡¼Ù¶¨ÓÐÒ»¸öWebÓ¦ÓãºC:\myHome
¡¡¡¡ myHome/WEB-INF/……
¡¡¡¡myHome/files/……
¡¡¡¡myHome/image/… ......
ÊÂʵÉÏÍøÂç±à³Ì¼òµ¥µÄÀí½â¾ÍÊÇÁ½Ì¨¼ÆËã»úÏ໥ͨѶÊý¾Ý¶øÒÑ¡£¶ÔÓÚ³ÌÐòÔ±¶øÑÔ£¬È¥ÕÆÎÕÒ»ÖÖ±à³Ì½Ó¿Ú²¢Ê¹ÓÃÒ»ÖÖ±à³ÌÄ£ÐÍÏà¶Ô¾Í»áÏԵüòµ¥µÄ¶àÁË¡£Java SDKÌṩһЩÏà¶Ô¼òµ¥µÄApiÀ´Íê³ÉÕâЩ¹¤×÷£¬Socket¾ÍÊÇÆäÖÐÖ®Ò»¡£
¶ÔÓÚJava¶øÑÔ¡£ÕâЩApi´æÔÚÓëJava.netÕâ¸ö°üÀïÃæ¡£Òò´ËÖ»Òªµ¼ÈëÕâ¸ö°ü¾Í¿ÉÒÔ×¼±¸ÍøÂç±à³ÌÁË¡£ÍøÂç± ......
ºËÐÄÌáʾ£º±¾ÎÄÃèÊöÁËÔÚJ2meÖпª·¢Ö÷ҪʹÓõÄÍøÂçÁ¬½Ó·½·¨£¬·Ö±ðÏêϸ½éÉÜÁËʹÓÃhttpºÍsocketÁ½ÖÖ·½·¨¡£ HttpConnection Ê×ÏÈÎÒÃÇÏÈÀ´¿´Ò»¸ö¼òµ¥µÄÀý×Ó°É£º Ö÷ÒªÓõ½µÄjava°ü£º javax.microedition.io.*; public String requestGET(String URLString,String URL) throws
±¾ÎÄÃèÊöÁËÔÚJ2meÖпª·¢Ö÷ҪʹÓõÄÍøÂçÁ¬½Ó·½·¨£¬ ......
ÏßÐÔ±í£¬Á´±í£¬¹þÏ£±íÊdz£ÓõÄÊý¾Ý½á¹¹£¬ÔÚ½øÐÐJava¿ª·¢Ê±£¬JDKÒѾΪÎÒÃÇÌṩÁËһϵÁÐÏàÓ¦µÄÀàÀ´ÊµÏÖ»ù±¾µÄÊý¾Ý½á¹¹¡£ÕâЩÀà¾ùÔÚjava.util°üÖС£±¾ÎÄÊÔͼͨ¹ý¼òµ¥µÄÃèÊö£¬Ïò¶ÁÕß²ûÊö¸÷¸öÀàµÄ×÷ÓÃÒÔ¼°ÈçºÎÕýȷʹÓÃÕâЩÀà¡£
Collection
©ÀList& ......