自己写的方式最后结果,新生成图片打开失败,提示图片错误
try { File file = new File("D:\chengdu.jpg"); FileInputStream in = new FileInputStream(file); FileOutputStream out = new FileOutputStream("D:\ceshi.jpg"); byte[] bytes = new byte[1024]; int len = -1; while((len = in.read(bytes)) != -1) { // out.write(bytes, 0, len); } String cont = EncodeUtils.base64Encode(bytes); System.out.println(cont); System.out.println(Arrays.toString(bytes)); byte[] ob = EncodeUtils.base64Decode(cont); System.out.println(EncodeUtils.base64Encode(ob)); System.out.println(Arrays.toString(ob)); out.write(ob); in.close(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
查找资料,换成另一种方式,新生成图片成功
byte[] data = null; FileInputStream is = null; try { is = new FileInputStream(new File("D:\chengdu.jpg")); data = new byte[is.available()]; is.read(data); base64Image = new String(Base64.encode(data)); } catch (Exception e) { e.printStackTrace(); } finally { if (null != is) { try { is.close(); } catch (Exception e) { e.printStackTrace(); } } } File file1=new File("D:\ceshi.jpg"); byte[] decoByte=Base64.decode(base64Image); for (int i = 0; i < decoByte.length; ++i) { if (decoByte[i] < 0) { decoByte[i] += 256; } } try { FileUtils.writeByteArrayToFile(file1, decoByte); } catch (IOException e) { e.printStackTrace(); }
具体原因待业余时间探索,特此记录