Servlet实现图片读取显示
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实现图片读取显示的更多相关文章
- C# 图片保存到数据库和从数据库读取图片并显示
图片保存到数据库的方法: public void imgToDB(string sql) { //参数sql中要求保存的imge变量名称为@images //调 ...
- 【Android】读取sdcard卡上的全部图片而且显示,读取的过程有进度条显示
尽管以下的app还没有做到快图浏览.ES文件浏览器的水平,遇到大sdcard还是会存在读取过久.内存溢出等问题,可是基本思想是这种. 例如以下图.在sdcard卡上有4张图片, 打开app,则会吧sd ...
- js读取本地图片并显示
抄自 http://blog.csdn.net/qiulei_21/article/details/52785191 js读取本地图片并显示 第一种方法比较好 版权声明:本文为博主原创文章,未经博主允 ...
- 上传图片到数据库,读取数据库中图片并显示(C#)
from:http://blog.csdn.net/bfcady/article/details/2622701 思路:建立流对象,将上传图片临时保存到byte数组中,再用SQL语句将其保存到数据库中 ...
- Delphi 读取 c# webservice XML的base64编码图片字符串转化图片并显示
Delphi 读取 c# webservice XML的base64编码图片字符串转化图片并显示 在 开发中遇到应用c#及asp.net的在的webservice 保存图片并以xml文件形式现实出来 ...
- servlet跳转页面后图片不显示
我是用图片的相对路径,原先直接打开jsp的话图片是可以正常显示的,通过servlet跳转之后图片就显示不出来了 后来发现是图片路径的问题, 我是将图片放在WebRoot里面自己创建的img中,原先图片 ...
- QT+OpenGL读取显示图片,OpenGL在QT里的刷新问题(好几篇)
GLuint readImage(char *filename){ GLuint tex_id; GLint alignment; QImage tex, buf; buf.load ...
- JSP读取数据库二进制图片并显示
用JSP从数据库中读取二进制图片并显示在网页上 环境mysql+tomcat: 先在mysql下建立如下的表. 并存储了二进制图像(二进制格式存储图片可以参考我的另一篇博客:https://www.c ...
- PHP 读取文件夹(比如某共享文件夹)中的图片并显示
1.获取文件夹下图片public function albumList(){ $share_url = input('path'); $files = getImgList($share_url); ...
随机推荐
- ISO/IEC 9899:2011 条款6.3.1——算术操作数
6.3.1 算术操作数 6.3.1.1 布尔.字符以及整数 1.每个整数类型具有一个整数转换等级,如下定义: ——两个带符号的整数类型都不应该具有相同等级,即使它们具有相同的表示. ——一个带符号整数 ...
- Qt使用boost库
1.在官网下载boost库 boost_1_70_0.zip 2.将你的Qt的工具目录(有gcc.exe)设置环境变量.(比如F:\Qt592\Tools\mingw530_32\bin) 3.在命令 ...
- 编译安装python3事出错:
configure: error: no acceptable C compiler found in $PATH 问题解决 解决方法: yum intall gcc -y
- thinkphp3.2.2 没有定义数据库配置
出现这个问题,温习下tp配置多个数据库 <?php return array( //默认数据库 'DB_TYPE' => 'mysql', // 数据库类型 'DB_HOST' => ...
- hadoop第一次面到hr(品友互动)
第一次“北漂” 准备了一个星期的Hadoop,把林子雨老师的视频刷了一遍,翻出了好久没用的小本本,密密麻麻的记了一大堆.刷了网上能找到的Hadoop的所有面试题(这个真的很重要) 然后,启程,北上,还 ...
- 【pep8规范】arc diff 不符合 pep 8 规范
arc land 的时候,arc报错提示代码不符合pep8规范: 1.单行代码过长(括号中的换行不需要加 /) python代码换行加 / https://blog.csdn.net/codechel ...
- Cas(01)——简介
Cas的全称是Centeral Authentication Service,是对单点登录SSO(Single Sign On)的一种实现.其由Cas Server和Cas Client两部分组成,C ...
- 【ARTS】01_38_左耳听风-201900729~201900804
ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...
- react如何在网页上编辑并运行代码?
最近想做个能在网站,能在网页上运行代码,并且保存这个组件,看了一下element-react的组件和官方的实例,发现都是可以编辑运行的,因为之前没有这方面的经验,所以看下各位大佬能不能给点意见
- git merge仓
git merge --no-ff branch 合并指定代码 如果有冲突 git mergetool 可视化解决冲突, qa! 全退出 如果修复失败 git checkout branch -f ...