当前位置: 代码迷 >> J2EE >> form提交后台上传文件,为什么得不到文件内容?该怎么处理
  详细解决方案

form提交后台上传文件,为什么得不到文件内容?该怎么处理

热度:278   发布时间:2016-04-22 02:44:14.0
form提交后台上传文件,为什么得不到文件内容??
版主、各位大侠,小弟在此求教了!

我为了实现客户端导入功能,写了一个jsp页面用来form提交要导入的文件到服务器端,但在后台取读取不到文件的内容。请赐教!!!!
jsp代码如下:

HTML code
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%><%    String path = request.getContextPath();    String basePath = request.getScheme() + "://"            + request.getServerName() + ":" + request.getServerPort()            + path + "/";    String contextPath = request.getContextPath();%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>    <head>        <base href="<%=basePath%>">        <title>文件上传</title>        <script language="javascript"            src="<%=basePath%>/pms/platform/pms.js"></script>        <script type="text/javascript"            src="<%=basePath%>common/ext/ext-base.js"></script>        <script type="text/javascript"            src="<%=basePath%>common/ext/ext-all.js"></script>        <script type="text/javascript"            src="<%=basePath%>common/ext/ext-lang-zh_CN.js"></script>        <link rel="stylesheet" type="text/css"            href="<%=basePath%>common/ext/resources/css/ext-all.css" />        <script language="javascript"            src="<%=basePath%>/common/fileUpload/fileUploadAction.js"></script>        <meta http-equiv="pragma" content="no-cache">        <meta http-equiv="cache-control" content="no-cache">        <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">        <meta http-equiv="description" content="This is my page">        <!--    <link rel="stylesheet" type="text/css" href="styles.css">    -->        <style type="text/css">        #jtxt{            color:#666666;            font-size:12px;        }    </style>        <script type="text/javascript">    var pbar1;    //定义一个变量用于进度条对象    var btn1;    //定义一个变量用于进按钮对象        Ext.onReady(function(){        pbar1=new Ext.ProgressBar({    //实例化进度条            renderTo:"div1",        //进度条呈现的一个DIV            width:350,                //进度条的宽度            text:"上传进度..."    //在进度条里的初始文本        });        //btn1=Ext.get("btnUpload");        //按钮对象        //btn1.on("click",isnull);        //按钮事件,单击时调用f1()函数        //隐藏进度条        document.getElementById("div1").style.display='none';    });    var items = ["file","btnUpload","btnReset"];    function isnull(){        var file = document.getElementById("file").value;        if(file==null || file==""){            alert("请选择上传的文件!");            return ;        }        document.getElementById("div1").style.display='';        f1();    }    function f1(){        //实际文件没有上传完成效果        if(document.getElementById("hd").value==96){            document.getElementById("hd").value=95;        }        var i = document.getElementById("hd").value++;//取得当前进度和设置进度                pbar1.updateProgress(i/100.0); //让进度条更新到指定的进度。这个值是0-1之间的数,我让i/100.0,以让它的结果是一个小数        pbar1.updateText("进度:"+i+"%");    //进度条显示的文本        //当i==100的时候让按钮状态可以使用,并让i=1,再让进度条停止            if(i==100){            //设置相应元件可编辑            _setDisabled(items,false);            //设置初始进度为0            document.getElementById("hd").value=0;            try{                clearInterval(x);            }catch(e){                return ;            }        }        //设置相应元件状态为不可用        _setDisabled(items,true);        setTimeout("f1()",1);//用setTimeout函数递归调用f1()函数,以达到进度条一直跑的效果    }    /**    *用于设置一些元件是否可编辑    */    function _setDisabled(items,isTrue){        for(var i=0;i<items.length;i++){            document.getElementById(items[i]).disabled=isTrue;        }    }        /*     *动态结束进度条,使其完成度为100%    */    _closeProgress = function ()    {        document.getElementById("hd").value=99;        //f1();    }    //隐藏进度条    _displayNone = function ()    {        document.getElementById("div1").style.display='none';        document.getElementById("file").value = "";    }        /*     * 导入操作     */    function OperExport()    {        if (_isExporting)        {            alert("正在导入过程中,请稍候再操作...");            return;        }                var flowid = GetUrlQuery("flowid");        var patternid = GetUrlQuery("patternid");        var formworkname = GetUrlQuery("formworkname");        var startrownum = GetUrlQuery("startrownum");        var flag = GetUrlQuery("flag");                var list = parent.defaultForm.getCellsByType("EditGrid")[0];        var fileObj = document.getElementById("file");        var file = fileObj.value;        if(file != "")        {            var option = confirm("确定导入文件 " + file + " ?");            if(option)            {debugger;                //file = file.replace("\", "\\");                isnull();//启动进度条                                mainform = document.getElementById("myform");                  mainform.action = parent.$appRoot+ "/servlet/com.sdjxd.pms.platform.serviceBreak.Invoke?_sender=page&_c=zyws.TJGLMethod&_m=impExcelData&_p0=\""+flowid+"\"&_p1=\""+patternid+"\"&_p2=\""+formworkname+"\"&_p3=\""+startrownum+"\"&_p4="+JSON.stringify(parent._fileExParam)+"&_p5=\""+file+"\"&_p6=\""+flag+"\"";                mainform.submit();                _isExporting = true;            }        }        else        {            alert("请先选择文件!");        }    }        /*     * 重置文件选择内容     */    resetFileObj = function ()    {        var file = document.getElementById("file");        file.value = "";    }    //控制上传文件格式    var fileFormat = function(){        var file = document.getElementById("file").value;        if(file==null || file==""){            return ;        }        //截取扩展名并转换为小写        var str = file.substring((file.length-4),file.length).toLowerCase();        if(str==null || str != ".xls" && str != "xlsx"){            document.getElementById("myform").reset();            alert("请上传正确的电子表格文件!");            return ;        }    }    /*      * 回调函数     */    var impResultBack = function (result, sMsg)    {        _isExporting = false;        _closeProgress();//结束进度条        setTimeout("sleep('"+sMsg+"')",1000);    }    //暂停1秒执行函数,使上传进度效果更好    var sleep = function(sMsg){        var list = parent.defaultForm.getCellsByType("EditGrid")[0];        alert(sMsg)           _displayNone();//隐藏进度条           if (parent._fileUploadWin)            parent._fileUploadWin.hide();        list.refresh();    }    </script>    </head>    <body        style="font-size:12px; width:100%; margin:0px; border-width:0px; overflow:auto; text-align:center; ; background-color:#E4EEEF;">        <br>        <input id="hd" type="hidden" value="0">        <div id="div1"></div>        <iframe name="upload_iframe" id="upload_iframe" style="width: 400px; height: 100px;display:none" src="about:blank"></iframe>        <form id="myform" name="myform" target="upload_iframe" enctype="multipart/form-data"            action="" method="post">            <p>                &nbsp;            </p>            <table width="413">                <tr>                    <td width="60" align="right"                        style="word-break:keep-all;white-space:nowrap;">                        <span id="jtxt">文件名:&nbsp;</span>                    </td>                    <td width="353" align="left">                        <input name="file" type="file" id="file"                            style="border:1px solid #92C1ED; background-color:#FFF;"                            onkeydown="return false;" size="40" height="39px"                            onpaste="return false;" onchange="fileFormat();"/>                    </td>                </tr>                <tr>                    <td width="60">                        &nbsp;                    </td>                    <td width="353">                        &nbsp;                    </td>                </tr>                <tr>                    <td width="60">                        &nbsp;                    </td>                    <td align="right" width="353">                        <input id="btnUpload" type="button"                            onclick="OperExport();"                            style="border:1px solid #92C1ED;" value="上&nbsp;传" />                        <input id="btnReset" type="reset" onclick="resetFileObj();"                            style="border:1px solid #92C1ED;" value="重&nbsp;置" />                        &nbsp;&nbsp;                    </td>                </tr>            </table>        </form>    </body></html>
  相关解决方案