登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

云之南

风声,雨声,读书声,声声入耳;家事,国事,天下事,事事关心

 
 
 

日志

 
 
关于我

专业背景:计算机科学 研究方向与兴趣: JavaEE-Web软件开发, 生物信息学, 数据挖掘与机器学习, 智能信息系统 目前工作: 基因组, 转录组, NGS高通量数据分析, 生物数据挖掘, 植物系统发育和比较进化基因组学

struts动态多文件上传实现  

2008-05-01 10:46:27|  分类: java-j2ee |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

JSP页面如下:

 

<script language="javascript" >

    function addFile(){

        var Rows=tableFiles.rows;//类似数组的Rows 

        var newRow=tableFiles.insertRow(tableFiles.rows.length);//插入新的一行 

         var Cells=newRow.cells;//类似数组的Cells 

        var newCell=Rows(newRow.rowIndex).insertCell(Cells.length); 

        newCell.align="center"; 

        newCell.innerHTML= "<input type=file name=uploadFile["+ (tableFiles.rows.length-1) + "].file value=''/>"

    }

    function delFile(){

        if (tableFiles.rows.length > 1){ 

            tableFiles.deleteRow(tableFiles.rows.length-1); 

        }

    }  

     

    function deleteOtherFiles(){

        while(tableFiles.rows.length > 1){ 

            tableFiles.deleteRow(tableFiles.rows.length-1); 

        }

    }

   function setAddDelBtnDisplay(){

           var objChkFolder = document.getElementById("chkUploadFolder");

           var objBtn = document.getElementById("RowAddDelBtn");

           if(objChkFolder.checked){

               objBtn.style.display="";

           }else{

            deleteOtherFiles();

               objBtn.style.display="none";

           }

  }

   

   function upload(){

     multiUploadForm.submit();

   }

</script>

<body bgcolor="#ffffff">

    <html:form method="post" action="/multiUploadAction.do" enctype="multipart/form-data">

        <table border="1">

            <TBODY>

            <tr>

                <td>

                    上传文件(<input name="chkUploadFolder" type="checkbox" onclick="setAddDelBtnDisplay();"/>文件夹)

                </td>

            </tr>

            <tr>

                <td>

                    <table id="tableFiles">

                        <nested:iterate id="uploadFile" property="myFiles"  name="multiUploadForm" indexId="index">

                            <nested:nest property="uploadFile">

                                <tr>

                                    <td>

                                        <nested:file property="file" name="uploadFile" indexed="true" />

                                    </td>

                                </tr>

                            </nested:nest>

                        </nested:iterate>

                    </table>

                </td>

            </tr>

            <TR id ="RowAddDelBtn" style="display: none;">

                <td align="center">

                    <table>

                        <tr>

                            <td>

                                文件夹名:<input type="text" name="folderName" size="10"/>

                            </td>

                            <td>

                                <input type="button" name="btnAddFile" value="Add File" onclick="addFile()" />

                            </td>

                            <td>

                                <input type="button" name="btnAddFile" value="Del File" onclick="delFile()" />

                            </td>

                        </tr>

                    </table>

                </td>

            </TR>

            <TR>

                <td>

                    <input type="button" name="btnUpload" value="upload" onclick="upload()">

                </td>

            </TR>

            </TBODY>

        </table>

    </html:form>

</body>

</html>

注意事项:

<html:form method="post" action="/multiUploadAction.do" enctype="multipart/form-data">中一定要加上 enctype="multipart/form-data"

否则,会出现类型不匹配的问题. 

//ACTIONFORM

package upload.multi;

import java.util.ArrayList;

import java.util.List;

import org.apache.struts.action.ActionForm;

public class MultiUploadForm extends ActionForm {

    private List myFiles;

// 上传的文件名
     private String uploadFileName;

 // 存入数据中的URL
     private String fileUrl;

    public MultiUploadForm() {

        myFiles = new ArrayList();

        //为了能够在页面初始显示一个file

        myFiles.add(new UploadFile());

    }

    public List getMyFiles() {

        return myFiles;

    }

public String getFileUrl() {
  return fileUrl;
 }

 public void setFileUrl(String fileUrl) {
  this.fileUrl = fileUrl;
 }

public String getUploadFileName() {
  return uploadFileName;
 }

 public void setUploadFileName(String uploadFileName) {
  this.uploadFileName = uploadFileName;
 }

    //注意这个方法的定义 

    public UploadFile getUploadFile(int index) {

        int size = myFiles.size();

        if (index > size - 1) {

            for (int i = 0; i < index - size + 1; i++) {

                myFiles.add(new UploadFile());

            }

        }

        return (UploadFile) myFiles.get(index);

    }

    public void setMyFiles(List myFiles) {

        this.myFiles = myFiles;

    }

}

 

 

//UploadFile类

package upload.multi;

import java.io.Serializable;

import org.apache.struts.upload.FormFile;

public class UploadFile implements Serializable {

    private FormFile file;

    public FormFile getFile() {

        System.out.println("run in uploadFile.getFile()");

        return file;

    }

    public void setFile(FormFile file) {

        this.file = file;

    }

}

//action类

package upload.multi;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.upload.FormFile;

public class MultiUploadAction extends Action {

    

    public ActionForward execute(ActionMapping mapping, ActionForm form,

                                 HttpServletRequest request,

                                 HttpServletResponse response) {

        MultiUploadForm multiUploadForm = (MultiUploadForm) form;

        List myFiles = multiUploadForm.getMyFiles();

String filePath = servlet.getServletContext().getRealPath(
    "/messageresponsefile/");//上传文件实际的存储的路径,文件内容存在硬盘上,而不是数据库中

        try{

           

            for(int i =0;i< P>

                UploadFile uploadFile = (UploadFile)myFiles.get(i);

                FormFile file = uploadFile.getFile(); 

    

                if(file==null){

                    System.out.println("file is null");

                }else{ 

    

                     String fileName = formfile.getFileName().trim();

                     uploadFile(file,filePath, fileName);//读取文件

               multiUploadForm.setUploadFileName(fileName);
                      multiUploadForm.setFileUrl("/messageresponsefile/"+ fileName);

                    //能运行到这里,就可以使用单个文件上传的方法进行上传了。循环而

//下面的代码是把ACTIONFORM中的数据传到MODEL或者说是"中间层"中的私有方法了,请自行定义

                               } 

    

        }catch(Exception e){

            e.printStackTrace();

        }

        return null;

    }

}  

//uploadFile方法,读取上传的文件

public  boolean uploadFile(FormFile file, String path, String name) {

  String fileName = name;
  File dir = new File(path);
  if (!dir.exists())
   dir.mkdir();
  //文件存在了,删除
  File f = new File(path + "/" + fileName);
  if (f.exists())
   f.delete();
  try {
   FileOutputStream out = new FileOutputStream(path + "/" + fileName);
   byte[] binaryBytes = new byte[file.getFileSize()];

   file.getInputStream().read(binaryBytes);
   out.write(binaryBytes);
   out.close();
   return true;

  } catch (IOException e) {
   e.printStackTrace();
   return false;
  }
 }

  评论这张
 
阅读(1286)| 评论(1)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018