1
<html>
2
<head>
3
<title>Welcome</title>
4
</head>
5
<body>
6
<form action="insert2.jsp" method="post">
7
圖片名稱<input type="text" name="name" size="80"><br>
8
文件<input type="file" name="file"><br>
9
<input type="submit" name="submit" value="提交">
10
</form>
11
<center>Welcome</center>
12
</body>
13
</html>
14

2

3

4

5

6

7

8

9

10

11

12

13

14

然后寫blob數(shù)據(jù)的輸入程序(insert2.jsp)
1
<%@ page language="java" contentType="text/html;charset=gb2312"%>
2
<%@page import="java.sql.*"%>
3
<%@page import="java.io.*"%>
4
<%@page import="java.util.*"%>
5
<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
6
<html>
7
<head>
8
<title>BLOB</title>
9
</head>
10
<body bgcolor="#FFFFFF">
11
<%
12
String name=request.getParameter("name");
13
String file=request.getParameter("file");
14
Connection conn;
15
String strConn;
16
request.setCharacterEncoding("gb2312");
17
try{
18
out.print(file+"<br>");
19
File files=new File(file); //定義一個file
20
FileInputStream str=new FileInputStream(files);//文件流
21
Class.forName("org.gjt.mm.mysql.Driver").newInstance();//注冊MYSQL驅(qū)動
22
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/book","root","root");//數(shù)據(jù)庫連接
23
String sql="insert into sample(name,image) values(?,?)";
24
PreparedStatement pstmt=conn.prepareStatement(sql)編;//預(yù)譯
25
pstmt.setString(1,name);
26
pstmt.setBinaryStream(2,str,(int)files.length());
27
try{
28
pstmt.executeUpdate();
29
}
30
catch(SQLException e){
31
out.print(e.getMessage());
32
}
33
pstmt.close();
34
conn.close();
35
36
out.print("111111");
37
}
38
catch(SQLException e){
39
out.print(e.getMessage());
40
}
41
catch(IOException e){
42
out.print(e.getMessage());
43
}
44
%>
45
<a href="readblob2.jsp?name=<%=name%>">查看圖片</a>
46
47
Write your content here
48
asdfasdfasdfsadf
49
</body>
50
</html>
51

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

現(xiàn)在在寫個readblob把寫如數(shù)據(jù)庫中的blob字段輸出到瀏覽器中:(readblob2.jsp)




















































小時候家的對面有一座山,山的上面就是藍(lán)天,所以總是幻想著有一天站到山頂用手摸一下藍(lán)天……