This servelt should pick up the image as per the URL parameter from local drive and give the option of save or option image on your disk.
1. Make one Servlet SendImage
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SendImage extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
private SendImageHelper sendImageHelper;
public SendImage() {
super();
sendImageHelper = new SendImageHelper();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
sendImageHelper.sendImage(getServletContext(),request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
2. One java file SendImageHelper.java
package com.tgt.image.sendimage.service;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SendImageHelper {
String image_name = "";
List
List numbers = new ArrayList();
ResourceBundle props = null;
String filePath = "";
private static final int BUFSIZE = 100;
private ServletContext servletContext;
public void sendImage(ServletContext servletContext,HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
this.servletContext = servletContext;
String reqUrl = request.getRequestURL().toString();
StringTokenizer tokens = new StringTokenizer(reqUrl,"/");
int noOfTokens = tokens.countTokens();
String tokensString[] = new String[noOfTokens];
int count=0;
while(tokens.hasMoreElements()){
tokensString[count++] = (String)tokens.nextToken();
}
String folderName = tokensString[noOfTokens-2];
image_name = tokensString[noOfTokens-1];
filePath="/"+folderName+"/"+image_name;
fullFilePath = "c:/ImageFolder"+filePath;
//doShowImageOnPage(fullFilePath, request, response);
doDownload(fullFilePath, request, response);
}
private void doShowImageOnPage(String fullFilePath,HttpServletRequest request, HttpServletResponse response) throws IOException {
response.reset();
response.setHeader("Content-Disposition", "inline" );
response.setHeader("Cache-Control","no-cache");
response.setHeader("Expires","0");
response.setContentType("image/tiff");
byte[] image = getImage(fullFilePath);
OutputStream outputStream = response.getOutputStream();
outputStream.write(image);outputStream.close();
}
private void doDownload(String filePath,HttpServletRequest request, HttpServletResponse response) throws IOException {
File fileName = new File(filePath);
int length = 0;
ServletOutputStream outputStream = response.getOutputStream();
//ServletContext context = getServletConfig().getServletContext();
ServletContext context = servletContext;
String mimetype = context.getMimeType(filePath);
response.setContentType( (mimetype != null) ? mimetype : "application/octet-stream" );
response.setContentLength( (int)fileName.length() );
response.setHeader( "Content-Disposition", "attachment; filename=\"" + image_name + "\"" );
byte[] bbuf = new byte[BUFSIZE];
DataInputStream in = new DataInputStream(new FileInputStream(fileName));
while ((in != null) && ((length = in.read(bbuf)) != -1)){
outputStream.write(bbuf,0,length);
}
in.close();
outputStream.flush();
outputStream.close();
}
}
the function called in
ReplyDeletebyte[] image = getImage(fullFilePath);
is not defined anywhere...
Regards
Aishwarya Goyal
aishwarya@maxworthsystems.com