Tuesday, April 15, 2014

Download Attachment File

<%@include file="../common/common.jsp"%>
<%@include file="../common/openConn.jsp"%>
<%@ page language="java" import="java.sql.*,java.io.*,java.util.*"%>
<%
String s1 = java.net.URLDecoder.decode(toHTML(request.getParameter("value1")));
String att_id = toHTML(request.getParameter("att_id"));

//Get file type
String fileType = s1.substring(s1.indexOf(".")+1, s1.length());

response.setHeader("Content-Disposition", "attachment; filename=" + s1);
if(fileType.equals("doc")){
    response.setContentType("application/msword");
}else if(fileType.equals("xls") || fileType.equals("xlsx")){
    response.setContentType("application/vnd.ms-excel");
}else if(fileType.equals("pdf")){
    response.setContentType("application/pdf");
}else if(fileType.equals("ppt") || fileType.equals("pptx")){  
    response.setContentType("application/vnd.ms-powerpoint");
}else if(fileType.equals("gif")){  
    response.setContentType("image/gif");
}else if(fileType.equals("jpeg")){
    response.setContentType("image/jpeg");
}else if(fileType.equals("jpg")){
    response.setContentType("image/jpg");
}else if(fileType.equals("bmp")){
    response.setContentType("image/bmp");
}else if(fileType.equals("zip")){  
    response.setContentType("application/zip");  
}else if(fileType.equals("rtf")){  
    response.setContentType("application/rtf");  
}else if(fileType.equals("txt")){      
    response.setContentType("text/plain");
}else{
    response.setContentType("text/html");
}

System.out.println("s1: " + s1);

String strSQL = "SELECT STREAMS FROM ATTACHMENT WHERE DOC_ID='" + att_id+"'";
ResultSet rs = stmt.executeQuery(strSQL);
//out.println(strSQL + "
");
if (!rs.next()) {
}

//This is refinement version will remove ΓΏ at the end of file
byte[] fileBytes=rs.getBytes(1);
ServletOutputStream outs = response.getOutputStream();
outs.write(fileBytes);
outs.flush();
outs.close();

%>
<%@include file="../common/closeConn.jsp"%>

Since 26 July 2008: