本人要实现文件的上传与下载。
在前台使用了Extjs,而action继承的是DispatchAction,
执行到这行代码:
smartupload.initialize(this.getServlet().getServletConfig(),request,response);
就出问题了。到底是什么原因?
哪位高手有相关成功的例子,麻烦提供一下,小弟拜谢了!

解决方案 »

  1.   

    http://blog.csdn.net/jinchun1234/archive/2009/04/11/4064169.aspx
      

  2.   

     前台js代码:var form = new Ext.form.FormPanel({   
            baseCls: 'x-plain',   
            labelWidth: 80,     
            url:'courseware.do?actionType=upload_courseware',   
                fileUpload:true,   
            defaultType: 'textfield',   
      
            items: [{   
                xtype: 'textfield',   
                fieldLabel: 'File Name',   
                name: 'userfile',   
                inputType: 'file',   
                allowBlank: false,   
                blankText: 'File can\'t not empty.',   
                anchor: '90%'  // anchor width by percentage   
            }, {
    xtype : 'combo',
    name : 'chapter',
    fieldLabel : '所属章节',
    width : 80,
    store : new Ext.data.SimpleStore({
    fields : ['chapter'],
    data : [['第二章'], ['第三章'], ['第四章'], ['第五章'], ['第六章'], ['第七章'], ['第八章'], ['第九章'], ['第十章'], ['第十一章'], ['第十二章']]
    }),
    displayField : 'chapter',
    mode : 'local',
    forceSelection : true,
    editable : false,
    triggerAction : 'all',
    value:'第一章'
    }]   
        });   
      
        var win = new Ext.Window({   
            title: 'Upload file',   
            width: 400,   
            height:200,   
            minWidth: 300,   
            minHeight: 100,   
            layout: 'fit',   
            plain:true,   
            bodyStyle:'padding:5px;',   
            buttonAlign:'center',   
            items: form,   
      
            buttons: [{   
                text: 'Upload',   
                handler: function() {   
                    if(form.form.isValid()){   
                        Ext.MessageBox.show({   
                               msg: 'Uploading...',   
                               width:300,   
                               progress:true,   
                               animEl: 'loding'  
                           });   
                        form.getForm().submit({       
                            success: function(form, action){   
                              Ext.Msg.alert("系统消息", action.result.msg, function() {
    this.closeWin();
    courseware1Store.reload();
    }, this);   
                            },       
                           failure: function(){       
                              Ext.Msg.alert('Error', 'File upload failure.');       
                           }   
                        })                
                    }   
               }   
            },{   
                text: 'Close',   
                handler:function(){win.hide();}   
            }]   
        });   
        win.show();action中的代码:public ActionForward upload_courseware(ActionMapping mapping, ActionForm form,
      HttpServletRequest request, HttpServletResponse response)
      throws Exception {
     PrintWriter out=response.getWriter();
     System.out.println(0);
     SmartUpload smartupload=new SmartUpload();
     System.out.println(1);
         String fileName2=null;
         smartupload.initialize(this.getServlet().getServletConfig(),request,response);
         System.out.println(2);
         try{
          smartupload.setMaxFileSize(100*1024*1024);
          smartupload.setAllowedFilesList("ppt,doc,xls,rar");
          smartupload.upload();       
          System.out.println(3);
         }catch(Exception ex)
         {  
          ex.printStackTrace();
          out.print("{success:false,msg:'系统异常,上传失败!'}");
             return null;
         }//获得上传文件个数
    int filecount=smartupload.getFiles().getCount();if(filecount>0)
    {
         //获取上传的文件
          File myFile=smartupload.getFiles().getFile(0);
         //判断是否选择了文件,选择时返回false,没有选择返回true
          if(!myFile.isMissing())
         {
         try
           {
          //上传文件名
          String myFileName=myFile.getFileName();
          String s1=smartupload.getRequest().getParameter("chapter").trim();
          System.out.println(s1);
          myFile.saveAs("courseware/"+myFileName, smartupload.SAVE_VIRTUAL);
              }
      catch(Exception e)
           {
               e.printStackTrace();
             System.out.println(e);
           }
         }
    }
      return null;
     }