2023年11月28日发(作者:)本文将阐述怎么使用commons-fileupload将图片上传至mysql数据库,并利用java servlet将其在浏览器中显示



出来。



前提条件:



正确安装了mysql,tomcat,并下载了。



1,前台页面



---test_---







<br><br>上传测试<br><br>





your name:









file to process:
















2,上传的后台处理页面



------



<%@ page contentType="text/html; charset=gb2312" %>

<%@ page import=".*" %>

<%@ page import=".*"%>



<%@ page import=".*"%>

<%@ page import="load.*" %>

<%@ page import="pulate" %>

<%

DiskFileUpload upload=new DiskFileUpload();

eThreshold(4096);

ositoryPath("d:/java/");

eMax(10000000);

List fileItems = equest(request);

Iterator i = or();

String name = ((FileItem)()).getString();

FileItem fi = (FileItem)();

String fileName = e();

//n(fileName);

fileName = e(':");

(getBlob(id));

();

();

}

public byte[] getBlob(int photoid){

String sql="select image from photo where id=?";

//(sql);

Blob blob=null;

byte[] bytes=null;

//String description="";

PreparedStatement pstmt=null;

ResultSet rs=null;

DbManipulate mydb=new DbManipulate();

try{

pstmt=y(sql);

(1,photoid);

rs=eQuery();

while(()){

blob=b(1);

}

bytes=es(1,(int)(()));

}catch(SQLException e){}

return bytes;

}

}



4,存储图片的表定义:



create database web_exam;



use web_exam;



create table photo(



id int not null auto_increment primary key,



image mediumblob not null,



description varchar(100) null



);



5,应用程序的目录结构



%catalina_home%webappsdemo



------test_



------



------WEB-INF



|----



|-----



|-----lib



|-------



|-------



|----src



|----com



|-----cf



|-----photo



|-----



|-----model



|----



|----



|----



6,文件内容






PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"/j2ee/dtds/web-app_2_">









photoview

rvlet





photoview

/photoview/*























7,ant的buildfile内容



------------













































8,



package ;



public interface DbConst{

public final static String JDBC_DRIVER="";

public final static String JDBC_URL="jdbc:mysql://localhost:3306/web_exam?"

+"useUnicode=true&characterEncoding=GB2312";

public final static String JDBC_USER="root";

public final static String JDBC_PASS="chenfu";

}



9,



package ;

import .*;

public class DbConnection implements DbConst{



private String jdbcDriver=JDBC_DRIVER;

private String databaseURL=JDBC_URL;

private String dbUsername=JDBC_USER;

private String dbPassword=JDBC_PASS;

private Connection con=null;

public String getJdbcDriver(){

return iver;

}

public void setJdbcDriver(String d){

iver=d;

}

public String getDatabaseURL(){

return seURL;

}

public void setDatabaseURL(String url){

seURL=url;

}

public String getDbUsername(){

return name;

}

public void setDbUsername(String u){

name=u;

}



public String getDbPassword(){

return word;

}

public void setDbPassword(String p){

word=p;

}

public boolean isConnected(){

return (con!=null);

}

public void disconnected(){

if(con!=null){

try

{

();

}

catch (SQLException ignore)

{

}

finally {

con=null;

}

}

}

public void setCon(Connection c){

=c;

}

public Connection getCon() throws SQLException{

if(isConnected()) throw new SQLException("Already connected");

if(jdbcDriver==null) throw new SQLException("No jdbcDriver property");

if(databaseURL==null) throw new SQLException("No jdbcURL property");

try

{

e(jdbcDriver);

//(jdbcDriver);

}

catch (ClassNotFoundException e)

{

throw new SQLException(jdbcDriver+"class could not loaded");

}

con=nection(databaseURL,dbUsername,dbPassword);

return con;

}

}





10,



package ;

import .*;

public class DbManipulate{

private Connection con=null;

private PreparedStatement pstmt=null;



public PreparedStatement DbQuery(String sql)throws SQLException{

DbConnection newcon=new DbConnection();

con=();

pstmt=eStatement(sql,

_SCROLL_SENSITIVE,_READ_ONLY);

//(sql);

return pstmt;

}

public PreparedStatement DbUpdate(String sql)throws SQLException{

DbConnection newcon=new DbConnection();

con=();

pstmt=eStatement(sql);

return pstmt;

}

public void DbClose(){

if(pstmt!=null){

pstmt=null;

}

}

}



11,转到该web application的WEB-INF下,在命令行中执行ant,以编译源文件,然后打开tomcat服务器,



在浏览器中打开地址:localhost:8080/demo/tes

t_,试着去上传一张jpg的图片,看看行不行。



12,欢迎大家提出宝贵意见,请联系我:



email:cf0124@



msn:cf_asp_master@