/** * 上传图片获取图片base64码,然后解码,然后转成字节数组,以流的形式输出到本地 * @return * @throws Exception */ @RequestMapping(value = "/uploadYujhImg1", method = RequestMethod.POST) @ResponseBody public EntityServiceResponse<Map<String, String>> uploadYujhImg1(String fileImg) throws Exception { logger.info("uploadYujh"+fileImg); String imgBase64 = fileImg.replaceAll("data:image/png;base64,",""); BASE64Decoder d = new BASE64Decoder(); byte[] data = d.decodeBuffer(imgBase64); FileOutputStream os = new FileOutputStream("D:\\tupian\\d.jpg"); os.write(data); os.close(); return null; } ///
public static void main (String arg[]) throws IOException {//读取本地图片输入流 //将图片文件转化为字节数组字符串,并对其进行Base64编码处理
//读取本地图片输入流
//将图片文件转化为字节数组字符串,并对其进行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();
System.out.println(encoder.encode(data));//返回Base64编码过的字节数组字符串
}
base64和byte[]相互转换
最新推荐文章于 2024-05-04 15:09:43 发布