Struts中实现多文件上传

         现在在网络上有很多关于struts文件上传的文章,但是很多都是直接摘抄struts自带的例子,而且很多都如同一辙,关于多文件上传的更是少之又少。我这几天刚好要做一个多文件上传的DEMO,就自己写了一个多文件上传的例子,不好的地方请大家多多指教。

        actionForm采用map来对应上传页面,action采用STRUTS自带的FormFile来联系上传的文件。 我就直接按actionForm,JSP->struts_config->action的顺序来说,程序就列出关键部分,由于时间关系我就不做详细解释了。

         actionForm的程序:

public   class  MutiUploadForm  extends  ActionForm
{
    
protected Map    fileMap;

    
public MutiUploadForm()
    
{
        fileMap 
= new HashMap();
    }


    
public Map getFileMap()
    
{
        
return fileMap;
    }


    
public void setFileMap(Map fileMap)
    
{
        
this.fileMap = fileMap;
    }


    
public void setValue(String attributeKey, Object attributeValue)
    
{
        getFileMap().put(attributeKey, attributeValue);
    }


    
public Object getValue(String attributeKey)
    
{
        Object keyValue 
= getFileMap().get(attributeKey);

        
return keyValue;
    }


}
 

 

             JSP程序:             我就简单的列出了两个文件上传你可以用脚本程序实现文件输入框的自动增加,有时间我再给出这方面的代码。

< html:form  action ="mutiUpload_act.do"  enctype ="multipart/form-data" >

              
< html:file  property ="value(file01)"   />

              
< html:file  property ="value(file02)"   />

< html:submit  />

</ html:form >

 

             注意:(file02)中的value对应actionform中的setValue(...),是javabean的默认用法。

             struts-config.xml: 请只参考其中的关键部分:

< action  path ="/mutiUpload_act"  

type
="com.hong.upload.web.action.MutiUploadAction"  

name
="mutiUploadForm"  

scope
="request"  

input
="input" >

             
< forward  name ="input"  path ="/mutiUpload.jsp"   />

            
< forward  name ="display"  path ="/display.jsp"   />

</ action >

 

               Action的程序:

 

public  ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        
throws  Exception
{
    System.out.println(
"action.....");

    
if (form instanceof MutiUploadForm)
    
{
        
// 如果form是MutiUploadForm 

        String encoding 
= request.getCharacterEncoding();

        
if ((encoding != null&& (encoding.equalsIgnoreCase("utf-8")))
        
{
            response.setContentType(
"text/html;charset=gb2312");
        }


        MutiUploadForm theForm 
= (MutiUploadForm) form;

        
/****************第一个文件***********************/

        FormFile file1 
= (FormFile) theForm.getValue("file01");// 取得上传的文件 
        String size1 = (file1.getFileSize() + " bytes");// 文件大小 
        String fileName1 = file1.getFileName();// 文件名 

        
try
        
{
            InputStream stream 
= file1.getInputStream();// 把文件读入 

            
//String filePath = request.getPathInfo();// 取当前系统路径 

            String filePath 
= "e:/web/frameDemo";

            
//ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

            OutputStream bos 
= new FileOutputStream(filePath + "/"
                    
+ file1.getFileName());

            
// 建立一个上传文件的输出流,将上传文件存入web应用的根目录。 

            System.out.println(filePath 
+ "/" + file1.getFileName());

            
int bytesRead = 0;

            
byte[] buffer = new byte[8192];

            
while ((bytesRead = stream.read(buffer, 08192)) != -1)
            
{
                bos.write(buffer, 
0, bytesRead);// 将文件写入服务器
            }


            bos.close();
            stream.close();

        }

        
catch (Exception e)
        
{
            System.err.print(e);
        }


        request.setAttribute(
"size1", size1);
        request.setAttribute(
"fileName1", fileName1);

        
/*******************第二个文件********************/

        FormFile file2 
= (FormFile) theForm.getValue("file02");// 取得上传的文件 
        String size2 = (file2.getFileSize() + " bytes");// 文件大小 
        String fileName2 = file2.getFileName();// 文件名 

        
try
        
{
            InputStream stream 
= file2.getInputStream();// 把文件读入 

            
//String filePath = request.getPathInfo();// 取当前系统路径 

            String filePath 
= "e:/web/frameDemo";

            
//ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

            OutputStream bos 
= new FileOutputStream(filePath + "/"
                    
+ file2.getFileName());

            
// 建立一个上传文件的输出流,将上传文件存入web应用的根目录。 

            System.out.println(filePath 
+ "/" + file2.getFileName());

            
int bytesRead = 0;

            
byte[] buffer = new byte[8192];

            
while ((bytesRead = stream.read(buffer, 08192)) != -1)
            
{
                bos.write(buffer, 
0, bytesRead);// 将文件写入服务器 
            }


            bos.close();
            stream.close();

        }

        
catch (Exception e)
        
{
            System.err.print(e);
        }


        request.setAttribute(
"size1", size1);

        request.setAttribute(
"fileName1", fileName1);

        request.setAttribute(
"size2", size2);

        request.setAttribute(
"fileName2", fileName2);

        
return mapping.findForward("display");
    }
 else
    
{
        System.out.println(
"form is not instanceof MutiUploadForm!");
    }


    
return null;

}
 

 

为了简单,我没有用循环,在文件多的时候可以根据map的keySet来循环。 上面是程序的关键部分,因为时间关系写的比较粗略,希望对大家有所帮助。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值