当前位置:编程学习 > JAVA >>

为什么这段代码无法显示图片啊

import java.io.IOException;   
import javax.servlet.ServletException;   
import javax.servlet.http.HttpServletRequest;   
import javax.servlet.http.HttpServletResponse;   
  
import java.io.*;   
import javax.servlet.*;   
import javax.servlet.http.*;   
import com.sun.image.codec.jpeg.*;//sun公司仅提供了jpg图片文件的编码api   
import javax.imageio.stream.*;   
import java.awt.*;   
import java.awt.image.BufferedImage;   
  
/**  
 *   
 * 显示文件的Servlet  
 * 负责从本地文件中读取图片,  
 *   
 */  
public class ShowPic extends javax.servlet.http.HttpServlet implements  
        javax.servlet.Servlet {   
  
    private static final String GIF = "image/gif;charset=GB2312";// 设定输出的类型   
  
    private static final String JPG = "image/jpeg;charset=GB2312";   
  
    /*  
     * (non-Java-doc)  
     *   
     * @see javax.servlet.http.HttpServlet#HttpServlet()  
     */  
    public ShowPic() {   
        super();   
    }   
  
    /*  
     * (non-Java-doc)  
     *   
     * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,  
     *      HttpServletResponse response)  
     */  
    protected void doGet(HttpServletRequest request,   
            HttpServletResponse response) throws ServletException, IOException {   
        doPost(request, response);   
    }   
  
    /*  
     * (non-Java-doc)  
     *   
     * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,  
     *      HttpServletResponse response)  
     */  
    protected void doPost(HttpServletRequest request,   
            HttpServletResponse response) throws ServletException, IOException {   
        String spec = request.getParameter("spec");// 输出图片的类型的标志   
  
        String imagePath = "D:\\temp\\1.jpg";   
  
        response.reset();   
  
        OutputStream output = response.getOutputStream();// 得到输出流   
        if (imagePath.toLowerCase().endsWith(".jpg"))// 使用编码处理文件流的情况:   
        {   
            response.setContentType(JPG);// 设定输出的类型   
            // 得到图片的真实路径   
  
            // 得到图片的文件流   
            InputStream imageIn = new FileInputStream(new File(imagePath));   
            // 得到输入的编码器,将文件流进行jpg格式编码   
            JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn);   
            // 得到编码后的图片对象   
            BufferedImage image = decoder.decodeAsBufferedImage();   
            // 得到输出的编码器   
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);   
            encoder.encode(image);// 对图片进行输出编码   
            imageIn.close();// 关闭文件流   
        }   
        if (imagePath.toLowerCase().endsWith(".gif"))// 不使用编码处理文件流的情况:   
        {   
            response.setContentType(GIF);   
            ServletContext context = getServletContext();// 得到背景对象   
            InputStream imageIn = context.getResourceAsStream(imagePath);// 文件流   
            BufferedInputStream bis = new BufferedInputStream(imageIn);// 输入缓冲流   
            BufferedOutputStream bos = new BufferedOutputStream(output);// 输出缓冲流   
            byte data[] = new byte[4096];// 缓冲字节数   
            int size = 0;   
            size = bis.read(data);   
            while (size != -1) {   
                bos.write(data, 0, size);   
                size = bis.read(data);   
            }   
            bis.close();   
            bos.flush();// 清空输出缓冲流   
            bos.close();   
        }   
        output.close();   
    }   
}





<servlet>  
    <servlet-name>ShowPic</servlet-name>  
    <display-name>ShowPic</display-name>  
    <description></description>  
    <servlet-class>  
    cn.ccb.zlgz.web.servlet.ShowPic</servlet-class>  
  </servlet>  
  <servlet-mapping>  
    <servlet-name>ShowPic</servlet-name>  
    <url-pattern>/ShowPic</url-pattern>  
  </servlet-mapping>  






Html代码  
<html>  
<body>  
<img src="ShowPic?spec=2"></a>  
</body>  
</html>  


http://tntxia.iteye.com/blog/826193源地址。





--------------------编程问答-------------------- 你设置一个断点,看有没有进入你的Servlet中,有报错吗?没有报错说明你配置的路径不对 --------------------编程问答-------------------- java.io.IOException: Stream closed
at java.io.BufferedInputStream.getInIfOpen(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at java.io.FilterInputStream.read(Unknown Source)
at max.tool.ShowPic.doPost(ShowPic.java:88)
at max.tool.ShowPic.doGet(ShowPic.java:45)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at max.com.ModifyCode.doFilter(ModifyCode.java:23)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:186)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Unknown Source) --------------------编程问答-------------------- <servlet>  
    <servlet-name>ShowPic</servlet-name>  
    <display-name>ShowPic</display-name>      <description></description>  
    <servlet-class>  
    max.tool.ShowPic</servlet-class>  
  </servlet>  
  <servlet-mapping>  
    <servlet-name>ShowPic</servlet-name>  
    <url-pattern>/ShowPic</url-pattern>  
  </servlet-mapping> 
</web-app>
这里也报错啊。。 --------------------编程问答-------------------- 路径还是max.tool.ShowPic这个把。。没有什么报错咯
补充:Java ,  Java相关
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,