JSP 4

 1 <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
3
4 <%
5 String path = request.getContextPath();
6 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
7 %>
8
9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
10 <html>
11 <head>
12 <base href="<%=basePath%>">
13
14 <title>My JSP 'index.jsp' starting page</title>
15 <meta http-equiv="pragma" content="no-cache">
16 <meta http-equiv="cache-control" content="no-cache">
17 <meta http-equiv="expires" content="0">
18 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
19 <meta http-equiv="description" content="This is my page">
20 <!--
21 <link rel="stylesheet" type="text/css" href="styles.css">
22 -->
23 </head>
24
25 <body>
26 <form action="upload2.jsp" method="post" enctype="Multipart/form-data">
27 <table>
28 <tr>
29 <td>请选择要上传的文件</td>
30 <td><input type="file" name="file1" size="40"/> </td>
31 </tr>
32 <tr>
33 <td>请输入文件的描述</td>
34 <td><input type="text" name="desc1" size="40"/> </td>
35 </tr>
36 <tr>
37 <td>请选择要上传的文件</td>
38 <td><input type="file" name="file2" size="40"/> </td>
39 </tr>
40 <tr>
41 <td>请输入文件的描述</td>
42 <td><input type="text" name="desc2" size="40"/> </td>
43 </tr>
44 <tr>
45 <td><input type="reset" value="重填"/></td>
46 <td><input type="submit" value="上传"/> </td>
47 </tr>
48 </table>
49 </form>
50 </body>
51 </html>

 

 1 <%@ page language="java" import="java.util.*,java.io.File,javax.naming.*,javax.sql.DataSource,java.sql.*" pageEncoding="GBK"%>
2 <%@ page import="org.apache.commons.fileupload.*"%>
3 <%
4 String path = request.getContextPath();
5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
6 %>
7
8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
9 <html>
10 <head>
11 <base href="<%=basePath%>">
12
13 <title>My JSP 'upload.jsp' starting page</title>
14
15 <meta http-equiv="pragma" content="no-cache">
16 <meta http-equiv="cache-control" content="no-cache">
17 <meta http-equiv="expires" content="0">
18 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
19 <meta http-equiv="description" content="This is my page">
20 <!--
21 <link rel="stylesheet" type="text/css" href="styles.css">
22 -->
23
24 </head>
25
26 <body>
27 <%
28
29 DiskFileUpload dfu =new DiskFileUpload();
30
31 //设置上传数据的最大数据为10M
32 dfu.setSizeMax(0xA00000);
33
34 dfu.setSizeThreshold(0x80000);
35
36 dfu.setRepositoryPath("E:\\fileupload");
37
38 List fileItems = dfu.parseRequest(request);
39 Iterator it = fileItems.iterator();
40
41 Context ctx =new InitialContext();
42 DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/bookstore");
43 Connection conn = ds.getConnection();
44 PreparedStatement pstmt = conn.prepareStatement("insert into upload (filenam,filesize,data) values (?,?,?)");
45
46 while(it.hasNext())
47 {
48 FileItem item = (FileItem)it.next();
49 if(!item.isFormField())
50 {
51 String name = item.getName();
52 int index = name.lastIndexOf(File.separator);
53 if(index >0)
54 {
55 name = name.substring(index +1,name.length());
56 }
57 long size = item.getSize();
58 if((name ==null || name.equals("")) && size ==0)
59 {
60 continue;
61 }
62
63 pstmt.setString(1,name);
64 pstmt.setInt(2,(int)size);
65 pstmt.setBinaryStream(3,item.getInputStream(),(int)size);
66 pstmt.executeUpdate();
67
68 }
69 }
70
71 if(pstmt !=null)
72 {
73 pstmt.close();
74 pstmt =null;
75 }
76 if(conn !=null)
77 {
78 conn.close();
79 conn =null;
80 }
81
82 out.println("上传成功");
83
84 %>
85
86 </body>
87 </html>

 

 1 <%@ page language="java" import="java.util.*,java.io.File" pageEncoding="GBK"%>
2 <%@ page import="org.apache.commons.fileupload.*" %>
3 <%
4 String path = request.getContextPath();
5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
6 %>
7
8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
9 <html>
10 <head>
11 <base href="<%=basePath%>">
12
13 <title>My JSP 'upload.jsp' starting page</title>
14
15 <meta http-equiv="pragma" content="no-cache">
16 <meta http-equiv="cache-control" content="no-cache">
17 <meta http-equiv="expires" content="0">
18 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
19 <meta http-equiv="description" content="This is my page">
20 <!--
21 <link rel="stylesheet" type="text/css" href="styles.css">
22 -->
23
24 </head>
25
26 <body>
27 <%
28
29 DiskFileUpload dfu = new DiskFileUpload();
30
31 //设置上传数据的最大数据为10M
32 dfu.setSizeMax(0xA00000);
33
34 dfu.setSizeThreshold(0x80000);
35
36 dfu.setRepositoryPath("E:\\fileupload");
37
38 List fileItems = dfu.parseRequest(request);
39 Iterator it = fileItems.iterator();
40 %>
41 <table cellpadding="3" border="1">
42 <%
43 while(it.hasNext())
44 {
45 FileItem item = (FileItem)it.next();
46 if(!item.isFormField())
47 {
48 String name = item.getName();
49 long size = item.getSize();
50 if((name == null || name.equals("")) && size == 0)
51 {
52 continue;
53 }
54 %>
55 <tr>
56 <td><%=item.getName() %></td>
57 <td><%=item.getSize() %></td>
58 </tr>
59 <%
60 File dir = new File("E:\\UploadFile");
61 int index = name.lastIndexOf(File.separator);
62 if(index > 0)
63 name = name.substring(index + 1,name.length());
64
65 File file = new File(dir,name);
66 item.write(file);
67 }
68 else
69 {
70 %>
71 <tr>
72 <td><%=item.getFieldName() %></td>
73 <td><%=item.getString() %></td>
74 </tr>
75 <%
76 }
77 }
78 %>
79 </table>
80 </body>
81 </html>




posted @ 2011-11-27 11:48  常伟华  阅读(351)  评论(0编辑  收藏  举报