如何让前端获取到硬盘上的图片并显示出来,不是项目中保存的图片
2017-08-01 15:26
513 查看
@ResponseBody
@RequestMapping(value = "/getImg.act")
public void getImg(HttpServletRequest request, HttpServletResponse response) throws IOException {
OutputStream os = null;
FileInputStream fis = null;
File file = null;
try {
response.setContentType("text/html; charset=UTF-8");
response.setContentType("image/jpeg"); // 设置图片格式格式,这里可以忽略
String advertImg = request.getParameter("advertImg");
// 文件存储相对位置
String uploadOrgPath = ReadSystemUtil.getValue("uploadOrgPath");
file = new File(uploadOrgPath + advertImg);
if (advertImg.equals("") || advertImg.equals("null") || advertImg == null) {
HttpSession session = request.getSession();
ServletContext application = session.getServletContext();
String serverRealPath = application.getRealPath("/");// 得到tomcat路径
fis = new FileInputStream(serverRealPath + "images/weishangchuan.png");
os = response.getOutputStream();
int count = 0;
byte[] buffer = new byte[1024];
while ((count = fis.read(buffer)) != -1)
os.write(buffer, 0, count);
} else if (file.exists()) {
fis = new FileInputStream(file);
os = response.getOutputStream();
int count = 0;
byte[] buffer = new byte[1024];
while ((count = fis.read(buffer)) != -1)
os.write(buffer, 0, count);
} else {
HttpSession session = request.getSession();
ServletContext application = session.getServletContext();
String serverRealPath = application.getRealPath("/");// 得到tomcat路径
fis = new FileInputStream(serverRealPath + "images/default.jpg");
os = response.getOutputStream();
int count = 0;
byte[] buffer = new byte[1024];
while ((count = fis.read(buffer)) != -1)
os.write(buffer, 0, count);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (os != null)
os.close();
if (fis != null)
fis.close();
}
}
通过这里我们可以分析出,想要前端img标签显示电脑中的图片,我们需要利用response进行输出就行了
@RequestMapping(value = "/getImg.act")
public void getImg(HttpServletRequest request, HttpServletResponse response) throws IOException {
OutputStream os = null;
FileInputStream fis = null;
File file = null;
try {
response.setContentType("text/html; charset=UTF-8");
response.setContentType("image/jpeg"); // 设置图片格式格式,这里可以忽略
String advertImg = request.getParameter("advertImg");
// 文件存储相对位置
String uploadOrgPath = ReadSystemUtil.getValue("uploadOrgPath");
file = new File(uploadOrgPath + advertImg);
if (advertImg.equals("") || advertImg.equals("null") || advertImg == null) {
HttpSession session = request.getSession();
ServletContext application = session.getServletContext();
String serverRealPath = application.getRealPath("/");// 得到tomcat路径
fis = new FileInputStream(serverRealPath + "images/weishangchuan.png");
os = response.getOutputStream();
int count = 0;
byte[] buffer = new byte[1024];
while ((count = fis.read(buffer)) != -1)
os.write(buffer, 0, count);
} else if (file.exists()) {
fis = new FileInputStream(file);
os = response.getOutputStream();
int count = 0;
byte[] buffer = new byte[1024];
while ((count = fis.read(buffer)) != -1)
os.write(buffer, 0, count);
} else {
HttpSession session = request.getSession();
ServletContext application = session.getServletContext();
String serverRealPath = application.getRealPath("/");// 得到tomcat路径
fis = new FileInputStream(serverRealPath + "images/default.jpg");
os = response.getOutputStream();
int count = 0;
byte[] buffer = new byte[1024];
while ((count = fis.read(buffer)) != -1)
os.write(buffer, 0, count);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (os != null)
os.close();
if (fis != null)
fis.close();
}
}
通过这里我们可以分析出,想要前端img标签显示电脑中的图片,我们需要利用response进行输出就行了
相关文章推荐
- 如何从网络获取图片显示并保存到SD卡里
- Winform下如何上传图片并显示出来。同时保存到数据库
- [django]用requests从url获取图片(数据类型是bytes)后如何在template中显示出来
- 如何从网络获取图片显示并保存到SD卡里
- 如何从网络获取图片显示并保存到SD卡里
- [VB.NET]如何把一个图片保存到access中,又如何把它读取出来,并在picturebox中显示?
- android如何将生成的图片保存至手机相册并显示出来
- 如何在web项目中使用jfreechart和显示硬盘上指定路径上的图片
- Win form 在pictureBox控件中实现选择图片然后以二进制方式保存到数据库。以及在pictureBox控件显示出来
- 如何将数据库二进制图片保存到硬盘上
- 从网络获取图片并保存到手机相册,保存成功但是相册不显示图片问题
- [原] XAF 如何将数据库中Byte array图片显示出来
- Android实现从网络获取图片显示并保存到SD卡的方法
- 【移动开发】Android相机、相册获取图片显示并保存到SD卡
- vc/mfc获取rgb图像数据后动态显示及保存图片的方法
- 在MVC项目中如何显示图片
- Res目录下资源如图片文件和xml文件资源如何被加载显示出来
- matplotlib如何不显示图片只保存图片
- android保存图片到本地并可以在相册中显示出来
- 上传图片时如何在前端限制图片的大小,获取图片的名称