common-fileupload-1.1.1 && struts1.3.8 单个文件上传

1. JSP

  1. <%@ page contentType="text/html;charset=GBK"%>
  2. <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
  3. <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
  4. <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
  5. <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
  6. <html:html>
  7.     <head>
  8.         <title>User List Page</title>
  9.     </head>
  10.     <body bgcolor="#99ffff">
  11.         <center><br><br><br>
  12.         <logic:present name="user" scope="session">
  13.             <html:form action="user.do" method="post" enctype="multipart/form-data">
  14.                 <table border="2" bgcolor="#ffff99">
  15.                     <tr>
  16.                         <td>Upload: </td>
  17.                         <td><html:file property="upDoc" /></td>
  18.                         <td><html:text property="upDocName" /></td>
  19.                     </tr>
  20.                     <tr>
  21.                         <td colspan="3" align="center">
  22.                             <html:hidden property="status" value="upload" />
  23.                             <html:submit value="Upload" />
  24.                             <html:reset value="RESET" />
  25.                         </td>
  26.                     </tr>
  27.                 </table>
  28.             </html:form>
  29.         </logic:present>
  30.         </center>
  31.     </body>
  32. </html:html>

2. ActionForm

  1. private FormFile upDoc; //接收类型为org.apache.struts.upload.FormFile. 这个要特别关注, 因为批量上传时不能用这个类型了
  2. private String upDocName;
  3. public FormFile getUpDoc() {
  4.     return upDoc;
  5. }
  6. public void setUpDoc(FormFile upDoc) {
  7.     this.upDoc = upDoc;
  8. }
  9. public String getUpDocName() {
  10.     return upDocName;
  11. }
  12. public void setUpDocName(String upDocName) {
  13.     this.upDocName = upDocName;
  14. }

3. UploadAction

  1. public ActionForward upload(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) {
  2.     userForm = (UserForm) form;
  3.     String upDocName = userForm.getUpDocName();
  4.     FormFile formFile = userForm.getUpDoc();
  5.     
  6.     OutputStream out = null;
  7.     String uploadPath = null;
  8.     
  9.     // 在编写IO流的时候必须有一个明确的绝对路径 --> this.getServlet().getServletContext().getRealPath("/")取得了当前项目的绝对路径。
  10.     /* 
  11.      * split()方法:public String[] split(String regex[, int limit]);
  12.      * eg:str.split(regex); --> 返回一个下标从0开始的数组,regex不作为任何数组元素的部分返回。
  13.      * 因为split()的参数regex是正则表达式, 所以特殊字符必须转义:
  14.      *  1、如果用"."作为分隔的话,必须是如下写法:String.split("//."),这样才能正确的分隔开,不能用String.split("."); 
  15.      *  2、如果用"|"作为分隔的话,必须是如下写法:String.split("//|"),这样才能正确的分隔开,不能用String.split("|"); 
  16.      *  3、如果用"/"作为分隔,就得写成这样:String.split(""),因为在Java中是用"//"来表示"/"的, "."、"|"和"/"都是转义字符,必须得加"//"; 
  17.      *  4、如果在一个字符串中有多个分隔符,可以用"|"作为连字符,比如:"acount=? and uu =? or n=?",把三个都分隔出来,可以用String.split("and|or");
  18.      * limit可选,用来限制返回数组中的元素个数。
  19.      */
  20.     
  21.     if ("".equals(upDocName)) {
  22.         //不修改上传文件名时uploadPath的值
  23.         uploadPath = this.getServlet().getServletContext().getRealPath("/") + "upload//" + userForm.getUpDoc().getFileName();
  24.     } else {
  25.         uploadPath = this.getServlet().getServletContext().getRealPath("/") + "upload//" + 
  26.                         userForm.getUpDocName() + "." + userForm.getUpDoc().getFileName().split("//.")[1];
  27.     }
  28.     
  29.     try {
  30.         byte b[] = formFile.getFileData();
  31.         out = new FileOutputStream(new File(uploadPath));
  32.         out.write(b);
  33.     } catch (FileNotFoundException fnfe) {
  34.         System.err.println(this.getClass().getName() + ": upload: failed --> " + fnfe.toString());
  35.     } catch (IOException ioe) {
  36.         System.err.println(this.getClass().getName() + ": upload: failed --> " + ioe.toString());
  37.     } finally {
  38.         try {
  39.             out.close();
  40.         } catch (IOException ioe) {
  41.             System.err.println(this.getClass().getName() + ": upload - out.close() failed --> " + ioe.toString());
  42.         }
  43.     }
  44.     return mapping.findForward("upload");
  45. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值