java jsp Struts2.X 文件上传

/**

*

*操作系统:WIN-XP

*后台程序:Java

*前端脚本:JavaScript/ExtJs/Html

*做过的尝试:用form的submit方式提交,在后台request.getInputStream()取到的值*为null,而*request.getContentLength()是可以正常取值的。用request.getRead()抛出错误,request has been *called 网查之后得知,似乎是Struts过滤掉了。另寻他法于是便有了下面代码。    

*/

@ParentPackage(StrutsNamespace.PP_JSON) 

@Namespace("")

@Action("savebillfile") 

public class SaveBillFileAction extends SDBaseAction {  

  

private static final long serialVersionUID = 5156288255337069381L; 

  

    private  String msg; 

    private int ErrNo;

    private String contentType; 

    private File docmentFile; 

    private String fileName; 

    private Boolean success = false;

    private String billtype;

    private String billid;

   

public String upload() throws Exception { 

    Connection con =null;

    Statement stm =null;

    PreparedStatement pst=null;

    ErrNo=0;

    msg="";

    if(!checkFile())

    {

    return SUCCESS;

    }

    if(docmentFile.isFile())

    {

    FileInputStream in = new FileInputStream(docmentFile);

    try{

      con=ConnectPools.getConnection(getSession());

      Date dt=new Date();

      String date = DateFormat.getDateInstance().format(dt);

      String sSql = "insert into billfiles(billtype,billid,filename,fileext,uploaddate,filesize,opid,filedata)values(?,?,?,?,?,?,?,?)";

      pst = con.prepareStatement(sSql);

      pst.setString(1, billtype);

      pst.setString(2, billid);

      pst.setString(3, fileName);

      pst.setString(4, fileName.substring(fileName.indexOf("."), fileName.length()));

      pst.setString(5, date);

      pst.setLong(6, in.available());

      pst.setInt(7, ((EmployInfo)this.getSession().getAttribute("employ")).getId());

      pst.setBinaryStream(8,in,in.available());

      pst.executeUpdate();    

    }

    catch(Exception e){

    ErrNo=1;

       msg=e.getMessage();

    }

    finally{

    try{

    pst.close();

    stm.close();

    con.close();

    }catch(Exception e)

    {

    ErrNo=1;

    msg=e.getMessage();

    }

    }

    }

   

    setSuccess(true);

        return SUCCESS; 

    } 

    private boolean checkFile() {

// TODO Auto-generated method stub

    //检查文件

        return true;

    }

    public void setFileName(String fileName) { 

        this.fileName = fileName; 

    } 

    public void setUploadFileName(String fileName) { 

        this.fileName = fileName; 

    } 

    public void setUploadContentType(String contentType) { 

        this.contentType = contentType; 

    } 

    public void setUpload(File docmentFile) { 

        this.docmentFile = docmentFile; 

    } 

   

    public String getMsg() { 

        return msg; 

    } 

   

    public void setSuccess(Boolean success) {

this.success = success;

}


public Boolean getSuccess() {

return success;

}

public void setBilltype(String billtype) {

this.billtype = billtype;

}


public void setBillid(String billid) {

this.billid = billid;

}

public int getErrNo() {

return ErrNo;

}


public void setMsg(String msg) { 

        this.msg = msg; 

    }

    public void setContentType(String contentType) { 

        this.contentType = contentType; 

    } 

}

//到此java代码全了。。

//接下来是脚本submit之后发现上传的进度条一直在那里走来走去,我滴个肾,又是一番百度。由于我这不要跳转,百度上许多方法在我这似乎行不通。。只好改用了Ajax。。

/*

*  ajax那部分脚本如下

*/

var params={};

Ext.apply(params,{billtype:this.billType});

Ext.apply(params,{billid:this.billId});

Ext.Ajax.request({

         url : 'savebillfile!upload.action',

         timeout : 10000,

         scope : this,

         params:params,

         isUpload:true,

         form:"ufile",/*发现这个form好像有点不一样。。。ExtJs直接写出来的form好像有问题?*/

         success : function(response)

         {

             var str =                                 response.responseText.substring(response.responseText.indexOf('{'),response.responseText.indexOf('}')+1);//前台脚本返回来的竟然被<pre>框起来了。好吧,为了正常使用,就手动把json找出来。

              var objFormData = Ext.util.JSON.decode(str);

                if(objFormData.ErrNo==0)

               {

                                  Ext.Msg.show({

                  title:'提示',

                               buttons:Ext.MessageBox.OK,

               msg: '上传成功!',

                                                                                    fn:function(button,e)

               {

                //上传成功关闭上传附件窗口

                var window = Ext.getCmp('selectfileid');

                window.close();

                //刷新列表

                var win= Ext.getCmp('billfileid');

                var grid = win.findById('billfile_grid');

                       var store = grid.store;

                     

                       store.load({

                           params:params

                       });

                }

                });

                }

                }

                    });

/*

* 最后贴出form的代码

*/

var fPanel=new Ext.Panel({

            layout : 'form',

            height:40,

            html:'<form id="ufile" enctype="multipart/form-data">请选择附件<input type="file" name="upload" size="50"></form>'

        });


你可能感兴趣的:(java,文件上传,struts,ExtJs)