欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JSP中通过Servlet 将服务器硬盘图片并展示到浏览器

程序员文章站 2023-11-13 15:14:22
jsp中通过servlet 将服务器硬盘图片并展示到浏览器 其实这个实例非常简单,但是今天有人问我了,而且我也写了个小例子,就顺便发上来吧!  在浏览器显...

jsp中通过servlet 将服务器硬盘图片并展示到浏览器

其实这个实例非常简单,但是今天有人问我了,而且我也写了个小例子,就顺便发上来吧! 

在浏览器显示一张图片,使用标签

<img src=""> 

img 元素向网页中嵌入一幅图像。

请注意,从技术上讲,<img> 标签并不会在网页中插入图像,而是从网页上链接图像。<img> 标签创建的是被引用图像的占位空间。

<img> 标签有两个必需的属性:src 属性 和 alt 属性。 

html 与 xhtml 之间的差异
在 html 中,<img> 标签没有结束标签。
在 xhtml 中,<img> 标签必须被正确地关闭。
在 html 4.01 中,不推荐使用 image 元素的 "align"、"border"、"hspace" 以及 "vspace" 属性。
在 xhtml 1.0 strict dtd 中,不支持 image 元素的 "align"、"border"、"hspace" 以及 "vspace" 属性。 

src的路径有很多:

指向其他站点(比如 src="http://www.******.com/***.jpg")

指向站点内的文件(比如 src="/i/image.gif")

许多新手忽略了一点是,其实img只是告诉浏览器这里要现实图片,而浏览器通过路径去获得图片的数据流然后进行显示

简单来说,src其实就是浏览器走了一个请求,然后这个请求返回图片的数据流给浏览器而已

所以,src同样可以是请求,可以是servlet也可以是action,这里我们用servlet来做一个简单示例 

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%>" rel="external nofollow" > 
  <title>图片显示</title> 
 </head> 
 <body> 
  <img src="<%=basepath %>servlet/imageshowservlet"> 
 </body> 
</html> 

 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> 
  <servlet-name>imageshowservlet</servlet-name> 
  <servlet-class>servlet.imageshowservlet</servlet-class> 
 </servlet> 
 <servlet-mapping> 
  <servlet-name>imageshowservlet</servlet-name> 
  <url-pattern>/servlet/imageshowservlet</url-pattern> 
 </servlet-mapping> 
 <welcome-file-list> 
  <welcome-file>index.jsp</welcome-file> 
 </welcome-file-list> 
</web-app> 
 

servlet非常简单:

package servlet; 
 
import java.io.bytearrayoutputstream; 
import java.io.file; 
import java.io.fileinputstream; 
import java.io.ioexception; 
import java.io.inputstream; 
import java.io.outputstream; 
import javax.servlet.servletexception; 
import javax.servlet.http.httpservlet; 
import javax.servlet.http.httpservletrequest; 
import javax.servlet.http.httpservletresponse; 
 
/** 
 * @说明 该servlet将本地硬盘的图片输入管道中 
 * @author cuisuqiang 
 * @version 1.0 
 * @since 
 */ 
@suppresswarnings("serial") 
public class imageshowservlet extends httpservlet { 
 
  @override 
  protected void service(httpservletrequest request, httpservletresponse response) 
      throws servletexception, ioexception { 
    outputstream os = response.getoutputstream(); 
    file file = new file("c:\\abc.jpg"); 
    fileinputstream fips = new fileinputstream(file); 
    byte[] btimg = readstream(fips); 
    os.write(btimg); 
    os.flush(); 
  } 
   
  /** 
   * 读取管道中的流数据 
   */ 
  public byte[] readstream(inputstream instream) { 
    bytearrayoutputstream bops = new bytearrayoutputstream(); 
    int data = -1; 
    try { 
      while((data = instream.read()) != -1){ 
        bops.write(data); 
      } 
      return bops.tobytearray(); 
    }catch(exception e){ 
      return null; 
    } 
  } 
} 
 

就是获取本地硬盘的文件的字节流,然后写入到管道中而已!

 以上就是jsp中通过servlet 将服务器硬盘图片并展示到浏览器的实例,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!