Struts2上传文件再分解

本文介绍了一个基于Struts2框架实现文件上传的方法,并利用Flex进行前端展示。该方法适用于需要通过Struts2框架处理文件上传的企业级应用。文章详细展示了如何在Struts2中解析由Flex前端发送过来的多部分请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

因公司开发项目要做Struts2上传文件,前台是通过flex实现所以再现有代码基础上又深入改进了一下,先转一下别人的代码,我们的回头二次改造好后在放上来。

public void preExecute()  
    {  
        if (httpServletRequest instanceof MultiPartRequestWrapper)  
        {  
            MultiPartRequestWrapper requestWrapper = (MultiPartRequestWrapper) httpServletRequest;  
            Enumeration<String> parameterNames = requestWrapper.getFileParameterNames();  
            while (parameterNames.hasMoreElements())  
            {  
                @SuppressWarnings("unused")  
                String paramterName = parameterNames.nextElement();  
                File[] files = requestWrapper.getFiles(paramterName);  
                if (null == files || files.length == 0)  
                {  
                    continue;  
                }  
                else if (files.length == 1)  
                {  
                    FileBean fileBean = new FileBean();  
                    fileBean.setAbsolutePath(files[0].getAbsolutePath());  
                    fileBean.setFileSize(files[0].length());  
                    fileBean.setFilename(requestWrapper.getFileNames(paramterName)[0]);  
                    this.fileMap.put(paramterName, fileBean);  
                }  
                else  
                {  
                    ArrayList<FileBean> list = new ArrayList();  
                    for (int i = 0; i < files.length; i++)  
                    {  
                        File file = files[i];  
                        FileBean fileBean = new FileBean();  
                        fileBean.setAbsolutePath(file.getAbsolutePath());  
                        fileBean.setFileSize(file.length());  
                        fileBean.setFilename(requestWrapper.getFileNames(paramterName)[i]);  
                        list.add(fileBean);  
                    }  
                    this.fileMap.put(paramterName, list);  
                }  
            }  
        }  
    } 
 转自 http://sdh88hf.iteye.com/?show_full=true
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值