• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
腾飞的菜鸟
博客园    首页    新随笔    联系   管理    订阅  订阅

struts2批量文件上传

xml文件的配置

<package name="/file" namespace="/file" extends="struts-default" >
       <action name="upload" class="org.web.action.UploadAction" method="Upload">
         <param name="savePath">/images</param>
            <result name="success">/public.jsp</result>
            <result name="input">/file.jsp</result>
            <interceptor-ref name="fileUpload">
                <!-- 文件过滤 -->
                <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param>
                <!-- 文件大小, 以字节为单位 -->
                <param name="maximumSize">1025956</param>
            </interceptor-ref>
            <!-- 默认拦截器必须放在fileUpload之后,否则无效 -->
            <interceptor-ref name="defaultStack" />
       </action>
   </package>

 

处理文件上传操作的action代码:

package org.web.action;

import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {   private String savePath; private List<File> file;

private List<String> fileFileName;

private List<String> fileContentType;

public String getSavePath() {  return ServletActionContext.getServletContext().getRealPath(savePath); }

public void setSavePath(String savePath) {  this.savePath = savePath; }

public List<File> getFile() { return file; }

public void setFile(List<File> file) { this.file = file; }

public List<String> getFileContentType() { return fileContentType; }

public void setFileContentType(List<String> fileContentType) { this.fileContentType = fileContentType; }

public List<String> getFileFileName() { return fileFileName; }

public void setFileFileName(List<String> fileFileName) { this.fileFileName = fileFileName; }

/** * 动态上传文件 * @return * @throws Exception */

public String Upload() throws Exception {

    for(int i=0;i<file.size();i++){             

   FileOutputStream fos = null;             

FileInputStream fis = null;          

    try {                  // 建立文件输出流      

            System.out.println("savePath===>"+getSavePath());       

           fos = new FileOutputStream(getSavePath() + "\\" + getFileFileName().get(i));          

        System.out.println("filename====>"+getFileFileName().get(i));         

         // 建立文件上传流           

       fis = new FileInputStream(getFile().get(i));    

             byte[] buffer = new byte[1024];            

        int len = 0;             

     while ((len = fis.read(buffer)) > 0) {           

           fos.write(buffer, 0, len);                  }         

     } catch (Exception e) {        

          System.out.println("文件上传失败");            

      e.printStackTrace();         

     } finally {                  }      }   

       return SUCCESS;  

}

}

 

上传的前台界面:

 <%@ page language="java" import="java.util.*" pageEncoding="gbk"%> <%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html>   <head>     <title>My JSP 'upload.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script type="text/javascript"> function addMore(){ var td=document.getElementById("more");

var br=document.createElement("br"); var input=document.createElement("input"); var button=document.createElement("input");

input.type="file"; input.name="file";

button.type="button"; button.value="-";

button.onclick=function(){   td.removeChild(br);   td.removeChild(input);   td.removeChild(button);   }  td.appendChild(br);  td.appendChild(input);  td.appendChild(button); } </script>

  </head>     <body>    <form action="${pageContext.request.contextPath}/file/upload.action"               enctype="multipart/form-data" method="post">    <table align="center" width="60%" border="1">    <tr>    <td>    选择上传的文件:    </td>    <td id="more">    <input type="file" name="file">    <input type="button" value="+" onclick="addMore()"/>    </td>    </tr>    <tr>    <td>    </td>    <td>    <s:submit value="上传" align="center"></s:submit>    </td>    </tr>       </table>    </form>   </body> </html>

posted @ 2013-11-21 14:18  腾飞的菜鸟  阅读(233)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3