common-fileupload组件是apache的一个开源项目之一,可以从下载。用该组件可实现一次上传一个或多个文件,并可限制文件大小。
实例:
1 upload.html
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
- <html>
- <head><title>文件上传</title></head>
- <body>
- <form action="upload.jsp" method="POST" enctype="Multipart/form-data">
- <table>
- <tr>
- <td>请选择要上传的文件:</td>
- <td><input type="file" name="file1" size="40"></td>
- </tr>
- <tr>
- <td>请输入文件的描述:</td>
- <td><input type="text" name="desc1" size="40"></td>
- </tr>
- <tr>
- <td>请选择要上传的文件:</td>
- <td><input type="file" name="file2" size="40"></td>
- </tr>
- <tr>
- <td>请输入文件的描述:</td>
- <td><input type="text" name="desc2" size="40"></td>
- </tr>
- <tr>
- <td><input type="reset" value="重填"></td>
- <td><input type="submit" value="上传"></td>
- </tr>
- </table>
- </form>
- </body>
- </html>
2 upload.jsp
- <%@ page contentType="text/html; charset=GBK"%>
- <%@ page import="java.util.List,java.util.Iterator"%>
- <%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
- <%@ page import="java.io.File,java.io.InputStream"%>
- <%@ page import="javax.naming.*,java.sql.*,javax.sql.DataSource" %>
- <%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
- <%@ page import="org.apache.commons.fileupload.FileItem" %>
- <html>
- <head>
- <title>upload</title>
- </head>
- <body>
- <%
- DiskFileItemFactory itemFactory = new DiskFileItemFactory();
- //设置内存缓冲区的阀值为512K
- itemFactory.setSizeThreshold(0x80000);
-
- File tempDir = new File("E:\\temp");
- if(!tempDir.exists())
- {
- tempDir.mkdir();
- }
- //设置临时存储文件的目录为E:\temp
- itemFactory.setRepository(tempDir);
-
- ServletFileUpload sfu=new ServletFileUpload(itemFactory);
- //设置上传文件的最大数据量为10M
- sfu.setSizeMax(0xA00000);
- //解析上传文件流,得到FileItem对象的列表
- List fileItems=sfu.parseRequest(request);
- Iterator it = fileItems.iterator();
-
- Context ctx=new InitialContext();
- DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/bookstore");
- Connection conn=ds.getConnection();
- PreparedStatement pstmt=conn.prepareStatement("insert into uploadfile(filename, filesize, data) values(?,?,?)");
-
- Statement stmt=conn.createStatement();
- //依次处理每个上传的文件
- while (it.hasNext())
- {
- //String sql = "insert into uploadfile(filename, filesize) values(";
- FileItem item = (FileItem) it.next();
- //判断是否是文件字段
- if (!item.isFormField())
- {
- String name = item.getName();
- System.out.println("name: " + name);
- //如果浏览器传送的文件名是全路径名,则取出文件名
- int index=name.lastIndexOf(File.separator);
- if(index>0)
- name=name.substring(index+1,name.length());
-
- long size = item.getSize();
- if((name==null || name.equals("")) && size==0)
- continue;
- pstmt.setString(1,name);
- pstmt.setInt(2,(int)size);
-
- InputStream is = item.getInputStream();
- pstmt.setBinaryStream(3,is,(int)size);
-
- pstmt.executeUpdate();
-
- is.close();
- }
- }
- if(pstmt!=null)
- {
- pstmt.close();
- pstmt=null;
- }
- if(conn!=null)
- {
- conn.close();
- conn=null;
- }
- out.println("上传成功!");
- %>
- </body>
- </html>
说明: 如果要往数据库中存储二进制大对象 可以利用PreparedStatement接口的setBinaryStream()方法
void setBinaryStream(int parameterIndex,InputStream x,int length) throws SQLException
第一个参数parameterIndex表示SQL语句中参数的索引 第二个参数x指定包含了二进制数据Java输入流,作为SQL语句中参数的值 第三个参数length指定输入流中包含的数据的字节数
3 存储上传文件内容的数据库表uploadfile
打开数据库
- mysql -uroot -proot bookstore
创建新表
- CREATE table uploadfile(
- id INT AUTO_INCREMENT PRIMARY key,
- filename varchar(255) not null,
- filesize INT not null,
- data mediumblob not null);
4 配置JDBC数据源
context.xml web.xml
5
阅读(1547) | 评论(0) | 转发(0) |