/*
*Servlet实现将图片写入到网页和实现图片下载的功能
*
*/
package lipf.java.servlet;
import java.io.File;
public class ShowImage extends HttpServlet {
public ShowImage() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
downlodeImage(request, response);
}
public void showImage(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("image/jpeg");
String path = this.getServletContext().getRealPath("/");
File file = new File(path + "/images/123.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/123.JPG");
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);
}
}