使用java,将图片转换成BYTE数组、及将数组转换成图片,进行图片的远程传输
参考:http://blog.csdn.net/huang9012/article/details/18241539
代码如下:
package com.third.demo; import java.io.ByteArrayOutputStream; import java.io.File; import javax.imageio.stream.FileImageInputStream; import javax.imageio.stream.FileImageOutputStream; import org.json.JSONObject; public class CreatUploadJson { public static void buildJson() throws Exception { // 图片转换成 BYTE数组 byte[] data = null; FileImageInputStream input = new FileImageInputStream(new File("d://7.jpg")); ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int numBytesRead = 0; while ((numBytesRead = input.read(buf)) != -1) { output.write(buf, 0, numBytesRead); } data = output.toByteArray(); output.close(); input.close(); // JSONObject jo = new JSONObject(); // jo.put("agentId", "001"); // jo.put("picType", "1"); // jo.put("picName", "素材名称"); // jo.put("picByte", data); // // System.out.println(jo.toString()); // byte数组 转换成 图片 FileImageOutputStream imageOutput = new FileImageOutputStream(new File("e://1.jpg")); imageOutput.write(data, 0, data.length); imageOutput.close(); } /** * @param args */ public static void main(String[] args) { try { buildJson(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
java--图片和BYTE数组的那些事
时间: 2024-10-06 00:53:41