java中图片上传并在jsp页面显示

更新时间:2023-06-08 19:37:46 阅读: 评论:0

本文将阐述怎么使用commons-fileupload将图片上传至mysql数据库,并利用java rvlet将其在浏览器中显示
get ud to出来。
前提条件:
正确安装了mysql,tomcat,并下载了commons-fileupload-1.0.jar。
1,前台页面
---test_upload.htm---
<html>
<head>
<title>
上传测试
</title>
<body>
<form action="upload.jsp" enctype="multipart/form-data" method="post">
your name:
<br>
<input type="text" name="name">
<br>
file to process:
<input type="file" name="urfile">
<br>
<input type="submit">
</form>
</body>
</html>
2,上传的后台处理页面
---upload.jsp---
<%@ page contentType="text/html; chart=gb2312" %>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ page import="s.fileupload.*" %>
<%@ page import="del.DbManipulate" %>
<%
DiskFileUpload upload=new DiskFileUpload();
upload.tSizeThreshold(4096);
upload.tRepositoryPath("d:/java/");
upload.tSizeMax(10000000);
List fileItems = upload.parRequest(request);
Iterator i = fileItems.iterator();
String name = ((()).getString();
FileItem fi = (();
String fileName = fi.getName();
//System.out.println(fileName);
fileName = place(':','_');
fileName = place('\\','_');
//System.out.println(fileName);
//File savedFile = new File("d:/java/upload/",fileName);
//fi.write(savedFile);
InputStream InputStream();
DbManipulate dm=new DbManipulate();
Long long_size=new Size());
int size=long_size.intValue();
PreparedStatement ps=dm.DbUpdate("inrt into photo(image,description) values(?,?)");
ps.tBinaryStream(1,fis,size);
ps.tString(2,fileName);
String sql="lect id from photo order by id desc limit 1";
PreparedStatement ps2=dm.DbQuery(sql);
ResultSet uteQuery();
hfc
int index=0;
())
{
Int(1);
}
//ps.clo();
//fis.cho();
%>
<%=name%>  <a href="(-1)">go on</a>
<br>
<iframe height="600" width="800" src=photoview?id=<%=index%> scrolling=true>
3,显示图片的rvlet
---BlobServlet.java----
package com.cf.photo;
import java.io.*;
import java.sql.*;
dec是几月import javax.sql.*;
import javax.rvlet.*;
import javax.rvlet.http.*;
//import s.logging.Log;
//import s.logging.LogFactory;
import del.*;
public class BlobServlet extends HttpServlet{
//private static Log log = Log(BlobServlet.class);
protected void doGet(HttpServletRequest request,HttpServletRespon respon)
throws ServletException,IOException{
ServletOutputStream OutputStream();
int id=Integer.Parameter("id"));
respon.tContentType("image/jpeg
");
out.write(getBlob(id));
out.flush();
out.clo();
}
public byte[] getBlob(int photoid){
String sql="lect image from photo where id=?";
//log.info(sql);
Blob blob=null;
byte[] bytes=null;
//String description="";
PreparedStatement pstmt=null;
ResultSet rs=null;
DbManipulate mydb=new DbManipulate();
try{
pstmt=mydb.DbQuery(sql);
pstmt.tInt(1,photoid);
uteQuery();
()){
Blob(1);
}
Bytes(1,(int)(blob.length()));
}catch(SQLException e){}
return bytes;
}
}
4,存储图片的表定义:
create databa web_exam;
u web_exam;
create table photo(
id int not null auto_increment primary key,
image mediumblob not null,
description varchar(100) null
)
;
5,应用程序的目录结构
aha%catalina_home%\webapps\demo
------test_upload.htm
------upload.jsp
------WEB-INF
|----l
|-----l
|-----lib
|-------mysql-connector-java-3.0.16-ga-bin.jar
|-------commons-fileupload-1.0.jar
|----src
|----com
|-----cf
|-----photo
|-----BlobServlet.java
|-----model
|----DbConnection.java
|----DbConst.java
vulnerable|----DbManipulate.java
6,l文件内容
<?xml version="1.0"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"/j2ee/dtds/web-app_2_3.dtd">
<web-app>
<rvlet>
<rvlet-name>photoview</rvlet-name>
<rvlet-class>com.cf.photo.BlobServlet</rvlet-class>
</rvlet>
<rvlet-mapping>
<rvlet-name>photoview</rvlet-name>
<url-pattern>/photoview/*</url-pattern>
</rvlet-mapping>
<welcome-file-list>
<welcome-file>default.jsp</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>test.jsp</welcome-file>
</welcome-file-list>
</web-app>
7,ant的buildfile内容
电子商务专业考研
------l------
<project name="buildweb" default="build" badir=".">
<property name="src" location="src"/>
<property name="lib" location="lib"/>
<property name="build" location="class"/>
<target name="build">
<mkdir dir="${build}" />
<javac srcdir="${src}" destdir="${build}">
<include name="**/*.java" />
</javac>
</target>
<target name="create-jars" depends="build">
<jar destfile="${lib}/myexam.jar" badir="${build}"/>
</target>
<target name="clean" depends="create-jars">
<delete dir="${build}"/>
</target>
</project>
8,DbConst.java
package del;
public interface DbConst{
public final static String JDBC_DRIVER="sql.jdbc.Driver";
public final static String JDBC_URL="jdbc:mysql://localhost:3306/web_exam?"
+"uUnicode=true&characterEncoding=GB2312";
public final static String JDBC_USER="root";
public final static String JDBC_PASS="chenfu";
}
9,DbConnection.java
package del;
import java.sql.*;
public class DbConnection implements DbConst{
private String jdbcDriver=JDBC_DRIVER;
private String databaURL=JDBC_URL;
private String dbUrname=JDBC_USER;
private String dbPassword=JDBC_PASS;
private Connection con=null;
public String getJdbcDriver(){
return this.jdbcDriver;
}
public void tJdbcDriver(String d){
this.jdbcDriver=d;
hac是什么意思}
public String getDatabaURL(){
return this.databaURL;
}
public void tDatabaURL(String url){
this.databaURL=url;
}
public String getDbUrname(){
return this.dbUrname;
}
public void tDbUrname(String u){
this.dbUrname=u;
}
public String getDbPassword(){
return this.dbPassword;
}
public void tDbPassword(String p){
this.dbPassword=p;
}姚明退役新闻发布会
public boolean isConnected(){
return (con!=null);
}
public void disconnected(){
if(con!=null){
try
{
con.clo();
}
catch (SQLException ignore)
{
}
finally {
con=null;
}
}
}
public void tCon(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(databaURL==null) throw new SQLException("No jdbcURL property");
try
{
Class.forName(jdbcDriver);
//log.info(jdbcDriver);
}
catch (ClassNotFoundException e)
{
throw new SQLException(jdbcDriver+"class could not loaded");
}
Connection(databaURL,dbUrname,dbPassword);
return con;
}
}
10,DbManipulate.java
package del;
import java.sql.*;
public class DbManipulate{
private Connection con=null;
private PreparedStatement pstmt=null;
public PreparedStatement DbQuery(String sql)throws SQLException{
DbConnection newcon=new DbConnection();
tefCon();
pstmt=con.prepareStatement(sql,
ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
//log.info(sql);
return pstmt;
}
public PreparedStatement DbUpdate(String sql)throws SQLException{
DbConnection newcon=new DbConnection();
Con();
pstmt=con.prepareStatement(sql);
return pstmt;
}
public void DbClo(){
if(pstmt!=null){
pstmt=null;
}
}
}
11,转到该web application的WEB-INF下,在命令行中执行ant,以编译源文件,然后打开tomcat服务器,
在浏览器中打开地址:localhost:8080/demo/testo make you feel my love
t_upload.htm,试着去上传一张jpg的图片,看看行不行。
12,欢迎大家提出宝贵意见,请联系我:
email:
msn:cf_asp_

本文发布于:2023-06-08 19:37:46,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/138437.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:图片   上传   打开   提出   页面
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图