package kou.inspector.servlet;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.risesoft.commons.database.Conn;
import oracle.sql.BLOB;
public class download extends HttpServlet {
/**
* 寇新磊
*
*/
public download() {
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 {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String tablename=request.getParameter("tablename");
String guid=request.getParameter("guid");
String pkname=request.getParameter("pkname");
Connection conn=null;
Statement stem=null;
ResultSet rs=null;
try{
//for update 锁定数据行
String sql="select ACCESSORIES,FILENAME,FILETYPE from "+tablename+" where "+pkname+"='"+guid+"'";
String type="";
String filename="";
conn=Conn.getConnection();
stem=conn.createStatement();
rs=stem.executeQuery(sql);
while(rs.next()){
//读取blob对象
BLOB blob=(oracle.sql.BLOB)rs.getBlob("ACCESSORIES");
filename=rs.getString("FILENAME");
type=rs.getString("FILETYPE");
String contenttype="";
if("doc".equalsIgnoreCase(type)){
contenttype="application/msword; charset=gbk";
}
else if("xls".equalsIgnoreCase(type)){
contenttype="application/vnd.ms-excel";
}
else if("pdf".equalsIgnoreCase(type)){
contenttype="application/pdf";
}
else if("xml".equalsIgnoreCase(type)){
contenttype="text/xml";
}
else if("gif".equalsIgnoreCase(type)){
contenttype="image/gif";
}
else if("gpeg".equalsIgnoreCase(type)){
contenttype="image/jpeg";
}
else if("zip".equalsIgnoreCase(type)){
contenttype="application/zip";
}
else if("rat".equalsIgnoreCase(type)){
contenttype="application/octet-stream";
}
else if("rat".equalsIgnoreCase(type)){
contenttype="text/plain";
}
else{
contenttype="application/s2";
}
filename=new String(filename.getBytes("GBK"),"iso-8859-1");
response.setContentType(contenttype);
response.setHeader("Content-Disposition", "attachment;filename="+filename);
InputStream input=blob.getBinaryStream();
OutputStream fo=response.getOutputStream();
byte[] b=new byte[1024];
int len=0;
while((len=input.read(b))!=-1){
fo.write(b, 0, len);
}
fo.close();
input.close();
rs.close();
stem.close();
conn.commit();
}
}
catch(Exception e){
}
finally{try{conn.close();}catch(Exception e){}}
}
public void init() throws ServletException {
// Put your code here
}
}