posts - 431,  comments - 344,  trackbacks - 0

     <form-bean name="fileUploadForm" type="org.apache.struts.action.DynaActionForm">
       <form-property name="fileName" type="Java.lang.String"></form-property>
       <form-property name="docfile" type="org.apache.struts.upload.FormFile"/>
       <form-property name="fileDescription" type="Java.lang.String"></form-property>
    </form-bean>


<form name="form1" method="post" action="<%=request.getContextPath()%>/fileUpload.do?method=upload" enctype="multipart/form-data">
  <table width="43%" border="1" align="center">
    <tr>
      <td colspan="2"><div align="center">上传周报</div></td>
    </tr>
    <tr>
      <td width="22%">文件名称</td>
      <td width="78%"><input type="text" name="fileName"></td>
    </tr>
    <tr>
      <td width="22%">选择文件</td>
      <td width="78%"><input type="file" name="docfile"/></td>
    </tr>
    <tr>
      <td width="22%">文件描述</td>
      <td><input type="textarea" name="fileDescription"/></td>
    </tr>
    <tr>
      <td colspan="2" align="center"><input type="submit" name="Submit" value="上传"><input type="reset" name="Submit2" value="重置"></td>
    </tr>
  </table>


public ActionForward upload(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
  request.setCharacterEncoding("UTF-8");
  DynaActionForm dynaform = (DynaActionForm) form;
  FormFile docfile = (FormFile) dynaform.get("docfile");
  String fileName=dynaform.get("fileName").toString();
  String fileDescription=(String) dynaform.get("fileDescription");
  if (docfile == null) {
   return mapping.getInputForward();
  }
  if (docfile.getFileSize() == 0) {
   request.getSession().setAttribute("message", "未选中文件或文件大小为零!");
   return mapping.getInputForward();
  }
  String ctype = docfile.getContentType();
  if (ctype.indexOf("doc") != -1) {
   request.getSession().setAttribute("message", "文件类型错误!");
   return mapping.getInputForward();
  }
  // 写文件
  BufferedInputStream bis = null;
  BufferedOutputStream bos = null;
  InputStream is = null;
  OutputStream fos = null;

  String filename = "";
  String filePath = "";
  try {
   is = (InputStream) docfile.getInputStream();// 把文件读入
   bis = new BufferedInputStream(is);

   filePath ="E:/upload/";// 取当前系统路径
   File rootfile = new File(filePath);
   if (!rootfile.exists()) {
    rootfile.mkdirs();
   }
   String name= new String(docfile.getFileName().getBytes("UTF-8"),"gb2312");
   filename = new Date().getTime()+request.getSession().getId()+fileName+".doc";
  fos = new FileOutputStream(filePath + filename);// 建立一个上传文件的输出流
   bos = new BufferedOutputStream(fos);
   int bytesRead = 0;
   byte[] buffer = new byte[2 * 1024];
   while ((bytesRead = bis.read(buffer)) != -1) {
    bos.write(buffer, 0, bytesRead);// 将文件写入服务器
   }
   FileUpload fileUpload=new FileUpload();
   fileUpload.setFileName(fileName);
   fileUpload.setFilePath(filePath+filename);
   fileUpload.setFileDescription(fileDescription);
   this.getSiteBusiness().getFileLoadService().save(fileUpload);
   return mapping.findForward("uploadsucess");
  }catch (Exception e) {
   e.printStackTrace();
   return mapping.getInputForward();
  } finally {
   if (bos != null) {
    try {
     bos.close();
    } catch (IOException e) {
     System.err.print(e);
     return mapping.getInputForward();
    }
   }
   if (bis != null) {
    try {
     bis.close();
    } catch (IOException e) {
     System.err.print(e);
     return mapping.getInputForward();
    }
   }
  }

 }

posted on 2007-01-30 11:40 周锐 阅读(534) 评论(2)  编辑  收藏 所属分类: Struts

只有注册用户登录后才能发表评论。


网站导航: