Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2321065
  • 博文数量: 252
  • 博客积分: 5472
  • 博客等级: 大校
  • 技术积分: 3107
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-17 18:39
文章分类

全部博文(252)

文章存档

2012年(96)

2011年(156)

分类: 系统运维

2011-12-17 16:27:51

common-fileupload组件是apache的一个开源项目之一,可以从下载。用该组件可实现一次上传一个或多个文件,并可限制文件大小。
 
实例:
 
1 upload.html
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
  2. <html>
  3.     <head><title>文件上传</title></head>
  4.     <body>
  5.         <form action="upload.jsp" method="POST" enctype="Multipart/form-data">
  6.             <table>
  7.                 <tr>
  8.                     <td>请选择要上传的文件:</td>
  9.                     <td><input type="file" name="file1" size="40"></td>
  10.                 </tr>
  11.                 <tr>
  12.                     <td>请输入文件的描述:</td>
  13.                     <td><input type="text" name="desc1" size="40"></td>
  14.                 </tr>
  15.                 <tr>
  16.                     <td>请选择要上传的文件:</td>
  17.                     <td><input type="file" name="file2" size="40"></td>
  18.                 </tr>
  19.                 <tr>
  20.                     <td>请输入文件的描述:</td>
  21.                     <td><input type="text" name="desc2" size="40"></td>
  22.                 </tr>
  23.                 <tr>
  24.                     <td><input type="reset" value="重填"></td>
  25.                     <td><input type="submit" value="上传"></td>
  26.                 </tr>
  27.             </table>
  28.         </form>
  29.     </body>
  30. </html>

 

2  upload.jsp

 

  1. <%@ page contentType="text/html; charset=GBK"%>
  2. <%@ page import="java.util.List,java.util.Iterator"%>
  3. <%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
  4. <%@ page import="java.io.File,java.io.InputStream"%>
  5. <%@ page import="javax.naming.*,java.sql.*,javax.sql.DataSource" %>
  6. <%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
  7. <%@ page import="org.apache.commons.fileupload.FileItem" %>

  8. <html>
  9.     <head>
  10.         <title>upload</title>
  11.     </head>
  12.     <body>
  13.     <%
  14.         DiskFileItemFactory itemFactory = new DiskFileItemFactory();
  15.         //设置内存缓冲区的阀值为512K

  16.         itemFactory.setSizeThreshold(0x80000);

  17.         
  18.         File tempDir = new File("E:\\temp");
  19.         if(!tempDir.exists())
  20.         {
  21.             tempDir.mkdir();
  22.         }
  23.         //设置临时存储文件的目录为E:\temp

  24.         itemFactory.setRepository(tempDir);
  25.         
  26.         ServletFileUpload sfu=new ServletFileUpload(itemFactory);
  27.         //设置上传文件的最大数据量为10M

  28.         sfu.setSizeMax(0xA00000);

  29.         //解析上传文件流,得到FileItem对象的列表

  30.         List fileItems=sfu.parseRequest(request);
  31.         Iterator it = fileItems.iterator();
  32.         
  33.         Context ctx=new InitialContext();
  34.         DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/bookstore");
  35.         Connection conn=ds.getConnection();
  36.         PreparedStatement pstmt=conn.prepareStatement("insert into uploadfile(filename, filesize, data) values(?,?,?)");
  37.         
  38.         Statement stmt=conn.createStatement();

  39.         //依次处理每个上传的文件

  40.         while (it.hasNext())
  41.         {
  42.             //String sql = "insert into uploadfile(filename, filesize) values(";

  43.             FileItem item = (FileItem) it.next();
  44.             //判断是否是文件字段

  45.             if (!item.isFormField())
  46.             {
  47.                 String name = item.getName();
  48.                 System.out.println("name: " + name);
  49.                 //如果浏览器传送的文件名是全路径名,则取出文件名

  50.                 int index=name.lastIndexOf(File.separator);
  51.                 if(index>0)
  52.                     name=name.substring(index+1,name.length());
  53.                     
  54.                 long size = item.getSize();
  55.                 if((name==null || name.equals("")) && size==0)
  56.                     continue;
  57.                 pstmt.setString(1,name);
  58.                 pstmt.setInt(2,(int)size);
  59.                 
  60.                 InputStream is = item.getInputStream();
  61.                 pstmt.setBinaryStream(3,is,(int)size);
  62.                 
  63.                 pstmt.executeUpdate();
  64.                 
  65.                 is.close();
  66.             }
  67.         }
  68.         if(pstmt!=null)
  69.         {
  70.             pstmt.close();
  71.             pstmt=null;
  72.         }
  73.         if(conn!=null)
  74.         {
  75.             conn.close();
  76.             conn=null;
  77.         }
  78.         out.println("上传成功!");
  79.     %>

  80.     </body>
  81. </html>

说明: 如果要往数据库中存储二进制大对象 可以利用PreparedStatement接口的setBinaryStream()方法

void setBinaryStream(int parameterIndex,InputStream x,int length) throws SQLException

第一个参数parameterIndex表示SQL语句中参数的索引 第二个参数x指定包含了二进制数据Java输入流,作为SQL语句中参数的值  第三个参数length指定输入流中包含的数据的字节数

3  存储上传文件内容的数据库表uploadfile

打开数据库

  1. mysql -uroot -proot bookstore

创建新表

  1. CREATE table uploadfile(
  2. id INT AUTO_INCREMENT PRIMARY key,
  3. filename varchar(255) not null,
  4. filesize INT not null,
  5. data mediumblob not null);

4  配置JDBC数据源

  context.xml    web.xml

 

阅读(1547) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~