将图片转换成base64 在前端使用标签显示出来
Java 端代码 根据图片绝对路径获取图片的base64编码
public static String getImageStrFromPath(String imgPath) {
InputStream in = null;
byte[] data = null;
// 读取图片字节数组
try {
in = new FileInputStream(imgPath);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
// 返回Base64编码过的字节数组字符串
return encoder.encode(data);
}
---------------------
public static String getImageStrFromPath(String imgPath) {
InputStream in = null;
byte[] data = null;
// 读取图片字节数组
try {
in = new FileInputStream(imgPath);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
// 返回Base64编码过的字节数组字符串
return encoder.encode(data);
}
---------------------
作者:ZksLoveLy
来源:CSDN
原文:https://blog.csdn.net/ZksLoveLy/article/details/80165954
版权声明:本文为博主原创文章,转载请附上博文链接!
Java 端代码 根据图片绝对路径获取图片的base64编码
public static String getImageStrFromPath(String imgPath) {
InputStream in = null;
byte[] data = null;
// 读取图片字节数组
try {
in = new FileInputStream(imgPath);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
// 返回Base64编码过的字节数组字符串
return encoder.encode(data);
}
将返回的字符串放入HTML的<img> 标签中
<img src="data:image/jpg;base64,${String}" class="images" border="5px"/>
---------------------
作者:ZksLoveLy
来源:CSDN
原文:https://blog.csdn.net/ZksLoveLy/article/details/80165954
版权声明:本文为博主原创文章,转载请附上博文链接!
以下才是正常得方案
从后台调取照片在前端显示
<img src="systemadmin/showpicture?url=${pd.merId}_${pd.bankCardNo}_1.jpg" width=400 height=400/>
代码实现
public void showPhoto(HttpServletRequest request,HttpServletResponse response) {
try {
String fileName = request.getParameter("url");//图片名字
String path="E://utilPicture//daikou//"+fileName;
// 以byte流的方式打开文件 d:\1.gif
FileInputStream hFile;
hFile = new FileInputStream(path);
//得到文件大小
int i=hFile.available();
byte data[]=new byte[i];
//读数据
hFile.read(data);
response.setHeader("Content-Type","image/jpeg");
//得到向客户端输出二进制数据的对象
OutputStream toClient=response.getOutputStream();
//输出数据
toClient.write(data);
toClient.flush();
toClient.close();
hFile.close();
} catch (Exception e) {
e.printStackTrace();
}
}
#24
---------------------
作者:爱自由的阿彬
来源:CSDN
原文:https://blog.csdn.net/a1029573879a/article/details/79240150
版权声明:本文为博主原创文章,转载请附上博文链接!
![](https://img.haomeiwen.com/i5548226/a35bfe555d1e8880.png)
只要设置response.setHeader("Content-Type","image/jpeg");就可以直接预览,用注释掉得是下载
网友评论