微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

FLEX搭配JSP下载文件的例子

FLEX代码

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
 <mx:Script>
  <![CDATA[
   public static function export():void{

   System.useCodePage = true;
   var url:String = "http://localhost:8080/flexSpring/exportExcel.jsp";
   var variables:urlvariables = new urlvariables();
   variables.fileType= "swf";
   variables.fileName="aaa"
   variables.filePath="/uploadFile/Dashboard.swf";
   var u:URLRequest = new URLRequest(url);
   u.data = variables;
   u.method = URLRequestMethod.POST;
   navigatetoURL(u,"_self");
  }
  ]]>
 </mx:Script>
 <mx:Button  label="test" click="export()"/>
</mx:Application>

JSP代码

<%@ page language="java" import="java.util.*,java.io.*,java.net.URLDecoder" pageEncoding="GBK"%>
<%@page contentType="application/msexcel;charset=GBK"%>
<%
 request.setCharacterEncoding("utf-8");
 InputStream is = request.getInputStream(); 
 BufferedReader tBufferedReader = new BufferedReader(new InputStreamReader(is));
 StringBuffer sb = new StringBuffer();
 String temp = new String("");
 while ((temp = tBufferedReader.readLine()) != null){
  sb.append(temp);
 }
 temp = sb.toString();
 String[] params = temp.split("&");
 HashMap<String,Object> paramMap = new HashMap<String,Object>();
 for(int i=0;i<params.length;i++){
  paramMap.put(params[i].split("=")[0],params[i].split("=")[1]);
 }
 String fileType = paramMap.get("fileType").toString();
 response.setHeader("Content-disposition","attachment;filename="+ new String(URLDecoder.decode(paramMap.get("fileName").toString()).getBytes(),"ISO8859_1") + "." + fileType.toLowerCase());
  try
     {
  Requestdispatcher dispatcher = request.getRequestdispatcher(URLDecoder.decode(paramMap.get("filePath").toString()));
  dispatcher.forward(request,response);
  response.flushBuffer();
  out.clear();
  out = pageContext.pushBody();
     }
     catch(Exception e)
     {
         e.printstacktrace();
     }
%>

这个例子和上个flex例子区别不大,唯一的就是变相的实现了flex下载和打开文件

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐