在项目中,有时会将文本文件、图片以2进制的方式存入数据库,对应字段的类型为blob,一般在jsp页面操作字符串来处理图片、文本文件,如何将blob类型转换成String类型就成了关键。
public ActionForward getBulletin(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception { buildAllBulletinType(request); String id = request.getParameter("id"); String titleSub = ""; String title=""; String state=""; String bulletinDate = ""; List ll = BulletinServiceImpl.getInstance().selForId(id); if(ll!=null && ll.size()>0) { Object [] obj = (Object [])ll.get(0); if(obj[3]!=null) { Blob bo = (Blob)obj[3]; InputStream insm = bo.getBinaryStream(); byte [] fileContent = new byte [(int)bo.length()]; insm.read(fileContent); String filetxt = new String (fileContent); request.setAttribute("filetxt", filetxt); return mapping.findForward("equityfile"); } }