美文网首页扣丁学堂Java培训我爱编程
扣丁学堂Java开发实现文件点击没反应的源码方法详解

扣丁学堂Java开发实现文件点击没反应的源码方法详解

作者: 994d14631d16 | 来源:发表于2018-07-26 14:15 被阅读6次

      今天扣丁学堂Java培训老师给大家准备了一篇关于Java实现文件点击没反应的方法及具体源码详解,比如jsp页面链接,点击访问action用IO流去下载服务器上的文件,问题是任凭怎么点击都没反应,日志也不报错,下面我们一起来看一下吧。

    ​  前台ajax代码

      Ext.Ajax.request({

      url:'/yjy/training/TrainingTimeAction.do?method=downLoadAttchById',

      params:{

      timeId:timeids

      },

      success:function(response,options){

      varresult=Ext.util.JSON.decode(response.responseText);

      Ext.Msg.alert("下载成功");

      },

      failure:function(response,options){

      varresult=Ext.util.JSON.decode(response.responseText);

      Ext.Msg.alert("下载失败"+result.message);

      }

      });

      后台action代码

      StringtimeId=request.getParameter("timeId");

      Stringsql="selectdoc_namefromCPER.EHRTRAIN_item_DOCUMENTwhereitem_id=?";

      DbHelperdbHelper=newDbHelper();

      Object[]params=newObject[]{timeId};

      StringfileName=(String)dbHelper.runSQLScalar(sql,params);

      StringfilePath=ServerPathUtil.getPathRoot()+"WEB-INF/cache/train_item_file/train_item_file_"+timeId+"/"+fileName;

      Filefile=newFile(filePath);

      if(!file.exists()){

      logger.debug("文件不存在");

      thrownewIOException("thefilenotexists");

      }

      response.setContentLength((int)file.length());

      OutputStreamo=response.getOutputStream();

      byteb[]=newbyte[5000];

      //response.setContentType("application/x-msdownload");

      response.setContentType("application/vnd.ms-excel");

      response.setContentLength((int)file.length());

      response.setHeader("Content-Disposition","attachment;filename="+fileName);

      FileInputStreamin=newFileInputStream(file);

      intn;

      while((n=in.read(b))!=-1){

      o.write(b,0,n);

      }

      in.close();

      }catch(Exceptione){

      e.printStackTrace();

      }

      解决方法:文件的下载,在前台请求的时候,只能是form表单请求,或者用window.open的方式,最后我采用了window.open的方式

      window.open('/yjy/training/TrainingTimeAction.do?method=downLoadAttchById&timeId='+timeids);

      注:采用这种方式页面会弹出一个空白窗口,下载之后窗口自动关闭,如果不想显示这个窗口,使用form提交的方式

      以上所述是小编给大家介绍的Java实现文件点击没反应的方法,希望对大家有所帮助,非常感谢大家对扣丁学堂小编的支持!

    相关文章

      网友评论

        本文标题:扣丁学堂Java开发实现文件点击没反应的源码方法详解

        本文链接:https://www.haomeiwen.com/subject/lvlbmftx.html