国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

JSP初級教程之跟我學JSP(八)

2024-09-05 00:17:19
字體:
來源:轉載
供稿:網友
第八章 Blob類型數據的存取和使用第一個Servlet—— 圖片文件的操作

以下是我經過改編得到的jsp代碼:
------------------------------upphoto.htm------------------------------------
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>無標題文檔</title>
head>

<body>
上傳圖片:
<form name="form1" method="post" action="upphoto.jsp">
<input name="id" type="text">
(輸入一個整數作為該圖片的ID)<br>
<input size="50" name="file" type="file">
<br>
<input type="submit" name="Submit" value="提交">
</form>
<p> </p>
<p> </p>
顯示圖片:<br>
<br>
<form name="form2" method="post" action="showphoto.jsp">
<input type="text" name="vid">
(輸入該圖片的ID)<br>
<input type="submit" name="Submit2" value="提交">
</form>
</body>
</html>
---------------------------------------------------------------------------
upphoto.htm包括兩個<form>,form1用于選擇要存于數據庫的圖片;form2用于顯示一張數據庫里的圖片。
-----------------------------upphoto.jsp----------------------------------
<%@ include file="include.inc"%>
<%@ page contentType="text/html;charset=gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>無標題文檔</title>
</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();

共3頁上一頁123下一頁
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 宁乡县| 郴州市| 防城港市| 贵州省| 乌拉特前旗| 新安县| 察隅县| 睢宁县| 电白县| 铜川市| 疏附县| 东辽县| 开化县| 岳西县| 荣成市| 射阳县| 定边县| 许昌县| 平陆县| 宁河县| 迭部县| 青浦区| 册亨县| 磐安县| 灌阳县| 克什克腾旗| 禄丰县| 武汉市| 广灵县| 安庆市| 荔浦县| 博客| 宣化县| 香港 | 博野县| 噶尔县| 枣庄市| 澎湖县| 高安市| 衡东县| 方城县|