struts实现上传篇

做了一个上传下载的程序,还有很多地方需要改进的地方,还是先贴上来吧,以后在用到的时候做个参照:

上传:用到struts提供的FormFile类,以及html:file标签.在设计上传页面的时候有多个上传浏览框.我这回用了一个土方法在页面上进行了循环赋值,所以在ActionForm中FormFile定义成数组,不知道还有没有更好的方法.

java 代码
  1. public class UploadAction extends Action {   
  2.   
  3. public ActionForward execute(ActionMapping mapping, ActionForm form,   
  4.    HttpServletRequest request, HttpServletResponse response)   
  5.    throws Exception {   
  6.   
  7.   int totalFileSize = 0;   
  8.   List list = new ArrayList();   
  9.   // 在web.xml中读取系统上传默认路径   
  10.   String path = servlet.getInitParameter("file-upload");   
  11.   // 获取页面信息,得到上传文件的相关信息.   
  12.   UploadForm upform = (UploadForm) form;   
  13.   // FormFile是struts提供的处理上传的类   
  14.   FormFile[] file = upform.getFile();   
  15.   for (int i = 0; i < file.length; i++) {   
  16.    // 取得上传文件的文件名.   
  17.    String fileName = file[i].getFileName();   
  18.    // 取得上传文件的大小,转成字符串类型的目的是为了在页面上显示文件大小.   
  19.    String size = file[i].getFileSize()+"";   
  20.       
  21.    totalFileSize += file[i].getFileSize();   
  22.    if (fileName.equals("") || fileName.equals(null)) {   
  23.     request.setAttribute("total",totalFileSize);   
  24.     request.setAttribute("filearraylist",list);   
  25.     return mapping.findForward("success");   
  26.    } else {   
  27.     // 主要方法,上传文件   
  28.     uploadFile(file[i], path, fileName, size, upform, request);   
  29.   
  30.     FileInfo fi = new FileInfo();   
  31.     fi.setFileName(fileName);   
  32.     fi.setFileSize(size);   
  33.        
  34.     list.add(fi);   
  35.   
  36.     // 上传成功后弹出上传成功对话框,在这里加一个标志,在页面进行判断用.   
  37.     request.setAttribute("flags"true);   
  38.   
  39.     // 取得文件上传路径   
  40.     File fileList = new File(path);   
  41.     // 取得所有文件名   
  42.     String[] files = fileList.list();   
  43.     // 将文件名放入request范围在页面显示用.   
  44.     request.setAttribute("fileList", files);   
  45.    }   
  46.   }   
  47.   request.setAttribute("total",totalFileSize);   
  48.   request.setAttribute("filearraylist",list);   
  49.   return mapping.findForward("success");   
  50.  }   
  51.   
  52.  private void uploadFile(FormFile file, String path, String fileName,   
  53.    String size, UploadForm upform, HttpServletRequest request) {   
  54.   try {   
  55.    InputStream is = file.getInputStream();   
  56.    OutputStream os = new FileOutputStream(path + "/" + fileName);   
  57.    // 文件大小   
  58.    byte[] buffer = new byte[20480];   
  59.    while ((is.read(buffer, 0, buffer.length)) != -1) {   
  60.     os.write(buffer, 0, buffer.length);   
  61.    }   
  62.    os.flush();   
  63.    file.destroy();   
  64.    // 关闭流   
  65.    close(is, os);   
  66.   } catch (FileNotFoundException e) {   
  67.    e.printStackTrace();   
  68.   } catch (IOException e) {   
  69.    e.printStackTrace();   
  70.   }   
  71.  }   
  72.   
  73.  private void close(InputStream is, OutputStream os) throws IOException {   
  74.   is.close();   
  75.   os.close();   
  76.  }   
  77.   
  78. }   
  79.   
  80. public class UploadForm extends ActionForm {   
  81.   
  82.  private FormFile[] file = new FormFile[4];   
  83.   
  84.  private String fileName;   
  85.   
  86.  private String size;   
  87.   
  88.  public FormFile[] getFile() {   
  89.   return file;   
  90.  }   
  91.   
  92.  public void setFile(FormFile[] file) {   
  93.   this.file = file;   
  94.  }   
  95.   
  96.  public String getFileName() {   
  97.   return fileName;   
  98.  }   
  99.   
  100.  public void setFileName(String fileName) {   
  101.   this.fileName = fileName;   
  102.  }   
  103.   
  104.  public String getSize() {   
  105.   return size;   
  106.  }   
  107.   
  108.  public void setSize(String size) {   
  109.   this.size = size;   
  110.  }   
  111.   
  112. }   
  113.   

 upload页面:

jsp 代码
  1. <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>  
  2. <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>  
  3. <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>  
  4.   
  5. <%@ page language="java" contentType="text/html; charset=GB2312"%>  
  6.   
  7. <logic:present name="flags">  
  8. <script language="javascript" >alert("上传成功!")</script>  
  9. </logic:present>  
  10. <html:html>  
  11. <head>  
  12.     <title>test struts upload file</title>  
  13. </head>  
  14. <body>  
  15. <html:errors/>  
  16. <html:messages id="messages" message="true">  
  17.  <LI><bean:write name="messages" /></LI>  
  18. </html:messages>  
  19. <html:form action="upload" enctype="multipart/form-data">  
  20.  <table cellpadding="0" cellspacing="0" border="0" >  
  21.   <tr>  
  22.    <td>  
  23.     <table height="80%" width="80%">  
  24.      <tr>  
  25.       <td bgcolor="#ddcc">  
  26.        选择文件   
  27.       </td>  
  28.      </tr>  
  29.     </table>  
  30.    </td>  
  31.   </tr>  
  32.   <tr height="50">  
  33.    <td width=2%>  
  34.     <table height="80%" width="80%" cellpadding="0" cellspacing="0" border="0">  
  35.      <tr>  
  36.       <% for(int i=0;i<4;i++){%>  
  37.       <td>  
  38.        <html:file property='<%="file["+i+"]"%>' />  
  39.       </td>  
  40.       <%   
  41.       }   
  42.       %>  
  43.      </tr>  
  44.     </table>  
  45.    </td>  
  46.   </tr>  
  47.   <tr>  
  48.    <td>  
  49.     <html:submit onclick="return(confirm('你确认要上传文件吗?'))">上传文件</html:submit>  
  50.    </td>  
  51.   </tr>  
  52.   <tr>  
  53.    <td>  
  54.     <table height="80%" width="80%">  
  55.      <tr bgcolor="#ddcc">    
  56.       <td width="50%">文件名</td>  
  57.       <td width="50%">文件大小(byte)</td>  
  58.      </tr>  
  59.      <tr>  
  60.      <logic:empty name="filearraylist" scope="request">  
  61.       <tr><td width="50%">总计</td><td width="50%">0</td></tr>  
  62.      </logic:empty>  
  63.      <logic:notEmpty name="filearraylist" scope="request">  
  64.      <logic:iterate id="element" name="filearraylist" scope="request">  
  65.         <tr>  
  66.         <td width="50%"><bean:write name="element" property="fileName" /></td>  
  67.         <td width="50%"><bean:write name="element" property="fileSize" /></td>  
  68.         </tr>  
  69.      </logic:iterate>  
  70.      <tr><td>总计</td><td><bean:write name="total"/></td></tr>  
  71.      </logic:notEmpty>  
  72.      </tr>  
  73.     </table>  
  74.    </td>  
  75.   </tr>  
  76.  </table>  
  77. </html:form>  
  78. <br/><br/><br/>  
  79. <%int i=0;%>  
  80. <logic:notEmpty name="fileList" scope="request">  
  81. <table cellpadding="0" cellspacing="0" border="0">  
  82.  <tr>  
  83.   <td>  
  84.     <logic:iterate id="element" name="fileList" scope="request">  
  85.     <%= ++i%>  
  86.     <a href="<html:rewrite page='/downLoad.do' />?filename=<bean:write name='element' />"><bean:write name="element" /></a><br/>  
  87.     </logic:iterate>  
  88.   </td>  
  89.  </tr>  
  90. </table>  
  91. </logic:notEmpty>  
  92. </body>  
  93. </html:html>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值