package name.aspx;import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;public class ImageServlet extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
{
try
{
String id = (String)req.getParameter("id");
Class.forName("org.gjt.mm.mysql.Driver");
//String url="jdbc:mysql://localhost/jspdev?user=root&password=";
String url="jdbc:mysql://localhost/xiaohuozhisql?user=xiaohuozhisql&password=6737038";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from image where id = " + id);
if(rs.next())
{
InputStream in = rs.getBinaryStream("image");
OutputStream out = res.getOutputStream();
byte[] bytes = new byte[1024];
while(in.read(bytes,0,1024) != -1)
{
out.write(bytes);
}
}
conn.close();
}
catch(Exception e)
{
System.out.println(e);
}
} public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
{
}
}
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;public class ImageServlet extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
{
try
{
String id = (String)req.getParameter("id");
Class.forName("org.gjt.mm.mysql.Driver");
//String url="jdbc:mysql://localhost/jspdev?user=root&password=";
String url="jdbc:mysql://localhost/xiaohuozhisql?user=xiaohuozhisql&password=6737038";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from image where id = " + id);
if(rs.next())
{
InputStream in = rs.getBinaryStream("image");
OutputStream out = res.getOutputStream();
byte[] bytes = new byte[1024];
while(in.read(bytes,0,1024) != -1)
{
out.write(bytes);
}
}
conn.close();
}
catch(Exception e)
{
System.out.println(e);
}
} public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
{
}
}
解决方案 »
- 如何将一个表生成JSON树型结构?
- 关于Struts2.0的问题
- JSP里面怎么实现执行一个sql语句并且返回一个int类型的变量?(我的Sql语句都写在存储过程里面的)
- 高分求解决tomcat异常
- 问一个distinct的问题,不知道有没有解决的办法?
- 请问j2ee是什么?
- 求使用JDBC连接SQL SERVER 2000的简单java程序,并能够详细说明每一步的作用。谢谢!
- 如何配置jbuilder+websphere可以开发ejb
- 关于activeMQ的问题,求大神指点!!!
- 四方支付
- struts:form-bean无法获取页面表单数据
- 再问:有谁会在axis中实现soap消息的加密和解密来实现安全的web service
上面那个是我用mysql实现的。完全可以用!你把他换个驱动就行了
调用的话就调用servelt映谢名加?id=*就可以了!
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import oracle.sql.BLOB;
import sun.jdbc.rowset.CachedRowSet;
import util.db.OpenDbBean;
import java.sql.*;public class GetPhoto extends HttpServlet {
//Initialize global variables
public void init() throws ServletException {
} //Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { String bmh = request.getParameter("bmh");
String sort = request.getParameter("sort");
if (bmh == null) {
bmh = "";
}
if (sort == null) {
sort = "master";
} Connection conn = null;
Statement ps = null;
ResultSet rs = null;
OpenDbBean db = new OpenDbBean();
response.setContentType("image/jpeg");
try {
ServletOutputStream out = response.getOutputStream();
String sql = null;
if (sort.equals("master")) {
sql = "select * from zs_s_photo where bmh='" + bmh + "'";
}
else if (sort.equals("doctor")) {
sql = "select * from zs_b_photo where bmh='" + bmh + "'";
}
System.out.println(sql);
conn = db.getConnection();
ps = conn.createStatement();
rs = ps.executeQuery(sql);
if (rs.next()) {
BLOB blob = (BLOB) rs.getBlob("binaryfile");
int blobsize = (int) blob.length();
InputStream pi = blob.getBinaryStream();
byte[] blobbytes = new byte[blobsize];
int bytesRead = 0;
while ((bytesRead = pi.read(blobbytes)) != -1) {
out.write(blobbytes, 0, bytesRead);
}
pi.close();
out.flush();
}
else {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
}
catch (Exception e) {
System.out.println(e.getMessage());
}
finally {
try {
db.CleanConnection(conn, ps, rs);
}
catch (SQLException e1) {
System.out.println(e1.getMessage());
}
}
}}
1.在BusinessBean中读出数据库中的路径
ArrayList arrImgPath = new ArrayList();
while(rs.next()){
arrImgPath.add(rs.getString("Img_Path"));
}
request.setAttribute("imgpath",arrImgPath);
2.在jsp中调用BusinessBean showImg.显示
<%@ taglib uri="struts-logic" prefix="logic" %>
<logic:iterate id="element1" name="element" scope="request">
<html:img page="/showImg.do" paramId="img" paramName="element1" border="1" width="108" height="26" scope="request"/>
</logic:iterate>
3.BusinessBean showImg.java:
String imgPath=request.getParameter("img");
file = new File(imgPath);
if (!file.isDirectory()&& file.exists()){
length = (int)file.length();
}
res.setContentType("image/jpg");//假设是jpg图
res.setHeader("Content-Disposition","inline");
res.setContentLength(length);
if (length > 0){
int bufferSize = 32768;
InputStream in = new FileInputStream(fullpath);
ServletOutputStream outfile = res.getOutputStream();
int chunkLen = bufferSize;
if(chunkLen > length && length > 0)
chunkLen = length;
byte ioBuf[] = new byte[chunkLen];
do{
chunkLen = in.read(ioBuf);
if(chunkLen <= 0)
break;
outfile.write(ioBuf, 0, chunkLen);
length -= chunkLen;
} while(length != 0);
in.close();
outfile.flush();
outfile.close();
}
将该流对象通过servlet,显示到页面就可以了
String ss="image/"+imageType;
InputStream in=null;
response.setContentType(ss);
ServletOutputStream out = response.getOutputStream();
Blob myBlob=r.getBlob("bin");
if(myBlob!=null){
in=myBlob.getBinaryStream();
byte[] b = new byte[1024];
int len = 0;
while((len = in.read(b)) != -1){
out.write(b,0,len);
out.flush();
}
out.close();
}else{
FileInputStream fin=new FileInputStream(this.getServletContext().getRealPath("/bdl_html/")+"/no.jpg");
byte[] b = new byte[1024];
int len = 0;
while((len = fin.read(b)) != -1){
out.write(b,0,len);
out.flush();
}
out.close();
看你的方案SERVLET + JSP + JAVABEAN
或 JAVABEAN+自定义标签+STRUTS