从数据库读取blob字段数据整理:
在servlet里面首先的是要判断这个文件的类型,以便得到是什么类型的流
便可以这样进行操作:servletOutStream sos = response.getOutStream();
然后便是进行jdbk数据库的链接,首先是:
conn = DataSourceFactory.create().getConnection();建立链接
待链接建立成功后就可以创建一个对象(这种称为可能是错误的)
stmt = conn.createStatement();
这个对象里面包含着执行sql的方法,所以然后就可以这样执行sql语句了,得到自己的有效数据
rs = stmt.executeQuery(sqlstr);
这些都应该是基本的基础工作:
然后就顺次的取出获得的rs,并得到其中的blob,字段
blob blob = (blob)rs.getBlob("fanganfile");
上面得到的是blob的数据,所以需要转化为二进制字节流:
InputStream pi = blob.getBinaryStream();
然后需要把得到的二进制字节流输出到byte[] blobbytes = new byte[blobsize];以便输出时使用
写到sos response里面
sos.write(blobtytes,0,bytesRead)
大体上是这样的,得到blob然后转化饿日二进制字节流,然后把它放入到请求的response里面。以备后面就行使用。
当然最后在所有的工作都结束以后,需要顺次关闭刚才开辟的一些通道
pi.close() sos.close() conn.close();
package com.sdjz.framework;
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
public class GetFile extends HttpServlet implements java.io.Serializable {
private static final String CONTENT_TYPE = "image/jpeg;charset=GB2312";
private static final String CHARACTER_TYPE = "GBK";
private HttpSession session;
private ServletConfig config;
/**
* 处理请求
*/
private void doExecute(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
System.out.println("HttpServlet");
//response.sendRedirect("/Item/content/GetFile.jsp");
Connection conn = null;
ResultSet rs = null;
Statement stmt = null;
String annalid = request.getParameter("FILEANNALID");
String filename = request.getParameter("FILENAME");
//String tablename=request.getParameter("TABLENAME");
String PicType = null;
if(filename.indexOf(".") != -1){
PicType = filename.substring(filename.indexOf(".")+1,filename.length());
System.out.println("++++++++PicType::"+PicType);
}
if (PicType != null && PicType != "") {
response.reset();
if(PicType.toUpperCase().equals("JPEG") || PicType.toUpperCase().equals("JPG"))
response.setContentType("image/jpeg");
else if(PicType.toUpperCase().equals("DOC"))
response.setContentType("application/msword;charset=GBK");
else if(PicType.toUpperCase().equals("XLS"))
response.setContentType("application/vnd.ms-excel;charset=GBK");
else if(PicType.toUpperCase().equals("TXT"))
response.setContentType("text/plain");
else if(PicType.toUpperCase().equals("RAR"))
response.setContentType("application/x-rar-compressed");
else if(PicType.toUpperCase().equals("ZIP"))
response.setContentType("application/zip");
ServletOutputStream sos = response.getOutputStream();
System.out.println("+++++++sos::"+sos);
try {
conn = DataSourceFactory.create().getConnection();
if (conn == null) {
System.err.println("对不起,无法获取数据库连接。");
} else {
stmt = conn.createStatement();
String sqlstr="";
//if(tablename!=null&&!tablename.equals("") && !tablename.toUpperCase().equals("TAB_NEA_UPLOADFILE")){
sqlstr="select fanganfile from jy_fangan where annalid="+ annalid;
//}else{
// sqlstr="select filecontent from TAB_NEA_UPLOADFILE where annalid="+ annalid;
//}
rs = stmt.executeQuery(sqlstr);
System.out.println("++++++++rs::"+rs);
if (rs.next()) {
BLOB blob = (BLOB) rs.getBlob("FANGANFILE");
System.out.println("++++++++blob::"+blob);
if (blob != null) {
InputStream pi = blob.getBinaryStream();
System.out.println("++++++pi::"+pi);
int blobsize = (int) blob.length();
System.out.println("+++++blobsize::"+blobsize);
byte[] blobbytes = new byte[blobsize];
int bytesRead = 0;
while ((bytesRead = pi.read(blobbytes)) != -1) {
sos.write(blobbytes, 0, bytesRead);
}
pi.close();
sos.flush();
}
}
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
} /**
* 释放资源
*/
public void destroy() {
}
/**
* Process the HTTP Get request
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doExecute(request, response);
}
/**
* Process the HTTP Post request
*/
public void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doExecute(request, response);
} /**
* 获取ServletConfig对象
*/
final public ServletConfig getServletConfig() {
return config;
} /**
* This method is run on
* @param request The incoming request information
* @param response The outgoing response information
*/
public String performServices(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String nextPage = null;
try {
String command = request.getParameter("command");
//在此添加执行代码
CommandFactory cc = CommandFactory.create();
BaseCommand bc = cc.getCommand(command);
bc.setRequest(request);
bc.setResponse(response);
bc.setConfig(this.getServletConfig());
if (!bc.execute())
session.setAttribute("Message", bc.getMessage());
} catch (Exception e) {
e.printStackTrace();
nextPage = getInitParameter("error_page");
}
return nextPage;
}
/**
* Initialize global variables
*/
final public void init(ServletConfig config) throws ServletException {
this.config = config;
}
/**
* 释放当前session
*/
public void invalidateSession(HttpSession session) {
try {
session.invalidate();
System.out.println("Session已成功释放!");
} catch (Exception e) {
e.printStackTrace();
}
}
}