文件下载 上传ext4.0.7+struts2

来源:互联网 发布:js prototype 阮一峰 编辑:程序博客网 时间:2024/05/21 11:36

所需jar包
这里写图片描述

ext界面

{    xtype : 'button',    cls : 'btn-color',    text : "模板下载",    width : 58,    handler : downLoad},做一个隐形表单将其提交function downLoad() {            download_form.submit();        }//表单<form action="pub/pob/download.action"        style="MARGIN: 0px; PADDING-LEFT: 0px; WIDTH: 0px; PADDING-RIGHT: 0px; HEIGHT: 0px; PADDING-TOP: 0px"        id="download_form" method="post"        name="download_form">        <INPUT id="fName" value="分支总表Excel表格模板.xls" type="hidden" name="fName">    </form>
struts2的配置<action name="download" class="com.holley.coms.web.pub.sys.Up_DownLoadAction"            method="excute"><result name="success" type="stream">//mime类型 因为要导出xls类型的    <param name="contentType">application/vnd.ms-excel</param>    //一个获取流的get方法    <param name="inputName">downLoadFile</param>    //对应的是下载文件的名称,在类中要有其getset方法    <param name="contentDisposition">attachment;filename="${fileName}"</param>    //写入大小    <param name="bufferSize">1024</param></result></action>
//后台java    private String fileName;    private String inputPath;    private InputStream DownLoadFile;    private File excelFile;    /**     * 下载excel文件     *      * @return     */    public String excute() {        String fName = getRequest().getParameter("fName");        if (StringUtil.isEmpty(fName)) {            System.out.println("文件不存在");            success = false;            return SUCCESS;        }        success = true;        //从前台获取文件名称,set进去        setFileName(fName);        return SUCCESS;    }    /**     * 获取指定位置文件流     *      * @return     */    public InputStream getDownLoadFile() {        return ServletActionContext.getServletContext().getResourceAsStream("/fireFox/" + fileName);    }

文件上传

//上传按钮    {    xtype:'button',    cls : 'btn-color',    text : "导入",    width : 58,    handler: fileUp    },//上传弹框//导入文件        function fileUp(){            var upPanel = Ext.create('Ext.form.Panel',{                title:'',//              width:600,                bodyPadding:10,//              height:400,                items:[{                    xtype:'textfield',                    allowBlank:false,                    fieldLabel:'选择文件',                     inputType:'file',                    //这里的name要和下面的fileName获取的name属性要一样                    name:'excelFile'                }],                buttons:[{                    text:'上传',                    margin:'1 50 3 10',                    handler:function(){                        var form = this.up('form').getForm();                        var fileName = form.findField('excelFile').getValue();                        if(fileName==null||fileName==undefined||fileName==""){                            form.reset();                            Ext.Msg.alert("注意","请选择Excel文件!");                            return;                        }                        /* var type = fileName.substring(fileName.indexOf("."),fileName.length);                        if(Ext.isEmpty(type)||type!=".xls"){                            Ext.Msg.alert("注意","请选择后缀为.xls的文件!");                            form.reset();                            return;                        } */                        if(form.isValid()){                            form.submit({                                url:'pub/pob/upload.action',                                params : {fileName : fileName},                                method:'POST',                                waitTitle:'请稍等',                                waitMsg:'正在上传,请稍等...',                                success:function(fp,o){                                    Ext.Msg.alert('信息','文件上传成功');                                    upWin.close();                                },                                failure:function(fp,o){                                    Ext.Msg.alert('警告','连接失败');                                    upWin.close();                                }                            });                        }                    }                }]            });            upWin = Ext.create('Ext.window.Window',{                width:400,                height:120,                title:'文件上传',                modal:true,                layout:'fit',                items:[upPanel]            }).show();        }

struts2的配置文件

<action name="upload" class="com.holley.coms.web.pub.sys.Up_DownLoadAction"            method="upload">            <result name="success" type="json">                <param name="contentType">text/html</param>            </result></action>

后台java代码

/**     * 上传     *      * @return     * @throws Exception     */    public String upload() throws Exception {    //这里的fileName和前台的fileName要一样        String fileName = getRequest().getParameter("fileName");        if(StringUtil.isEmpty(fileName)){            return SUCCESS;        }        String excelPath = "upFile\\" + fileName;        String upPath = getSession().getServletContext().getRealPath("/") + excelPath;        if (excelFile.isFile()) {            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(excelFile));            BufferedOutputStream bos = null;            try {                bos = new BufferedOutputStream(new FileOutputStream(upPath));                byte[] buff = new byte[8192];                for (int len = -1; (len = bis.read(buff)) != -1;) {                    bos.write(buff, 0, len);                }                bos.flush();            } catch (Exception e) {                e.printStackTrace();                returnMsg = "文件上传失败";                HttpServletResponse response = ServletActionContext.getResponse();                response.setContentType("text/html;charset=UTF-8");                success = false;                return SUCCESS;            } finally {                if (bis != null) {                    try {                        bis.close();                    } catch (Exception e2) {                        e2.printStackTrace();                    }                }                if (bos != null) {                    try {                        bos.close();                    } catch (Exception e2) {                        e2.printStackTrace();                    }                }            }        }        List<List<String>> BranchList = parseExcel(excelPath);        boolean flag = optBranch(BranchList);        returnMsg = "文件上传成功";        success = true;        HttpServletResponse response = ServletActionContext.getResponse();        response.setContentType("text/html;charset=UTF-8");        return SUCCESS;    }
0 0
原创粉丝点击