您现在的位置:首页 >> 基础 >> Java网络编程 >> 内容

Java存储和读取数据库中二进制图片

时间:2014-12-21 16:36:25

  核心提示:Java存储和读取数据库中二进制图片这个页面是JSP上传页面 源码:%@ page language='java' contentType='text/html; charset=GB18030'pa...

Java存储和读取数据库中二进制图片
这个页面是JSP上传页面 源码:

<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ page import="cc.javaweb.*"%>
<%@ page import="java.sql.*"%>
<%
String path = request.getScheme() + "://" + request.getServerName()
+ ":" + request.getServerPort() + request.getContextPath()
+ "/";
String url = request.getScheme() + "://";
url += request.getHeader("host");
url += request.getRequestURI();
if (request.getQueryString() != null)
url += "?" + request.getQueryString();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>图片上传显示实例-Java教程</title>
</head>
<body>
<form id="form1" name="thisform" method="post"
action="<%=path%>Gys_tgsp_Servlet?lx=2">
<TABLE borderColor=#eeeeee cellSpacing=0 cellPadding=0 width=710
border=1>
<TBODY>
<TR>
<TD class=memInfo_l width="150"></TD>
<TD></TD>
</TR>
<TR>
<TD class=memInfo_l style="font-size:12px">
产品图片:
</TD>
<TD class=memInfo_r width=506>
<DIV align="left" style="font-size:12px">
<input type=file name="upimg" style=width:100>
<SPAN class=font_2 id=ShopCarCount>*</SPAN> </DIV>

</TD>
</TR>
<TR>
<TD class=memInfo_l style="font-size:12px">
产品名称:
</TD>
<TD class=memInfo_r width=506>
<DIV align="left" style="font-size:12px">
<input type="text" name="cpmc" />
<SPAN class=font_2 id=ShopCarCount>*</SPAN>
</DIV>
</TD>
</TR>

</TBODY>
</TABLE>
<DIV id=submitInfo align="center">
<br />
<input type="submit" name="button" value="提交商品报价">

</DIV>
</form>
<TABLE borderColor=#eeeeee cellSpacing=0 cellPadding=0 width=710
border=1>
<TBODY>
<TR>
<TD class=memInfo_l width="150"></TD>
<TD></TD>
</TR>
<TR>
<TD class=memInfo_l style="font-size:12px">
图片名称
</TD>
<TD class=memInfo_r width=506>
<DIV align="left" style="font-size:12px">
图片
</DIV>
</TD>
</TR>
<%
try {

//从数据库读取图片 并显示到页面上
DataBaseClass dbc = new DataBaseClass();
ResultSet rs = dbc.getQuery("select * from spbj");
while (rs.next()) {
%>
<TR>
<TD class=memInfo_l style="font-size:12px">
<%=rs.getString("cpmc")%>
</TD>
<TD class=memInfo_r width=506>
<DIV align="left" style="font-size:12px">
<img src="caozuo/show2.jsp?photo=<%=rs.getInt("id")%>"
width="120">
</a>
</DIV>
</TD>
</TR>
<%
}
rs.close();
} catch (Exception e) {
System.out.println("test.jsp出现的异常是:" + e);
}
%>
</TBODY>
</TABLE>
</body>
</html>

这个是servlet代码 Gys_tgsp_Servlet.java

package com.wer.user;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wer.dao.DBPool;
import com.wer.dao.DataBaseClass;

@SuppressWarnings("serial")
public class Gys_tgsp_Servlet extends HttpServlet {


public Gys_tgsp_Servlet() {
super();
}

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 {
InputStream is = null;
FileInputStream fis = null;
FileOutputStream fos = null;
Connection conn = null;
try{
String lx=request.getParameter("lx");
if(lx.equals("1")){

//这就不发了http://www.javaweb.cc
}else if(lx.equals("2")){
String cpmc=request.getParameter("cpmc"); 
byte aa[] = cpmc.getBytes("ISO-8859-1");
cpmc = new String(aa);
String upimg=request.getParameter("upimg");

byte bb[] = upimg.getBytes("ISO-8859-1");
upimg = new String(bb);

File file = new File(upimg);
fis = new FileInputStream(file);
byte[] buf=new byte[(int)fis.available()];
fis.read(buf);//图片数据 
conn = DBPool.getConnection();
PreparedStatement ps = conn.prepareStatement("insert into spbj(cpimg,cpmc)values(?,?)"); 
ps.setBytes(1, buf);//插入图片
ps.setString(2,cpmc);
ps.executeUpdate();
response.sendRedirect("test.jsp"); 
}
}catch(Exception e){
System.out.println("Gys_tgsp_servlet.java异常是:"+e);
}
}

public void init() throws ServletException {
// Put your code here
}

}

Servlet记得在web.xml中配置一下!这个代码就不发了。

下面是从数据库中读取数据库中二进制图片代码 show2.jsp

<%@ page contentType="text/html; charset=gb2312"%>
<%@ page import="java.io.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="com.wer.dao.*"%>
<%
String photo = "";
try {
photo = request.getParameter("photo");
DataBaseClass dbc = new DataBaseClass();
String sql = "select top 1 * from spbj where id=" + photo;
ResultSet rs = dbc.getQuery(sql);
if (rs.next()) {
Blob b = rs.getBlob("cpimg");
long size = b.length();
byte[] bs = b.getBytes(1, (int) size);
response.setContentType("image/jpeg");
OutputStream outs = response.getOutputStream();
outs.write(bs);
outs.flush();
out.clear();
out = pageContext.pushBody();
rs.close();
} else {
rs.close();
response.sendRedirect("imgs/nopic.gif");
}
} catch (Exception e) {
System.out.println("show.jsp的异常是:" + e);
}
%>
所有代码都已经发上来,今天没事做了一个例子,希望对有需要的朋友有用,记得配置一下web.xml

差点忘记了,连接数据库那块代码没发上来,这个很简单就不发了!

Java免费学习  Java自学网   http://www.javalearns.com

作者:不详 来源:网络
    你是从哪里知道本网站的?
  • 网友介绍的
  • 百度搜索的
  • Google搜索的
  • 其它搜索过来的
  • 网址输错了进来的
  • 太忙了不记得了
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
  • java学习网(www.javalearns.com) © 2014 版权所有 All Rights Reserved.
  • Email:javalearns@163.com 站长QQ:1356121699 晋ICP备14003680号-3
  • java学习网部分内容来自网络或网友发布,如侵犯了您利益,请发邮件至:javalearns@126.com,我们尽快处理!
  • Java学习网
  • 网站统计
  • 晋公网安备 14042902000001号