public class ImageShowServlet
extends HttpServlet {
private String STORAGENAME = null; //附件在服务器上的存储路径
public void service(HttpServletRequest request, HttpServletResponse response) throws
IOException, ServletException {
try {
STORAGENAME = request.getParameter("path");//设置文件名和文件所在的物理路径
}
catch (FileNotFoundException ex) {
throw ex;
}
catch (SQLException ex) {
throw new FileNotFoundException(ex.toString());
}
File file = null;
try {
file = new File(STORAGENAME);
Image src = ImageIO.read(file);
BufferedImage image = new BufferedImage(src.getWidth(null), src.getHeight(null),
BufferedImage.TYPE_INT_RGB);
image.getGraphics().drawImage(src, 0, 0, src.getWidth(null),
src.getHeight(null), null);
ChartUtilities.writeBufferedImageAsJPEG(response.getOutputStream(), image);
}catch(Exception e) {
e.printStackTrace();
return;
}
}
}
在jsp页面中加入以下代码
- <image src="imageShow?path=****.jpg"/>
在web.xml里面配置如下
- <servlet>
- <servlet-name>imageShowServletservlet-name>
- <servlet-class>com.hyjx.framework.pub.attachment.ImageShowServletservlet-class>
- servlet>
- <servlet-mapping>
- <servlet-name>imageShowServletservlet-name>
- <url-pattern>/imageShowurl-pattern>
- servlet-mapping>
本文介绍了一个用于展示服务器上图片的Servlet实现方式。该Servlet通过获取请求参数中的图片路径,读取对应文件,并将其以JPEG格式返回给客户端。此外,还展示了如何在web.xml中配置Servlet映射。
402

被折叠的 条评论
为什么被折叠?



