大体的思路是把图片以二进制流的方式存到blob字段中。这一步调试成功。我现在不知道如何把已存的图片显示在jsp页面上。现在用一种我认为比较笨的办法可以实现。就是从blob字段中读出这个图片的信息,生成一个新的图片,保存在服务器的相关目录下面,然后用页面的<img src=''>从该目录下面找到该图片显示出来。
但是这样要专门设一个目录存放图片,占用磁盘空间;图片多了以后还有重名的问题;所有图片放在一起也不容易管理不好分类。感觉很麻烦。
我的想法看能不能直接从数据库中读出图片信息然后直接类似于out.println(picture);这样显示在页面上,而不保存到硬盘的实际目录下。
请各位出出主意看如何实现?用什么办法?如果可以请给出范例代码。分数可以再加,欢迎指教,多谢。
6 个解决方案
#1
package newhua;
import java.io.*;
import java.util.*;
import java.sql.*;
import java.text.*;
import oracle.sql.*;
import oracle.jdbc.driver.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
public class writeBean {
private String sample = "";
//Access sample property
public String getSample() {
return sample;
}
//Access sample property
public void setSample(String newValue) {
if (newValue!=null) {
sample = newValue;
}
}
private String filename = "";
//Access sample property
public String getFilename() {
return filename;
}
//Access sample property
public void setFilename(String newValue) {
if (newValue!=null) {
filename = newValue;
}
}
public void gwrite() throws SQLException,IOException
{
PreparedStatement stmt = null;
ResultSet rs = null;
InputStream fin = null;
OutputStream fout = null;
Connection conn=null;
try{
newhua.dbbean db=new newhua.dbbean();
db.InitDatabase();
conn =db.conn;
//conn.setAutoCommit(false);
System.out.println(sample);
System.out.println(filename);
//String filename;
//
//filename="c:/tomcat/webapps/nw/lsdoc/"+sample+".sxw";
String mysql= "select word from tmanuscript where glidenumber='"+sample+"' for update";
System.out.println(mysql);
stmt=conn.prepareStatement(mysql);
//stmt.setString(1,sample);
rs=stmt.executeQuery() ;
//System.out.println();
if(rs.next()) {
System.out.println(sample);
System.out.println(filename);
BLOB blob = ((OracleResultSet)rs).getBLOB("word");
fout = blob.getBinaryOutputStream();
File f = new File(filename);
fin = new FileInputStream(f);
byte[] buffer = new byte[blob.getBufferSize()];
int bytesRead = 0;
while((bytesRead = fin.read(buffer)) != -1)
{
fout.write(buffer, 0, bytesRead);
System.out.println(bytesRead);
}
blob = null;
f = null;
buffer = null;
fin.close();
fout.close();
conn.commit();
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally {
try {
fin = null;
fout = null;
rs = null;
conn = null;
stmt = null;
}
catch(Exception e) {
e.printStackTrace();
}
}
}
public void gread() throws SQLException,IOException
{
Connection conn = null;
PreparedStatement stmt = null;
InputStream in = null;
OutputStream out = null;
BLOB blob = null;
ResultSet rs = null;
try {
newhua.dbbean db=new newhua.dbbean();
db.InitDatabase();
conn =db.conn;
//String filename;
//filename="c:/tomcat/webapps/nw/lsdoc/o_"+sample+".sxw";
String mysql="Select word FROM tmanuscript WHERE glidenumber ='"+sample+"' ";
System.out.println(mysql);
stmt = conn.prepareStatement(mysql);
//stmt.setString(1,"67");
rs = stmt.executeQuery();
if(rs.next()) {
blob = ((OracleResultSet)rs).getBLOB("word");
in = blob.getBinaryStream();
out = new FileOutputStream(filename);
int bufferSize = blob.getBufferSize();
byte[] buffer = new byte[bufferSize];
int bytesRead = 0;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
System.out.println(bytesRead);
}
stmt.clearParameters();
buffer = null;
in.close();
out.close();
conn.commit();
}
}
catch(Exception e) {
e.printStackTrace();
}
finally {
try {
in = null;
blob = null;
rs = null;
out = null;
conn = null;
stmt = null;
}
catch(Exception e) {
}
}
}
}
jsp:<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.io.*,java.util.*,java.sql.*,java.text.*,oracle.jdbc.driver.*,oracle.sql.*" %>
<HTML><HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="STYLESHEET" type="text/css" href="css/style.css">
<TITLE>发文--保存</TITLE>
<script language="javascript">
</script>
</HEAD>
<jsp:useBean id="writeBeanId" scope="session" class="newhua.writeBean" />
<jsp:setProperty name="writeBeanId" property="*" />
<BODY BGCOLOR=#CCCCc2 TEXT=#0f0000 LINK=#0000ff ALINK=#cc0000 VLINK=#306898 TOPMARGIN=5 MARGINHEIGHT="5" >
<%!
public static String UnicodeToChinese(String s){
try{
if(s==null||s.equals("")) return "";
String newstring=null;
newstring=new String(s.getBytes("ISO8859_1"),"gb2312");
return newstring;
}
catch(UnsupportedEncodingException e)
{
return s;
}
}
%>
<%
String glidenumber,comenumber,author,editor,buildtime,subject,flag;
glidenumber=UnicodeToChinese(request.getParameter("glidenumber")) ;
comenumber=UnicodeToChinese(request.getParameter("comenumber")) ;
author=UnicodeToChinese(request.getParameter("author")) ;
editor=UnicodeToChinese(request.getParameter("editor")) ;
buildtime=UnicodeToChinese(request.getParameter("buildtime")) ;
subject=UnicodeToChinese(request.getParameter("subject")) ;
flag=UnicodeToChinese(request.getParameter("flag")) ;
String mysql;
mysql="INSERT INTO SYSTEM.TMANUSCRIPT (COMENUMBER ,GLIDENUMBER ,AUTHOR ,FLAG ,EDITOR ,BUILDTIME,SUBJECT,WORD )";
mysql=mysql+" VALUES ('"+comenumber+"' ,'"+glidenumber+"' ,'"+author+"' ,'"+flag+"' ,'"+editor;
mysql=mysql+" ' ,'"+buildtime+" ' ,'"+subject+"' ,EMPTY_BLOB() )";
out.println(mysql);
newhua.dbbean db=new newhua.dbbean();
db.executeSql(mysql);
String filename="c:/lsdoc/"+glidenumber+".sxw";
writeBeanId.setFilename(filename);
writeBeanId.setSample(glidenumber);
System.out.println(filename);
System.out.println(glidenumber);
writeBeanId.gwrite() ;
response.sendRedirect("search.jsp");
%>
</BODY>
</HTML>
import java.io.*;
import java.util.*;
import java.sql.*;
import java.text.*;
import oracle.sql.*;
import oracle.jdbc.driver.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
public class writeBean {
private String sample = "";
//Access sample property
public String getSample() {
return sample;
}
//Access sample property
public void setSample(String newValue) {
if (newValue!=null) {
sample = newValue;
}
}
private String filename = "";
//Access sample property
public String getFilename() {
return filename;
}
//Access sample property
public void setFilename(String newValue) {
if (newValue!=null) {
filename = newValue;
}
}
public void gwrite() throws SQLException,IOException
{
PreparedStatement stmt = null;
ResultSet rs = null;
InputStream fin = null;
OutputStream fout = null;
Connection conn=null;
try{
newhua.dbbean db=new newhua.dbbean();
db.InitDatabase();
conn =db.conn;
//conn.setAutoCommit(false);
System.out.println(sample);
System.out.println(filename);
//String filename;
//
//filename="c:/tomcat/webapps/nw/lsdoc/"+sample+".sxw";
String mysql= "select word from tmanuscript where glidenumber='"+sample+"' for update";
System.out.println(mysql);
stmt=conn.prepareStatement(mysql);
//stmt.setString(1,sample);
rs=stmt.executeQuery() ;
//System.out.println();
if(rs.next()) {
System.out.println(sample);
System.out.println(filename);
BLOB blob = ((OracleResultSet)rs).getBLOB("word");
fout = blob.getBinaryOutputStream();
File f = new File(filename);
fin = new FileInputStream(f);
byte[] buffer = new byte[blob.getBufferSize()];
int bytesRead = 0;
while((bytesRead = fin.read(buffer)) != -1)
{
fout.write(buffer, 0, bytesRead);
System.out.println(bytesRead);
}
blob = null;
f = null;
buffer = null;
fin.close();
fout.close();
conn.commit();
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally {
try {
fin = null;
fout = null;
rs = null;
conn = null;
stmt = null;
}
catch(Exception e) {
e.printStackTrace();
}
}
}
public void gread() throws SQLException,IOException
{
Connection conn = null;
PreparedStatement stmt = null;
InputStream in = null;
OutputStream out = null;
BLOB blob = null;
ResultSet rs = null;
try {
newhua.dbbean db=new newhua.dbbean();
db.InitDatabase();
conn =db.conn;
//String filename;
//filename="c:/tomcat/webapps/nw/lsdoc/o_"+sample+".sxw";
String mysql="Select word FROM tmanuscript WHERE glidenumber ='"+sample+"' ";
System.out.println(mysql);
stmt = conn.prepareStatement(mysql);
//stmt.setString(1,"67");
rs = stmt.executeQuery();
if(rs.next()) {
blob = ((OracleResultSet)rs).getBLOB("word");
in = blob.getBinaryStream();
out = new FileOutputStream(filename);
int bufferSize = blob.getBufferSize();
byte[] buffer = new byte[bufferSize];
int bytesRead = 0;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
System.out.println(bytesRead);
}
stmt.clearParameters();
buffer = null;
in.close();
out.close();
conn.commit();
}
}
catch(Exception e) {
e.printStackTrace();
}
finally {
try {
in = null;
blob = null;
rs = null;
out = null;
conn = null;
stmt = null;
}
catch(Exception e) {
}
}
}
}
jsp:<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.io.*,java.util.*,java.sql.*,java.text.*,oracle.jdbc.driver.*,oracle.sql.*" %>
<HTML><HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="STYLESHEET" type="text/css" href="css/style.css">
<TITLE>发文--保存</TITLE>
<script language="javascript">
</script>
</HEAD>
<jsp:useBean id="writeBeanId" scope="session" class="newhua.writeBean" />
<jsp:setProperty name="writeBeanId" property="*" />
<BODY BGCOLOR=#CCCCc2 TEXT=#0f0000 LINK=#0000ff ALINK=#cc0000 VLINK=#306898 TOPMARGIN=5 MARGINHEIGHT="5" >
<%!
public static String UnicodeToChinese(String s){
try{
if(s==null||s.equals("")) return "";
String newstring=null;
newstring=new String(s.getBytes("ISO8859_1"),"gb2312");
return newstring;
}
catch(UnsupportedEncodingException e)
{
return s;
}
}
%>
<%
String glidenumber,comenumber,author,editor,buildtime,subject,flag;
glidenumber=UnicodeToChinese(request.getParameter("glidenumber")) ;
comenumber=UnicodeToChinese(request.getParameter("comenumber")) ;
author=UnicodeToChinese(request.getParameter("author")) ;
editor=UnicodeToChinese(request.getParameter("editor")) ;
buildtime=UnicodeToChinese(request.getParameter("buildtime")) ;
subject=UnicodeToChinese(request.getParameter("subject")) ;
flag=UnicodeToChinese(request.getParameter("flag")) ;
String mysql;
mysql="INSERT INTO SYSTEM.TMANUSCRIPT (COMENUMBER ,GLIDENUMBER ,AUTHOR ,FLAG ,EDITOR ,BUILDTIME,SUBJECT,WORD )";
mysql=mysql+" VALUES ('"+comenumber+"' ,'"+glidenumber+"' ,'"+author+"' ,'"+flag+"' ,'"+editor;
mysql=mysql+" ' ,'"+buildtime+" ' ,'"+subject+"' ,EMPTY_BLOB() )";
out.println(mysql);
newhua.dbbean db=new newhua.dbbean();
db.executeSql(mysql);
String filename="c:/lsdoc/"+glidenumber+".sxw";
writeBeanId.setFilename(filename);
writeBeanId.setSample(glidenumber);
System.out.println(filename);
System.out.println(glidenumber);
writeBeanId.gwrite() ;
response.sendRedirect("search.jsp");
%>
</BODY>
</HTML>
#2
假如是一个GIF图片
JSP中
<img border="0" src="yourServlet" width="283" height="212">
//在yourServlet后面可以跟参数
在yourServlet中
从数据库读出二进制流,象out中写
在最前面
response.setContentType("image/gif");
JSP中
<img border="0" src="yourServlet" width="283" height="212">
//在yourServlet后面可以跟参数
在yourServlet中
从数据库读出二进制流,象out中写
在最前面
response.setContentType("image/gif");
#3
thx,研究一下先。
请各位高手继续支持。
请各位高手继续支持。
#4
不好意思还是没搞定。
to wjmmml(笑着悲伤) :writeBeanId.gread()是实现读取的功能吧。但是我如何把读出的图片显示在页面上呢?可否给我讲解一下?
to AYellow(北斗猪) :我不太明白你的意思response.setContentType("image/gif");加在什么地方啊,可不可以给我讲的清楚一点?如果用这种方式那么我的yourServlet应该怎么写呢?
请多多帮忙3q
to wjmmml(笑着悲伤) :writeBeanId.gread()是实现读取的功能吧。但是我如何把读出的图片显示在页面上呢?可否给我讲解一下?
to AYellow(北斗猪) :我不太明白你的意思response.setContentType("image/gif");加在什么地方啊,可不可以给我讲的清楚一点?如果用这种方式那么我的yourServlet应该怎么写呢?
请多多帮忙3q
#5
//Servlet1.java 注意我是从文件中读来的
package test;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class Servlet1 extends HttpServlet {
static final private String CONTENT_TYPE = "image/gif";
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
OutputStream out = response.getOutputStream();
FileInputStream fis = new FileInputStream("c:\\aaa.gif");
int b = fis.read();
while(b!=-1){
out.write(b);
b = fis.read();
}
fis.close();
}
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws javax.servlet.ServletException, java.io.IOException {
doGet(req,resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws javax.servlet.ServletException, java.io.IOException {
doGet(req,resp);
}
}
////////////////////////////////////////////////////////////////////
//JSP
<%@ page contentType="text/html; charset=GB2312" %>
<html>
<head>
<title>Jsp1</title>
</head>
<body>
<img border="0" src="servlet1">
</body>
</html>
package test;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class Servlet1 extends HttpServlet {
static final private String CONTENT_TYPE = "image/gif";
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
OutputStream out = response.getOutputStream();
FileInputStream fis = new FileInputStream("c:\\aaa.gif");
int b = fis.read();
while(b!=-1){
out.write(b);
b = fis.read();
}
fis.close();
}
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws javax.servlet.ServletException, java.io.IOException {
doGet(req,resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws javax.servlet.ServletException, java.io.IOException {
doGet(req,resp);
}
}
////////////////////////////////////////////////////////////////////
//JSP
<%@ page contentType="text/html; charset=GB2312" %>
<html>
<head>
<title>Jsp1</title>
</head>
<body>
<img border="0" src="servlet1">
</body>
</html>
#6
终于搞定。
多谢 AYellow(北斗猪)和 wjmmml(笑着悲伤)。
放分。以后还请继续指教。
多谢 AYellow(北斗猪)和 wjmmml(笑着悲伤)。
放分。以后还请继续指教。
#1
package newhua;
import java.io.*;
import java.util.*;
import java.sql.*;
import java.text.*;
import oracle.sql.*;
import oracle.jdbc.driver.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
public class writeBean {
private String sample = "";
//Access sample property
public String getSample() {
return sample;
}
//Access sample property
public void setSample(String newValue) {
if (newValue!=null) {
sample = newValue;
}
}
private String filename = "";
//Access sample property
public String getFilename() {
return filename;
}
//Access sample property
public void setFilename(String newValue) {
if (newValue!=null) {
filename = newValue;
}
}
public void gwrite() throws SQLException,IOException
{
PreparedStatement stmt = null;
ResultSet rs = null;
InputStream fin = null;
OutputStream fout = null;
Connection conn=null;
try{
newhua.dbbean db=new newhua.dbbean();
db.InitDatabase();
conn =db.conn;
//conn.setAutoCommit(false);
System.out.println(sample);
System.out.println(filename);
//String filename;
//
//filename="c:/tomcat/webapps/nw/lsdoc/"+sample+".sxw";
String mysql= "select word from tmanuscript where glidenumber='"+sample+"' for update";
System.out.println(mysql);
stmt=conn.prepareStatement(mysql);
//stmt.setString(1,sample);
rs=stmt.executeQuery() ;
//System.out.println();
if(rs.next()) {
System.out.println(sample);
System.out.println(filename);
BLOB blob = ((OracleResultSet)rs).getBLOB("word");
fout = blob.getBinaryOutputStream();
File f = new File(filename);
fin = new FileInputStream(f);
byte[] buffer = new byte[blob.getBufferSize()];
int bytesRead = 0;
while((bytesRead = fin.read(buffer)) != -1)
{
fout.write(buffer, 0, bytesRead);
System.out.println(bytesRead);
}
blob = null;
f = null;
buffer = null;
fin.close();
fout.close();
conn.commit();
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally {
try {
fin = null;
fout = null;
rs = null;
conn = null;
stmt = null;
}
catch(Exception e) {
e.printStackTrace();
}
}
}
public void gread() throws SQLException,IOException
{
Connection conn = null;
PreparedStatement stmt = null;
InputStream in = null;
OutputStream out = null;
BLOB blob = null;
ResultSet rs = null;
try {
newhua.dbbean db=new newhua.dbbean();
db.InitDatabase();
conn =db.conn;
//String filename;
//filename="c:/tomcat/webapps/nw/lsdoc/o_"+sample+".sxw";
String mysql="Select word FROM tmanuscript WHERE glidenumber ='"+sample+"' ";
System.out.println(mysql);
stmt = conn.prepareStatement(mysql);
//stmt.setString(1,"67");
rs = stmt.executeQuery();
if(rs.next()) {
blob = ((OracleResultSet)rs).getBLOB("word");
in = blob.getBinaryStream();
out = new FileOutputStream(filename);
int bufferSize = blob.getBufferSize();
byte[] buffer = new byte[bufferSize];
int bytesRead = 0;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
System.out.println(bytesRead);
}
stmt.clearParameters();
buffer = null;
in.close();
out.close();
conn.commit();
}
}
catch(Exception e) {
e.printStackTrace();
}
finally {
try {
in = null;
blob = null;
rs = null;
out = null;
conn = null;
stmt = null;
}
catch(Exception e) {
}
}
}
}
jsp:<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.io.*,java.util.*,java.sql.*,java.text.*,oracle.jdbc.driver.*,oracle.sql.*" %>
<HTML><HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="STYLESHEET" type="text/css" href="css/style.css">
<TITLE>发文--保存</TITLE>
<script language="javascript">
</script>
</HEAD>
<jsp:useBean id="writeBeanId" scope="session" class="newhua.writeBean" />
<jsp:setProperty name="writeBeanId" property="*" />
<BODY BGCOLOR=#CCCCc2 TEXT=#0f0000 LINK=#0000ff ALINK=#cc0000 VLINK=#306898 TOPMARGIN=5 MARGINHEIGHT="5" >
<%!
public static String UnicodeToChinese(String s){
try{
if(s==null||s.equals("")) return "";
String newstring=null;
newstring=new String(s.getBytes("ISO8859_1"),"gb2312");
return newstring;
}
catch(UnsupportedEncodingException e)
{
return s;
}
}
%>
<%
String glidenumber,comenumber,author,editor,buildtime,subject,flag;
glidenumber=UnicodeToChinese(request.getParameter("glidenumber")) ;
comenumber=UnicodeToChinese(request.getParameter("comenumber")) ;
author=UnicodeToChinese(request.getParameter("author")) ;
editor=UnicodeToChinese(request.getParameter("editor")) ;
buildtime=UnicodeToChinese(request.getParameter("buildtime")) ;
subject=UnicodeToChinese(request.getParameter("subject")) ;
flag=UnicodeToChinese(request.getParameter("flag")) ;
String mysql;
mysql="INSERT INTO SYSTEM.TMANUSCRIPT (COMENUMBER ,GLIDENUMBER ,AUTHOR ,FLAG ,EDITOR ,BUILDTIME,SUBJECT,WORD )";
mysql=mysql+" VALUES ('"+comenumber+"' ,'"+glidenumber+"' ,'"+author+"' ,'"+flag+"' ,'"+editor;
mysql=mysql+" ' ,'"+buildtime+" ' ,'"+subject+"' ,EMPTY_BLOB() )";
out.println(mysql);
newhua.dbbean db=new newhua.dbbean();
db.executeSql(mysql);
String filename="c:/lsdoc/"+glidenumber+".sxw";
writeBeanId.setFilename(filename);
writeBeanId.setSample(glidenumber);
System.out.println(filename);
System.out.println(glidenumber);
writeBeanId.gwrite() ;
response.sendRedirect("search.jsp");
%>
</BODY>
</HTML>
import java.io.*;
import java.util.*;
import java.sql.*;
import java.text.*;
import oracle.sql.*;
import oracle.jdbc.driver.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
public class writeBean {
private String sample = "";
//Access sample property
public String getSample() {
return sample;
}
//Access sample property
public void setSample(String newValue) {
if (newValue!=null) {
sample = newValue;
}
}
private String filename = "";
//Access sample property
public String getFilename() {
return filename;
}
//Access sample property
public void setFilename(String newValue) {
if (newValue!=null) {
filename = newValue;
}
}
public void gwrite() throws SQLException,IOException
{
PreparedStatement stmt = null;
ResultSet rs = null;
InputStream fin = null;
OutputStream fout = null;
Connection conn=null;
try{
newhua.dbbean db=new newhua.dbbean();
db.InitDatabase();
conn =db.conn;
//conn.setAutoCommit(false);
System.out.println(sample);
System.out.println(filename);
//String filename;
//
//filename="c:/tomcat/webapps/nw/lsdoc/"+sample+".sxw";
String mysql= "select word from tmanuscript where glidenumber='"+sample+"' for update";
System.out.println(mysql);
stmt=conn.prepareStatement(mysql);
//stmt.setString(1,sample);
rs=stmt.executeQuery() ;
//System.out.println();
if(rs.next()) {
System.out.println(sample);
System.out.println(filename);
BLOB blob = ((OracleResultSet)rs).getBLOB("word");
fout = blob.getBinaryOutputStream();
File f = new File(filename);
fin = new FileInputStream(f);
byte[] buffer = new byte[blob.getBufferSize()];
int bytesRead = 0;
while((bytesRead = fin.read(buffer)) != -1)
{
fout.write(buffer, 0, bytesRead);
System.out.println(bytesRead);
}
blob = null;
f = null;
buffer = null;
fin.close();
fout.close();
conn.commit();
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally {
try {
fin = null;
fout = null;
rs = null;
conn = null;
stmt = null;
}
catch(Exception e) {
e.printStackTrace();
}
}
}
public void gread() throws SQLException,IOException
{
Connection conn = null;
PreparedStatement stmt = null;
InputStream in = null;
OutputStream out = null;
BLOB blob = null;
ResultSet rs = null;
try {
newhua.dbbean db=new newhua.dbbean();
db.InitDatabase();
conn =db.conn;
//String filename;
//filename="c:/tomcat/webapps/nw/lsdoc/o_"+sample+".sxw";
String mysql="Select word FROM tmanuscript WHERE glidenumber ='"+sample+"' ";
System.out.println(mysql);
stmt = conn.prepareStatement(mysql);
//stmt.setString(1,"67");
rs = stmt.executeQuery();
if(rs.next()) {
blob = ((OracleResultSet)rs).getBLOB("word");
in = blob.getBinaryStream();
out = new FileOutputStream(filename);
int bufferSize = blob.getBufferSize();
byte[] buffer = new byte[bufferSize];
int bytesRead = 0;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
System.out.println(bytesRead);
}
stmt.clearParameters();
buffer = null;
in.close();
out.close();
conn.commit();
}
}
catch(Exception e) {
e.printStackTrace();
}
finally {
try {
in = null;
blob = null;
rs = null;
out = null;
conn = null;
stmt = null;
}
catch(Exception e) {
}
}
}
}
jsp:<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.io.*,java.util.*,java.sql.*,java.text.*,oracle.jdbc.driver.*,oracle.sql.*" %>
<HTML><HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="STYLESHEET" type="text/css" href="css/style.css">
<TITLE>发文--保存</TITLE>
<script language="javascript">
</script>
</HEAD>
<jsp:useBean id="writeBeanId" scope="session" class="newhua.writeBean" />
<jsp:setProperty name="writeBeanId" property="*" />
<BODY BGCOLOR=#CCCCc2 TEXT=#0f0000 LINK=#0000ff ALINK=#cc0000 VLINK=#306898 TOPMARGIN=5 MARGINHEIGHT="5" >
<%!
public static String UnicodeToChinese(String s){
try{
if(s==null||s.equals("")) return "";
String newstring=null;
newstring=new String(s.getBytes("ISO8859_1"),"gb2312");
return newstring;
}
catch(UnsupportedEncodingException e)
{
return s;
}
}
%>
<%
String glidenumber,comenumber,author,editor,buildtime,subject,flag;
glidenumber=UnicodeToChinese(request.getParameter("glidenumber")) ;
comenumber=UnicodeToChinese(request.getParameter("comenumber")) ;
author=UnicodeToChinese(request.getParameter("author")) ;
editor=UnicodeToChinese(request.getParameter("editor")) ;
buildtime=UnicodeToChinese(request.getParameter("buildtime")) ;
subject=UnicodeToChinese(request.getParameter("subject")) ;
flag=UnicodeToChinese(request.getParameter("flag")) ;
String mysql;
mysql="INSERT INTO SYSTEM.TMANUSCRIPT (COMENUMBER ,GLIDENUMBER ,AUTHOR ,FLAG ,EDITOR ,BUILDTIME,SUBJECT,WORD )";
mysql=mysql+" VALUES ('"+comenumber+"' ,'"+glidenumber+"' ,'"+author+"' ,'"+flag+"' ,'"+editor;
mysql=mysql+" ' ,'"+buildtime+" ' ,'"+subject+"' ,EMPTY_BLOB() )";
out.println(mysql);
newhua.dbbean db=new newhua.dbbean();
db.executeSql(mysql);
String filename="c:/lsdoc/"+glidenumber+".sxw";
writeBeanId.setFilename(filename);
writeBeanId.setSample(glidenumber);
System.out.println(filename);
System.out.println(glidenumber);
writeBeanId.gwrite() ;
response.sendRedirect("search.jsp");
%>
</BODY>
</HTML>
#2
假如是一个GIF图片
JSP中
<img border="0" src="yourServlet" width="283" height="212">
//在yourServlet后面可以跟参数
在yourServlet中
从数据库读出二进制流,象out中写
在最前面
response.setContentType("image/gif");
JSP中
<img border="0" src="yourServlet" width="283" height="212">
//在yourServlet后面可以跟参数
在yourServlet中
从数据库读出二进制流,象out中写
在最前面
response.setContentType("image/gif");
#3
thx,研究一下先。
请各位高手继续支持。
请各位高手继续支持。
#4
不好意思还是没搞定。
to wjmmml(笑着悲伤) :writeBeanId.gread()是实现读取的功能吧。但是我如何把读出的图片显示在页面上呢?可否给我讲解一下?
to AYellow(北斗猪) :我不太明白你的意思response.setContentType("image/gif");加在什么地方啊,可不可以给我讲的清楚一点?如果用这种方式那么我的yourServlet应该怎么写呢?
请多多帮忙3q
to wjmmml(笑着悲伤) :writeBeanId.gread()是实现读取的功能吧。但是我如何把读出的图片显示在页面上呢?可否给我讲解一下?
to AYellow(北斗猪) :我不太明白你的意思response.setContentType("image/gif");加在什么地方啊,可不可以给我讲的清楚一点?如果用这种方式那么我的yourServlet应该怎么写呢?
请多多帮忙3q
#5
//Servlet1.java 注意我是从文件中读来的
package test;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class Servlet1 extends HttpServlet {
static final private String CONTENT_TYPE = "image/gif";
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
OutputStream out = response.getOutputStream();
FileInputStream fis = new FileInputStream("c:\\aaa.gif");
int b = fis.read();
while(b!=-1){
out.write(b);
b = fis.read();
}
fis.close();
}
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws javax.servlet.ServletException, java.io.IOException {
doGet(req,resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws javax.servlet.ServletException, java.io.IOException {
doGet(req,resp);
}
}
////////////////////////////////////////////////////////////////////
//JSP
<%@ page contentType="text/html; charset=GB2312" %>
<html>
<head>
<title>Jsp1</title>
</head>
<body>
<img border="0" src="servlet1">
</body>
</html>
package test;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class Servlet1 extends HttpServlet {
static final private String CONTENT_TYPE = "image/gif";
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
OutputStream out = response.getOutputStream();
FileInputStream fis = new FileInputStream("c:\\aaa.gif");
int b = fis.read();
while(b!=-1){
out.write(b);
b = fis.read();
}
fis.close();
}
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws javax.servlet.ServletException, java.io.IOException {
doGet(req,resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws javax.servlet.ServletException, java.io.IOException {
doGet(req,resp);
}
}
////////////////////////////////////////////////////////////////////
//JSP
<%@ page contentType="text/html; charset=GB2312" %>
<html>
<head>
<title>Jsp1</title>
</head>
<body>
<img border="0" src="servlet1">
</body>
</html>
#6
终于搞定。
多谢 AYellow(北斗猪)和 wjmmml(笑着悲伤)。
放分。以后还请继续指教。
多谢 AYellow(北斗猪)和 wjmmml(笑着悲伤)。
放分。以后还请继续指教。