站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题一个Jsp初学者的学习过程(八)

一个Jsp初学者的学习过程(八)

摘要: 一个Jsp初学者的学习过程(八)
一个Jsp初学者的学习过程(八)

TheUnforgiven


第八章 图片文件的操作——Blob类型数据的存取和使用第一个Servlet

关于这部分内容,我在网上找到一些资料,最后按照我的需求,经过改编得到了下面的代码:
------------------------------upphoto.htm------------------------------------

<head>

无标题文档
head>

<body>
上传图片:
<form name="form1" method="post" action="upphoto.jsp">
<input name="id" type="text">
(输入一个整数作为该图片的ID)

<input size="50" name="file" type="file">


<input type="submit" name="Submit" value="提交">
</form>

&nbsp;


&nbsp;


显示图片:



<form name="form2" method="post" action="showphoto.jsp">
<input type="text" name="vid">
(输入该图片的ID)

<input type="submit" name="Submit2" value="提交">
</form>
</body>

---------------------------------------------------------------------------
upphoto.htm包括两个<form>,form1用于选择要存于数据库的图片;form2用于显示一张数据库里的图片。
-----------------------------upphoto.jsp----------------------------------
<%@ include file="include.inc"%>
<%@ page contentType="text/html;charset=gb2312"%>
HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<head>

无标题文档
</head>

<body>
<%
int id=Integer.parseInt(request.getParameter("id"));
request.setCharacterEncoding("gb2312");
String f=request.getParameter("file");//得到路径,如:c:\d\e.jpg
String fpath=f.replaceFirst("\\\\","\\\\\\\\");//把一个\变成两个\,即路径改成:c:\\d\e.jpg

Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
Class.forName(CLASSFORNAME);//载入驱动程式类别
con=DriverManager.getConnection(SERVANDDB);//建立数据库连接
con.setAutoCommit(false);
String sql="insert into blb(id,blob) values("+id+",empty_blob())";//数据库里那个表名叫blb,字段的名就叫blob
pstmt=con.prepareStatement(sql);//添加一条blob字段为空的记录,
pstmt.executeUpdate();
pstmt=null;
sql="select * from blb where id="+id+" for update";
pstmt=con.prepareStatement(sql);//查找刚刚添加的那条记录
rs=pstmt.executeQuery();
if (rs.next())
{
oracle.sql.BLOB osb = (oracle.sql.BLOB) rs.getBlob("blob");
//到数据库的输出流
OutputStream outStream = osb.getBinaryOutputStream();
//这里用一个文件模拟输入流
File file = new File(fpath);
InputStream inStream = new FileInputStream(file);
//将输入流写到输出流
byte[] b = new byte[osb.getBufferSize()];
int len = 0;
while ( (len = inStream.read(b)) != -1)
{
outStream.write(b, 0, len);
}
//依次关闭(注意顺序)
inStream.close();
outStream.flush();
outStream.close();
con.commit();
rs.close();
pstmt.close();
con.close();

out.print("<script>");
out.print("alert('操作成功!');");
out.print("window.location.href='upphoto.htm';");
out.print("</script>");
}//if
}//try
catch(Exception e)
{out.print(e);}
%>
</body>

----------------------------------------------------------------------------
upphoto.jsp对图片进行存入数据库操作。注意需要将得到的文件的路径改变一下格式:c:\d\e.jpg改成c:\\d\e.jpg
------------------------------showphoto.jsp---------------------------------
<%@ page contentType="text/html;charset=gb2312"%>

<head>
显示图片
</head>
<body>
<%
String id=request.getParameter("vid");
%>
<table>
<tr>
<td colspan="3"> "></td>
</tr>
</table>
</body>

---------------------------------------------------------------------------
showphoto.jsp的这句是关键:src="http://ringz/photo?id=<%=id%>",它说明调用了一个Servlet,这个Servlet的名字叫photo,而且需要给它传一个值(id)。下面看这个Servlet的代码:
---------------------------PhotoServlet.java------------------------------
package ringz.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.lang.*;
import java.sql.*;

public class PhotoServlet extends HttpServlet//和javabean一样是“类”,所以类名同样要和文件名一致
{
private String CLASSFORNAME = "oracle.jdbc.driver.OracleDriver";
private String SERVANDDB = "jdbc:oracle:thin:name/password@ringz:1521:rock";
Connection con = null;
PreparedStatement psmt = null;
ResultSet rs = null;

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
int id = Integer.parseInt(request.getParameter("id"));
try
{
Class.forName(CLASSFORNAME);
con=DriverManager.getConnection(SERVANDDB);
con.setAutoCommit(false);
String sql = "select * from clb where id="+id;
psmt = con.prepareStatement(sql);
rs = psmt.executeQuery();
if (rs.next())
{
Blob bb = rs.getBlob("blob");
InputStream instream = bb.getBinaryStream();
response.setContentType("image/*");
OutputStream outStream = response.getOutputStream();
byte[] bytes = new byte[1024];
int i=0;
while ( (i = instream.read(bytes)) != -1)
{
outStream.write(bytes, 0, i);
}
instream.close();
outStream.close();
outStream = null;
con.commit();
rs.close();
psmt.close();
con.close();
}//if
}//try
catch (Exception ex)
{}
}//doGet
}
--------------------------------------------------------------------------
关于Servlet的知识请详细参阅有关参考书。下面说编译的事情:Servlet像javabean一样需要编译成.class文件,编译方法也和javabean一样。但是我在编译的时候发现出了错误,错误提示如下:
package javax.servlet does not exist
import javax.servlet.*;
^
我分析是少了什么东西造成的,于是在网上查资料,最后终于得出原因:环境变量里没有指出servlet相关的包的位置。解决办法:将原来的环境变量里的classpath添加一条:d:\j2sdk1.4.2_07\lib\servlet.jar;
以我的为例,这是原来的:
classpath——.;d:\j2sdk1.4.2_07\lib\tools.jar;d:\j2sdk1.4.2_07\lib\dt.jar;
这是修改后的:
classpath——.;d:\j2sdk1.4.2_07\lib\tools.jar;d:\j2sdk1.4.2_07\lib\dt.jar;d:\j2sdk1.4.2_07\lib\servlet.jar;
现在顺利的编译出了.class文件,但是同样有问题:文件放在哪?和使用javabean时一样,放在根目录e:\MyJsp下的WEB-INF\classes里,并且可以使用自己的包,比如我的在:e:\MyJsp\WEB-INF\classes\ringz\servlet下。接下来还有一个工作:给这个servlet进行“注册”:在WEB-INF下建一个web.xml文件,内容大致如下:
----------------------------------web.xml---------------------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>
web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">


RingZ's Station

RingZ's JSP



PhotoServlet
Servlet
ringz.servlet.PhotoServlet



PhotoServlet
/photo



---------------------------------------------------------------------------
其中最重要的是,如果你有其他的servlet也需要在这里“注册”一下。关于servlet的配置还有很多其他的内容,比如初始化参数、优先级、映射等等,请学习其他专业资料。顺便说一下:你可以到D:\Tomcat 5.0\conf里看看那里那个web.xml文件,我最开始的时候曾经把它复制到我的WEB-INF下,然后在里面添加了关于PhotoServlet的内容,结果Tomcat服务出错,我猜想是里边的一些内容发生的冲突,但具体是哪些我不清楚,也没有有研究。
现在,配置完毕,可以使用了。



结语

对实例的学习进行到现在,我已经对Jsp和Java有了初步的了解了,我知道是时候了——该回头找些书看看理论部分了,否则我永远也成不了一个程序员。
学习还要继续,本文就先到这里,假如大家认为我这个东西还有那么一点用处的话,我会把以后的阶段相继写出来;如果没有必要,那就算了吧:)。
请大家指教,扔玉——为了本民族的软件事业。