各位大虾,小弟现在有个问题:
我现在想做一个JSP页面(负责将文件选定并提交,就是一般的文件提交页面),接受提交的JSP页面调用JAVABEAN(将图片存进ORACLE BLOB字段)。然后就是在JSP页面里读取存在BLOB字段里的图片。要是有源码那就非常感谢了。
我现在想做一个JSP页面(负责将文件选定并提交,就是一般的文件提交页面),接受提交的JSP页面调用JAVABEAN(将图片存进ORACLE BLOB字段)。然后就是在JSP页面里读取存在BLOB字段里的图片。要是有源码那就非常感谢了。
解决方案 »
- 做个论坛应该需要哪几张表
- 急!jstree,求当前节点高度
- 正则表达式解惑?
- JSP中在两三个表关联时,如何向其中插入数据、删除数据,更新数据、查询数据。最好
- 在jBuider 中怎么配置hibernate,谢谢
- jsp连接sqlserver,写程序好用,放类里不好用?
- <bean : message key="welcome"/>出现乱码!!
- 大哥大姐请进`````进则有分``谢谢```----------JSP输出表格问题```
- 请问如何在自己的tag中使用其他标签库?
- linux中用jsp连接mysql!
- 单击某个链接,弹出一个新窗口,请问如何传参数给新窗口?
- 求助高手指点:intellij+tomcat的问题
boolean flag = false;
if (entity == null)
return false;
try {
StringBuffer sql = new StringBuffer(
"insert into PRODUCT_TAB(PRODUCT_ID,PRODUCT_NAME,PRODUCT_TYPE,PARENTID,REMARK,IMAGE)");
sql.append(" values(?,?,?,?,?,empty_blob())");
PreparedStatement pst = con.prepareStatement(sql.toString());
String id = generatorID();
pst.setString(1, id);
pst.setString(2, entity.getProductName() == null ? id : entity
.getProductName());
pst.setString(3, entity.getProductType());
pst.setString(4, entity.getParentId());
pst.setString(5, entity.getRe());
pst.executeUpdate();
pst.close();
if (entity.getImageFile() != null) {
/***************************************************************
* 插入blob二进制数据
*/
BLOB imgBlob = null;
pst = con
.prepareStatement("select image from PRODUCT_TAB where PRODUCT_ID=? for update");
pst.setString(1, id);
ResultSet rs = pst.executeQuery();
if (rs.next()) {
imgBlob = (BLOB) rs.getBlob(1);
}
InputStream isr = entity.getImageFile();
pst = con
.prepareStatement("update PRODUCT_TAB set image=? where PRODUCT_ID=?"); OutputStream out = imgBlob.getBinaryOutputStream();
int count = -1, total = 0;
byte[] data = new byte[imgBlob.getBufferSize()]; // 另一种实现方法,节省内存
while ((count = isr.read(data)) != -1) {
total += count;
out.write(data, 0, count);
}
isr.close();
out.close();
pst.setBlob(1, imgBlob);
pst.setString(2, id);
pst.executeUpdate();
pst.close();
}
con.commit();
con.close();
flag = true;
entity = null;
} catch (FileNotFoundException e) {
log.info("ProductDAO.createProduct() FileNotFoundException :"
+ e.getMessage());
} catch (IOException e) {
log.info("ProductDAO.createProduct() IOException :"
+ e.getMessage());
} catch (SQLException e) {
log.info("ProductDAO.createProduct() is error :" + e.getMessage());
}
return flag;
}
下面方法从数据库读出二进制流
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.hf.db.dao.ProductDAO;
import com.hf.db.entity.Product;public class ShowImage extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* Constructor of the object.
*/
public ShowImage() {
super();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/jpeg;charset=gb2312"); //设定类型
String productId=(String)request.getParameter("productId");
try{
ProductDAO dao=new ProductDAO();
Product entity=dao.getProduct(productId);
long size = entity.getImage().length();
byte[] bs = entity.getImage().getBytes(1,(int)size);
OutputStream outs = response.getOutputStream();
outs.write(bs);
outs.flush();
outs.close();
}catch(Exception e){
PrintWriter pw=response.getWriter();
response.setContentType("text/html;charset=gb2312");
pw.write("无法打开图片");
pw.close();
}
}}jsp中显示图片<td height="130">
<div align="center">
<img src="<%=context%>/ShowImage?productId=<%=products[0].getProductId()%>" alt="点击查看大图片" width="90" height="100" border="0" align="middle">/div>
</td>