JAvaがピクチャを読み出してバイナリ文字列に変換する実現方法
2471 ワード
この例の目的は、oracleデータベースへのblobフィールドの挿入をテストすることです.
--
以上の考え方で書いたテスト
しかしavailable()には一定の制限があります.
適切にするために、以下の方法を採用することを深刻に提案します.
まとめ
以上述べたのは編集者が皆さんに紹介したjavaが画像を読み取り、バイナリ文字列に変換したことで、皆さんに役に立つことを望んでいます.もし皆さんが何か疑問があれば、私にメッセージを送ってください.編集者はすぐに皆さんに返事します.
public static String getImgStr(String imgFile){
// , Base64
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();
}
return new String(Base64.encodeBase64(data));
}
--
以上の考え方で書いたテスト
public class ReadImageTest {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream(new File("C:\\Users\\luzhifei\\Pictures\\hc_logo.png"));
String picStr="";
byte[] read = null;
int len = 0;
read= new byte[fis.available()];
fis.read(read);
String baseStr= Base64.getEncoder().encodeToString(read);
//System.out.println( baseStr);
byte[] op= Base64.getDecoder().decode(baseStr);
// System.out.println(new String(op));
FileOutputStream fos = new FileOutputStream(new File("d:\\temp\\1.jpg"));
fos.write(op,0,op.length );
fos.flush();
fos.close();
}
}
しかしavailable()には一定の制限があります.
適切にするために、以下の方法を採用することを深刻に提案します.
public static void imageToBase64Str() throws IOException{
FileInputStream fis = new FileInputStream(new File("C:\\Users\\luzhifei\\Pictures\\hc_logo.png"));
byte[] read = new byte[1024];
int len = 0;
List blist=new ArrayList();
int ttllen=0;
while((len = fis.read(read))!= -1){
byte[] dst=new byte[len];
System.arraycopy(read, 0, dst, 0, len);
ttllen+=len;
blist.add(dst);
}
fis.close();
byte[] dstByte=new byte[ttllen];
int pos=0;
for (int i=0;i
まとめ
以上述べたのは編集者が皆さんに紹介したjavaが画像を読み取り、バイナリ文字列に変換したことで、皆さんに役に立つことを望んでいます.もし皆さんが何か疑問があれば、私にメッセージを送ってください.編集者はすぐに皆さんに返事します.