Servlet实现将图片写入到网页和实现图片下载的功能
2018-01-18 15:40
661 查看
package donghongyujava.servlet;
import java.io.File;
public class ShowImage extends HttpServlet {
/**
* Constructor of the object.
*/
public ShowImage() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// showImage(request, response);
downlodeImage(request, response);
}
public void showImage(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// 读取方式
// response.setContentType("application/zip");
response.setContentType("image/jpeg");
// 获取图片绝对路径
String path = this.getServletContext().getRealPath("/");
File file = new File(path + "/images/WIN_20131027_214421.JPG");
// 创建文件输入流
FileInputStream is = new FileInputStream(file);
// 响应输出流
ServletOutputStream out = response.getOutputStream();
// 创建缓冲区
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
is.close();
out.flush();
out.close();
}
public void downlodeImage(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// 获取图片绝对路径
String path = this.getServletContext().getRealPath("/");
File file = new File(path + "/images/WIN_20131027_214421.JPG");
//设置头信息,内容处理的方式,attachment以附件的形式打开,就是进行下载,并设置下载文件的命名
response.setHeader("Content-Disposition","attachment;filename="+file.getName());
// 创建文件输入流
FileInputStream is = new FileInputStream(file);
// 响应输出流
ServletOutputStream out = response.getOutputStream();
// 创建缓冲区
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
is.close();
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
import java.io.File;
public class ShowImage extends HttpServlet {
/**
* Constructor of the object.
*/
public ShowImage() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// showImage(request, response);
downlodeImage(request, response);
}
public void showImage(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// 读取方式
// response.setContentType("application/zip");
response.setContentType("image/jpeg");
// 获取图片绝对路径
String path = this.getServletContext().getRealPath("/");
File file = new File(path + "/images/WIN_20131027_214421.JPG");
// 创建文件输入流
FileInputStream is = new FileInputStream(file);
// 响应输出流
ServletOutputStream out = response.getOutputStream();
// 创建缓冲区
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
is.close();
out.flush();
out.close();
}
public void downlodeImage(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// 获取图片绝对路径
String path = this.getServletContext().getRealPath("/");
File file = new File(path + "/images/WIN_20131027_214421.JPG");
//设置头信息,内容处理的方式,attachment以附件的形式打开,就是进行下载,并设置下载文件的命名
response.setHeader("Content-Disposition","attachment;filename="+file.getName());
// 创建文件输入流
FileInputStream is = new FileInputStream(file);
// 响应输出流
ServletOutputStream out = response.getOutputStream();
// 创建缓冲区
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
is.close();
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
相关文章推荐
- Servlet实现将图片写入到网页和实现图片下载的功能
- dreamweaver实现图片超链接(点击图片按钮实现下载指定apk功能,可以用于在静态网页里做公司宣传页等)
- servlet 简单案例!实现图片下载功能
- Java实现网页截屏功能(图片下载功能)的几种方式(整理)
- a标签实现下载图片功能
- html制作简单框架网页 实现自己的音乐驿站 操作步骤及源文件下载 (播放功能限mp3文件)
- Python实现简单爬虫功能--批量下载百度贴吧里的图片
- 使用Servlet实现注册网页的随机认证图片
- php中使用gd库实现下载网页中所有图片
- php中使用gd库实现下载网页中所有图片
- 实现自动发帖,注册。以及访问网页,执行网页下载等等功能
- 【C++】纯C++实现http打开网页下载内容的功能
- 使用Servlet实现网页验证码功能
- Android编程实现图片的上传和下载功能示例
- 拥有网页版小U盘 ASP.NET实现文件上传与下载功能
- Python爬虫实现抓取京东店铺信息及下载图片功能示例
- 微信小程使用swiper组件实现图片轮播切换显示功能【附源码下载】
- JS中实现网页中禁止下载图片
- 安卓:IntentService实现网络下载图片并写入到SD卡