博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Servlet实现图片读取显示
阅读量:4699 次
发布时间:2019-06-09

本文共 3409 字,大约阅读时间需要 11 分钟。

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

2.index.jsp:

1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6  7  8  9     10         11 12         文件上传13         
14
15
16
17
18
21 22 23 24
25 26

3.showPic.jsp

1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6  7  8  9     10         11 12         文件上传13         
14
15
16
17
18
21 22 23 24
25
图片26
27 28

4.ShowPictureServlet.java

pacgake com.pearl.util;  1 import java.io.File; 2 import java.io.FileInputStream; 3 import java.io.IOException; 4 import java.io.OutputStream; 5  6 import javax.servlet.ServletConfig; 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest;10 import javax.servlet.http.HttpServletResponse;11 12 public class ShowPictureServlet extends HttpServlet {13 14     public void destroy() {15         super.destroy(); 16     }17 18     public void doGet(HttpServletRequest request, HttpServletResponse response)19             throws ServletException, IOException {20         //文件路径21         String picFolder = "E:/upload/";22         String fileName = request.getParameter("fileName");23         if(fileName!=null && !fileName.equals("")){24             String mimeType = "image/gif";25             //设置content类型26             response.setContentType(mimeType);27             //设置大小28             File file = new File(picFolder + fileName);29             response.setContentLength((int) file.length());30             //打开文件并输出31             FileInputStream inputStream = new FileInputStream(file);32             OutputStream out = response.getOutputStream();33             34             //把文件复制到输出流35             byte[] data = new byte[1024];36             int count = 0;37             while ((count=inputStream.read(data))>=0){38                 out.write(data, 0, count);39             }40             inputStream.close();41             out.close();42         }43     }44 45     public void doPost(HttpServletRequest request, HttpServletResponse response)46             throws ServletException, IOException {47         doGet(request, response);48     }49 50     51     public void init(ServletConfig config) throws ServletException {52         super.init(config);53     }54 55 }

 5.web.xml

1 
2
7
8
This is the description of my J2EE component
9
This is the display name of my J2EE component
10
ShowPictureServlet
11
com.pearl.util.ShowPictureServlet
12
13 14
15
ShowPictureServlet
16
/ShowPictureServlet
17
18 19
20
index.jsp
21
22

6.完成。

 

转载于:https://www.cnblogs.com/yeqrblog/p/4894323.html

你可能感兴趣的文章
jar包上传到jcenter
查看>>
《黑白团团》第九次团队作业:Beta冲刺与验收准备
查看>>
团队站立会议04
查看>>
PHP计划任务:如何使用Linux的Crontab执行PHP脚本(转载)
查看>>
Working with Data Sources 2
查看>>
设计模式12——中介者模式
查看>>
小马过河
查看>>
npm和gulp学习
查看>>
关注细节但不陷入细节
查看>>
【Python】django模型models的外键关联使用
查看>>
httperf ---linux web站点压力测试
查看>>
JAVA基础知识(五)数据类型转换
查看>>
hdu-5583 Kingdom of Black and White(数学,贪心,暴力)
查看>>
算法与设计模式
查看>>
(4)理解 neutron ml2---port创建流程代码解析
查看>>
python List
查看>>
Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'userinfo.
查看>>
免费资源:Polaris UI套件 + Linecons图标集(AI, PDF, PNG, PSD, SVG)
查看>>
《Javascript高级程序设计》读书笔记(1-3章)
查看>>
ubuntu内核版本管理
查看>>