icecoldheart 2010-05-11 10:45
浏览 214
已采纳

webwork的文件上传总是得不到文件对象

xml

<interceptor name="fileupload" class="com.opensymphony.webwork.interceptor.FileUploadInterceptor"/>
            
            <interceptor-stack name="uploadStack">
                <interceptor-ref name="fileupload"/>
                <interceptor-ref name="defaultStack"/>
             </interceptor-stack>
<package name="fileUpload" namespace="/fileUpload" extends="webwork-default">
        <default-interceptor-ref name="uploadStack" />
        <action name="fileUpload" class="com.casetrial.fileUpload.UploadAction">
            <result name="success">/pages/appealSituation/fileUploadIndex.jsp</result>
        </action>
    </package>

 action

public class UploadAction extends ActionSupport {
    private File[] files;             //多个文件对象数组
    private String[] filesFileName;   //文件对应的真实文件名  单个文件上传时不需要此属性定义 
                                      //多文件上传时 file.getName() 无效,取得不是真实的文件名,而是upload_xxx.tmp
    public String execute(){
        System.out.println(files==null);
        if (files != null){
            for (int i = 0; i < files.length; i++){
                File file = files[i];
                String fileName = filesFileName[i];
                System.out.print(" File list: ");
                System.out.print("   name:=" + file.getName() + " ");
                System.out.print("   fileName:=" + fileName + " ");
                System.out.print("   path:=" + file.getPath() + " ");
                System.out.print("  ");

                FileOutputStream outputStream = null;
                FileInputStream fileIn = null;
                ResourceBundle rb = ResourceBundle.getBundle("config");
                String fileDir = rb.getString("saveDir")+File.separator;
                String filePath = fileDir+fileName;
                
                File f = new File(fileDir);
                f.mkdirs();
                try {
                    outputStream = new FileOutputStream(filePath);
                    fileIn = new FileInputStream(file);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                byte[] buffer = new byte[1024];
                int len;
                try {
                    while((len=fileIn.read(buffer))>0){
                        outputStream.write(buffer,0,len);
                    }
                    fileIn.close();
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.out.println("uploadfile name="+fileName);
                
            }
        }else{
            System.out.println("file is null!");
        }
        return SUCCESS;
    }

    public String[] getFilesFileName(){
        return filesFileName;
    }

    public void setFilesFileName(String[] filesFileName){
        this.filesFileName = filesFileName;
    }

    public File[] getFiles(){
        return files;
    }

    public void setFiles(File[] files){
        this.files = files;
    }

    /**//**
     * 获取session对象
     * @return
     */
    public HttpSession getSession(){
        return getRequest().getSession();
    }

    /**//**
     * 获取request对象
     *
     * @return httpServletRequest
     */
    public HttpServletRequest getRequest(){
        return ServletActionContext.getRequest();
    }

    /**//**
     * 获取response对象
     *
     * @return HttpServletResponse
     */
    public HttpServletResponse getResponse(){
        return ServletActionContext.getResponse();
    }

    /**//**
     * 获取ServletContext对象
     *
     * @return ServletContext
     */
    public ServletContext getContext(){
        return ServletActionContext.getServletContext();
    }

    /**//**
     * 获取ActionContext
     *
     * @return
     */
    public ActionContext getActionContext(){
        return ActionContext.getContext();
    }

    /**//**
     * 获取参数map
     *
     * @return
     */
    public Map getParameterMap(){
        return getActionContext().getSession();
    }

    /**//**
     * 获取输入参数名字对应的值
     *
     * @param name
     * @return
     */
    public String getParameter(String name){
        return (String) getParameterMap().get(name);
    }
}

页面

<html>
    <head>
    </head>
    <script language="Javascript" charset="utf-8">
    function addAccessory(){
        var accessoryListObj = document.getElementById("accessoryList");
        var accObj = document.createElement("span");
        var spanId = (new Date()).getTime();
        var spanHTML = " <input name='files' type='file' /><input type='button' value='删除' onclick=deleteFile('" + spanId + "')><br>";

        accObj.setAttribute("id", spanId);
        accObj.setAttribute("width", "100px");
        accObj.innerHTML = spanHTML;
        accessoryListObj.appendChild(accObj);
    }

    function deleteFile(fileId){
        var accessoryListObj = document.getElementById("accessoryList");
        var accObj = document.getElementById(fileId);
        accessoryListObj.removeChild(accObj);
    }
</script>
    <body>
        <form action="../fileUpload/fileUpload.action"
            enctype="multipart/form-data" method="post">
            <table id="DataTable">
                <tbody>
                    <tr>
                        <td class="style2" colspan="4">
                            <strong>附件: </strong>
                            <span title="增加附件" onclick="addAccessory()"
                                style="cursor:pointer;color:blue;"><strong>[添加附件]</strong>
                            </span>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="4" style="width:100%;height:300px;">
                            <div id="accessoryList"
                                style="overflow-x:auto;overflow-y:auto;width:1000px;height:100%;">
                                &nbsp;
                                <input type="file" name="files"> 
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="4" style="width:100%;height:15px;" align="center">
                            <input type="Submit" value="提交">

                        </td>
                    </tr>
                </tbody>
            </table>
        </form>
    </body>
</html>

一运行总是会发现files对象为Null,就得不到所要上传的东西,大家看看是错在哪儿了,我看了半天找不出来

  • 写回答

4条回答 默认 最新

  • 拽拽的初行者 2010-05-11 10:58
    关注

    [code="xml"]

            <interceptor-stack name="uploadStack">   
                <interceptor-ref name="fileupload"/>   
                <interceptor-ref name="defaultStack"/>   
             </interceptor-stack>  [/code]
    

    [color=blue]
    [b]defaultStack中,fileupload这个Interceptor有。而你的配置,打乱了Interceptor的顺序,所以得不到值了。

    直接使用默认的defaultStack就可以了。改一下试试吧。[/b][/color]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大