1.数据库存的图片路径,表格导出的时候,需要展示成图片
2.需要将图片路径转化为字节码
3.先获取图片路径
4.转为字节码
5.接收的传参格式‘:
@JsonProperty("product_back_url")
private String productBackUrl;
@Excel(name = "商品背面", width = 15, height = 35, type = 2, imageType = 2)
private byte[] productBackPic;
6.实际取值的地方:
if (StringUtils.isNotBlank(obj.getProductBackUrl())) {
obj.setProductBackPic(HttpUtil.getNetImgByUrl(obj.getProductBackUrl()));
}
public class HttpUtil {标签:return,字节,导出,byte,事宜,inStream,conn,图片 From: https://www.cnblogs.com/ewa-03/p/17479300.html
/**
* 获取网络图片转成字节流
*
* @param strUrl 完整图片地址
* @return 图片资源数组
*/
public static byte[] getNetImgByUrl(String strUrl) {
try {
URL url = new URL(strUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(2 * 1000);
// 通过输入流获取图片数据
InputStream inStream = conn.getInputStream();
// 得到图片的二进制数据
byte[] btImg = readInputStream(inStream);
return btImg;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 从输入流中获取字节流数据
*
* @param inStream 输入流
* @return 图片流
*/
private static byte[] readInputStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
// 设置每次读取缓存区大小
byte[] buffer = new byte[1024 * 10];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
inStream.close();
return outStream.toByteArray();
}