问一个Struts中图片显示的问题,解决马上给分 还有,前提是图片要包含在JSP页面内显示 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我是把图片存到数据库中,我用的是servlet把代码帖出来,看看对你有没有帮助;javabean:mport java.io.*;public class dbo { public dbo() { } public InputStream getBytes() { Connection con = null; PreparedStatement pst = null; ResultSet rs = null; InputStream stream = null; File f = null; byte context[] = new byte[100000]; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con = DriverManager.getConnection("jdbc:odbc:test"); f = new File("C://aa.jpg"); stream = new FileInputStream(f); //pst = con.prepareStatement("INSERT INTO MyTable (COL10) VALUES(?)"); //pst.setBinaryStream(1,stream,(int)f.length()); pst = con.prepareStatement("SELECT COL10 FROM MyTable"); rs = pst.executeQuery(); if (rs.next()) { stream = rs.getBinaryStream(1); } return stream; } catch (Exception e) { System.out.println(e); return stream; } finally { try { /* if (rs != null) { rs.close(); } if (pst != null) { pst.close(); } if (con != null) { pst.close(); }*/ } catch (Exception e) { } } } public static void main(String args[]) { }}/* 写入数据库 Connection con = null; PreparedStatement pst = null; ResultSet rs = null; InputStream stream = null; File f=null; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con = DriverManager.getConnection("jdbc:odbc:test"); f = new File("C://aa.jpg"); stream = new FileInputStream(f); pst = con.prepareStatement("INSERT INTO MyTable (COL10) VALUES(?)"); pst.setBinaryStream(1,stream,(int)f.length()); pst.execute(); } catch (Exception e) { System.out.println(e); } finally { try { if (rs != null) { rs.close(); } if (pst != null) { pst.close(); } if (con != null) { pst.close(); } } catch (Exception e) { } } */ jsp and servletjsp:<%@ page contentType="text/html; charset=GBK" %><html><head><title>jsp1</title></head><body bgcolor="#ffffff"> dfdfd<img src="servlet1?id=1"/>dfdef</body></html>servlet:package index;import javax.servlet.*;import javax.servlet.http.*;import java.io.*;//import java.util.*;import index.dbo;public class Servlet1 extends HttpServlet { private static final String CONTENT_TYPE = "text/html; charset=GBK"; //Initialize global variables public void init() throws ServletException { } //Process the HTTP Get request public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //response.setContentType("image/gif;"); dbo d = new dbo(); java.io.InputStream pic = null; java.io.OutputStream pw = null; try{ //request.getParameter("id"); response.setContentType("image/gif;name=doskt.jpg"); pic=d.getBytes(); pw=response.getOutputStream(); byte[] b = new byte[4096]; int bt = pic.read(b); while (bt != -1) { pw.write(b, 0, bt); bt = pic.read(b); } } catch(Exception e){ System.out.println("doGet:" + e); } finally{ if(pic!=null){ pic.close(); } } /*out.println("<html>"); out.println("<head><title>Servlet1</title></head>"); out.println("<body bgcolor=\"#ffffff\">"); out.println("<p>The servlet has received a GET. This is the reply.</p>"); out.println("</body></html>");*/ } //Clean up resources public void destroy() { }} spring 事务问题 java bean问题,急 java.lang.NoClassDefFoundError: com/sun/activation/registries/LogSupport 求解 页面问题 求助@@@动态表单 我找到两本好书,大家快来下啊 servlet + javabean 如何做高效的数据库连接? 一个B/S中关于时间的问题 J2EE部署问题,请高手指点 java中可以定义以类为类型的数组吗? 连接RMI远程对象的奇怪问题
把代码帖出来,看看对你有没有帮助;
javabean:
mport java.io.*;public class dbo {
public dbo() {
} public InputStream getBytes() { Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
InputStream stream = null;
File f = null;
byte context[] = new byte[100000];
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:test");
f = new File("C://aa.jpg");
stream = new FileInputStream(f);
//pst = con.prepareStatement("INSERT INTO MyTable (COL10) VALUES(?)");
//pst.setBinaryStream(1,stream,(int)f.length());
pst = con.prepareStatement("SELECT COL10 FROM MyTable");
rs = pst.executeQuery();
if (rs.next()) {
stream = rs.getBinaryStream(1);
}
return stream;
}
catch (Exception e) {
System.out.println(e);
return stream;
}
finally {
try {
/*
if (rs != null) {
rs.close();
}
if (pst != null) {
pst.close();
}
if (con != null) {
pst.close();
}
*/
}
catch (Exception e) { }
} } public static void main(String args[]) { }
}/*
写入数据库
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
InputStream stream = null;
File f=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:test");
f = new File("C://aa.jpg");
stream = new FileInputStream(f);
pst = con.prepareStatement("INSERT INTO MyTable (COL10) VALUES(?)");
pst.setBinaryStream(1,stream,(int)f.length());
pst.execute();
}
catch (Exception e) {
System.out.println(e);
}
finally {
try {
if (rs != null) {
rs.close();
}
if (pst != null) {
pst.close();
}
if (con != null) {
pst.close();
}
}
catch (Exception e) { }
}
*/
jsp:
<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
jsp1
</title>
</head>
<body bgcolor="#ffffff">
dfdfd
<img src="servlet1?id=1"/>
dfdef
</body>
</html>servlet:package index;import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
//import java.util.*;
import index.dbo;public class Servlet1
extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK"; //Initialize global variables
public void init() throws ServletException {
} //Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
//response.setContentType("image/gif;");
dbo d = new dbo();
java.io.InputStream pic = null;
java.io.OutputStream pw = null;
try{
//request.getParameter("id");
response.setContentType("image/gif;name=doskt.jpg");
pic=d.getBytes();
pw=response.getOutputStream();
byte[] b = new byte[4096];
int bt = pic.read(b);
while (bt != -1) {
pw.write(b, 0, bt);
bt = pic.read(b);
}
}
catch(Exception e){
System.out.println("doGet:" + e);
}
finally{
if(pic!=null){
pic.close();
}
} /*out.println("<html>");
out.println("<head><title>Servlet1</title></head>");
out.println("<body bgcolor=\"#ffffff\">");
out.println("<p>The servlet has received a GET. This is the reply.</p>");
out.println("</body></html>");*/
} //Clean up resources
public void destroy() {
}
}