1.导入jar包:commons-io-1.4.jar

2.index.jsp:

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>文件上传</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<div><a href="${pageContext.request.contextPath}/showPic.jsp">查看图片</a></div>
</body>
</html>

3.showPic.jsp

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>文件上传</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<div>
<img alt="图片" src="${pageContext.request.contextPath}/ShowPictureServlet?fileName=fanfan.jpg">
</div>
</body>
</html>

4.ShowPictureServlet.java

 pacgake com.pearl.util;

 1 import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream; import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class ShowPictureServlet extends HttpServlet { public void destroy() {
super.destroy();
} public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//文件路径
String picFolder = "E:/upload/";
String fileName = request.getParameter("fileName");
if(fileName!=null && !fileName.equals("")){
String mimeType = "image/gif";
//设置content类型
response.setContentType(mimeType);
//设置大小
File file = new File(picFolder + fileName);
response.setContentLength((int) file.length());
//打开文件并输出
FileInputStream inputStream = new FileInputStream(file);
OutputStream out = response.getOutputStream(); //把文件复制到输出流
byte[] data = new byte[1024];
int count = 0;
while ((count=inputStream.read(data))>=0){
out.write(data, 0, count);
}
inputStream.close();
out.close();
}
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} public void init(ServletConfig config) throws ServletException {
super.init(config);
} }

5.web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>ShowPictureServlet</servlet-name>
<servlet-class>com.pearl.util.ShowPictureServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>ShowPictureServlet</servlet-name>
<url-pattern>/ShowPictureServlet</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

6.完成。

Servlet实现图片读取显示的更多相关文章

  1. C# 图片保存到数据库和从数据库读取图片并显示

    图片保存到数据库的方法: public void imgToDB(string sql)        {   //参数sql中要求保存的imge变量名称为@images            //调 ...

  2. 【Android】读取sdcard卡上的全部图片而且显示,读取的过程有进度条显示

    尽管以下的app还没有做到快图浏览.ES文件浏览器的水平,遇到大sdcard还是会存在读取过久.内存溢出等问题,可是基本思想是这种. 例如以下图.在sdcard卡上有4张图片, 打开app,则会吧sd ...

  3. js读取本地图片并显示

    抄自 http://blog.csdn.net/qiulei_21/article/details/52785191 js读取本地图片并显示 第一种方法比较好 版权声明:本文为博主原创文章,未经博主允 ...

  4. 上传图片到数据库,读取数据库中图片并显示(C#)

    from:http://blog.csdn.net/bfcady/article/details/2622701 思路:建立流对象,将上传图片临时保存到byte数组中,再用SQL语句将其保存到数据库中 ...

  5. Delphi 读取 c# webservice XML的base64编码图片字符串转化图片并显示

    Delphi 读取 c# webservice XML的base64编码图片字符串转化图片并显示 在 开发中遇到应用c#及asp.net的在的webservice 保存图片并以xml文件形式现实出来 ...

  6. servlet跳转页面后图片不显示

    我是用图片的相对路径,原先直接打开jsp的话图片是可以正常显示的,通过servlet跳转之后图片就显示不出来了 后来发现是图片路径的问题, 我是将图片放在WebRoot里面自己创建的img中,原先图片 ...

  7. QT+OpenGL读取显示图片,OpenGL在QT里的刷新问题(好几篇)

    GLuint readImage(char *filename){    GLuint tex_id;    GLint alignment; QImage tex, buf;    buf.load ...

  8. JSP读取数据库二进制图片并显示

    用JSP从数据库中读取二进制图片并显示在网页上 环境mysql+tomcat: 先在mysql下建立如下的表. 并存储了二进制图像(二进制格式存储图片可以参考我的另一篇博客:https://www.c ...

  9. PHP 读取文件夹(比如某共享文件夹)中的图片并显示

    1.获取文件夹下图片public function albumList(){ $share_url = input('path'); $files = getImgList($share_url); ...

随机推荐

  1. js根据数据条数与每页多少条数据计算页数,js前端计算页数

    /**  *根据数据条数与每页多少条数据计算页数   * totalnum 数据条数  * limit 每页多少条  */ function pageCount (totalnum,limit){   ...

  2. js的Map实例

    1.创建实例 let map= new Map(); // 创建 2.对map的写入 // 要添加的对象 let obj1 = {name:'张三', sex:'boy',age: 21}; let ...

  3. ES6内建对象的继承

    在ES6之前, JS是无法通过继承的方式创建属于自己的特殊数组的, 也就是说可以使用原型链来继承数组的一些方法, 但是某些返回一个数组的方法返回的值还是一个Array的实例, 例如slice, len ...

  4. jmeter 测试 websocket 接口

    https://www.cnblogs.com/penghaihang/p/7724830.html(请求的亲测ok) https://www.jianshu.com/p/bb8b3e928607 h ...

  5. ABAP DEMO篇21 选择屏幕显示说明TEXT

    实现方式1: *&---------------------------------------------------------------------**& Report YCX ...

  6. ABAP Memory ID

    转自:https://blog.csdn.net/lyq123333321/article/details/52659114 (一)          Difference Between SAP a ...

  7. 隐藏Nginx、Apache、PHP的版本号

    Nginx: 在nginx配置文件nginx.conf中,加入以下代码: server_tokens off; Apache: 在apache配置文件httpd.conf中,加入以下代码: Serve ...

  8. (十五)Centos之安装jdk

    一.手动解压JDK的压缩包,然后设置环境变量 1 下载linux版的jdk http://www.oracle.com/technetwork/java/javase/downloads/jdk8-d ...

  9. idea的enable auto-import/内存设置

    设置pom.xml依赖的自动导入 设置idea的memory heap On the Help menu, click Edit Custom VM Options. Set the -Xmx opt ...

  10. Ubuntu LVS DR模式生产环境部署

    1.环境说明 系统版本:ubuntu14.04 LVS服务器:14.17.64.3 真实服务器:14.17.64.4-12 VIP:14.17.64.13 部署目的:用户请求14.17.64.13的报 ...