上传Servlet的实现代码

 1 public void doGet(HttpServletRequest request, HttpServletResponse response)
 2    throws ServletException, IOException {
 3 
 4 String id = null;
 5    Affiche affiche = new Affiche();//自定义的一个简单javabean
 6    boolean isMultipart = ServletFileUpload.isMultipartContent(request);// 判断提交的表单是不是普通表单,不是的话就进行上传
 7    if (isMultipart) {// 不是普通表单的话就进行上传
 8     FileItemFactory factory = new DiskFileItemFactory();// 固定用法,创建一个FileItemFactory对象
 9     ServletFileUpload upload = new ServletFileUpload(factory);// 固定用法,创建一个ServletFileUpload对象
10     Iterator<FileItem> items;// 定义一个保存所有表单数据的集合
11     try {
12      items = upload.parseRequest(request).iterator();// 将表单数据全部赋值给items
13      while (items.hasNext()) {// while循环迭代items,得到所有的表单数据
14       FileItem item = items.next();
15       if (!item.isFormField()) {// 判断此表单数据是否是file类型没事的话  就进行上传
16        String name = item.getName();// 得到file文件的名字
17        String fileName = name.substring(name.lastIndexOf('\\') + 1, name.length());
18        fileName = System.currentTimeMillis() + "_" + fileName;// 生成保存file的文件名,利用当前时间的毫秒值加上源文件的后缀名
19        String path = request.getSession().getServletContext().getRealPath("upload")
20          + File.separatorChar + fileName;// 生成保存该文件的路径名
21        System.out.println(path);
22        File uploadedFile = new File(path);
23        item.write(uploadedFile);// 保存文件
24        affiche.setAfFilepath(path);
25       } else {// 如果不是file类型的表单,那么就进行简单的赋值操作,注意这里不能再简单的用reqeuest.getParameter("")得到页面传递的表单数据,因为全部封装到items这个集合了
26        if ("id".equals(item.getFieldName())) { //获取非上传参数
27         id = item.getString("gb2312");
28        }
29        if ("time".equals(item.getFieldName())) {//获取非上传参数
30 
31         affiche.setAfEnabletime(parseDate(item.getString("gb2312")));
32        }
33        if ("title".equals(item.getFieldName())) {//获取非上传参数
34 
35         affiche.setAfTitle(item.getString("gb2312"));
36        }
37        if ("content".equals(item.getFieldName())) {//获取非上传参数
38 
39         affiche.setAfContent(item.getString("gb2312"));
40        }
41       }
42      }
43     } catch (Exception e) {
44      e.printStackTrace();
45     }
46    }
47 
48 }
49 
50 //转换日期的小函数
51 
52 private Date parseDate(String str) {
53   SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd");
54   Date date = new Date();
55   try {
56    date = dd.parse(str);
57   } catch (ParseException e) {
58   }
59   return date;
60  }
JSP页面实现代码
 1 <form action="afficheManager?act=save" method="post" enctype="multipart/form-data">
 2 
 3     <table cellspacing="0" cellpadding="1" border="1" width="70%"
 4      style="border: 1px solid #FF34B3">
 5      <tr>
 6       <td width="10%" style="border: 1px solid #FF34B3">
 7        公告标题:
 8       </td>
 9       <td align="left" style="border: 1px solid #FF34B3">
10        <input id="title" type="text" name="title" size="50">
11       </td>
12      </tr>
13      <tr>
14       <td width="10%" style="border: 1px solid #FF34B3">
15        公告内容:
16       </td>
17       <td align="left" style="border: 1px solid #FF34B3">
18        <textarea id="content" name="content" rows="10" cols="80"></textarea>
19      </tr>
20      <tr>
21       <td width="10%" style="border: 1px solid #FF34B3">
22        有效时间:
23       </td>
24       <td align="left" style="border: 1px solid #FF34B3">
25        <input type="text" name="time" size="30">
26        (YYYY-MM-DD)
27       </td>
28      </tr>
29      <tr>
30       <td width="10%" style="border: 1px solid #FF34B3">
31        相关文件:
32       </td>
33       <td align="left" style="border: 1px solid #FF34B3">
34        <input type="file" name="file" size="40" >
35       </td>
36      </tr>
37      <tr>
38       <td align="center" colspan="2" style="border: 1px solid #FF34B3">
39        <input type="submit" value=" 添加 ">       &nbsp; | &nbsp;
40        <input type="button" onclick="javascript:history.go(-1)" value=" 返回 ">
41       </td>
42      </tr>
43     </table>
44    </form>
Affiche JavaBean实现代码:
 1 package com.wxl.po;
 2 
 3 import java.util.Date;
 4 
 5 public class Affiche {
 6  private int afId;
 7  private String dpId;
 8  private String afTitle;
 9  private String afContent;
10  private Date afTime;
11  private Date afEnabletime;
12  private String afFilepath;
13 
14  //..一些get..set方法
15 
16 } 

 



posted on 2012-08-23 15:44  云 娜Blog  阅读(949)  评论(0编辑  收藏  举报