javaバックグラウンドでbase 64文字列を画像として保存する方法
本論文では、javaバックグラウンドでbase 64文字列を画像として保存する方法を紹介します。
直接コード:
直接コード:
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:\\tupian\\a.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:\\tupian\
ew.jpg";//
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
}
catch (Exception e)
{
return false;
}
}
}
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。