Enlightensoft's Blog

Helping in your each step

  • Categories

  • Authors

Servlet

Posted by Pankil Patel on March 26, 2010

package cc.co.enlightensoft.sample.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DownloadFileServlet extends HttpServlet {

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 {
doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//File f = new File(“D:\\Personal Data\\Resumes\\Ajax-Basics.pdf”);
//String downloadFileName = request.getParameter(“file”);
//String filePath = HrmsCommonProperties.getPropertyValue(Constants.RESUME_LOCATION) + downloadFileName;
String filePath = request.getParameter(“file”);
//System.out.println(filePath);
File f = new File(filePath);
String fileName = f.getName().substring(
f.getName().lastIndexOf(“/”) + 1, f.getName().length());
if (f.exists() && f.canRead()) {
response.setHeader(“Content-Disposition”, “attachment; filename=”
+ fileName);
response.encodeRedirectURL(“Resume”);
String fileExtension = fileName
.substring(fileName.lastIndexOf(“.”) + 1);

response.setContentType(getMimeType(fileExtension));

try {
InputStream in = new FileInputStream(f);
BufferedInputStream stream = new BufferedInputStream(in);
ServletOutputStream outs = response.getOutputStream();

final int BUFFER_SIZE = 1024;

byte[] buffer = new byte[BUFFER_SIZE];
int readCount = -1;
while ((readCount = stream.read(buffer)) > 0) {
outs.write(buffer, 0, readCount);
}
outs.flush();
outs.close();
stream.close();
in.close();
} catch (Exception e) {
response.setContentType(“text/html”);
PrintWriter out = response.getWriter();
out.println(“<script type=\”text/javascript\”>”);
out.println(“alert(\”Could not read requested file\”);”);
out.println(“this.window.close();”);
out.println(“</script>”);
}
} else {
response.setContentType(“text/html”);
PrintWriter out = response.getWriter();
out.println(“<script type=\”text/javascript\”>”);
out.println(“alert(\”Requested file is not on Hard Disk\”);”);
out.println(“this.window.close();”);
out.println(“</script>”);
}
}

private String getMimeType(String fileExtension) {
if (fileExtension.equalsIgnoreCase(“doc”)) {
return “application/msword”;
}
if (fileExtension.equalsIgnoreCase(“txt”)) {
return “html/text”;
}
if (fileExtension.equalsIgnoreCase(“pdf”)) {
return “application/pdf”;
}
if (fileExtension.equalsIgnoreCase(“oda”)) {
return “application/oda”;
}
if (fileExtension.equalsIgnoreCase(“rtf”)) {
return “application/rtf”;
}
return “application/octet-stream”;
}

public void init() throws ServletException {
// Put your code here
}

}

Advertisements

2 Responses to “Servlet”

  1. https://enlightensoft.wordpress.com/?attachment_id=77

  2. Good article. thank you

    If you want download avatars for you, click link below

    http://avatarsforyou.net

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: