如何将图片上传到数据库,并在JSP网页中动态的显示出来? 有smartupload啊。我是用它把图片传到服务器的一个文件夹下,然后把路径存到数据库里。再取出来。。:)嘿嘿已经做成一个个人相册了。。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在定义数据库时,应将字段类型定义为BLOB,然后上传就可以了 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> 这个是在ORACLE数据库中吧SQL SERVER2000中也可以存图片吗?好象没有BLOB这种数据类型啊 SQL Server2000中的image类型就是吧 struts2遇到了一个难以解决的问题 jsp初学,从哪里开始。 AJAX 写web程序 一般配置文件 大家都是放在什么地方 ,怎么读取的? 怎样中生成图形报表? 哪位能人能介绍一下目前比较有前途并且是常用的框架 【急问!!!】IP匹配问题, 怎样学习java的util包下面的东西? 如何控制输出字符串的显示长度(包括汉字<一个汉字有算两个字节>、数字、字母等) 有人说JDBC那种查询到数据库,取出数据,并赋给javabean,方式不好,请问还有什么更好的方式? struts datasource 设置问题,请教高手 在jsp中如何<font size="20">ddd</font>屏蔽掉
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>
SQL SERVER2000中也可以存图片吗?好象没有BLOB这种数据类型啊