加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Flex用filereference上传文件firefox下报2038错误

发布时间:2020-12-15 04:09:06 所属栏目:百科 来源:网络整理
导读:Flex Code : var urlVars:URLVariables = new URLVariables(); urlVars.jsessionid = sessionID; var uploadUrl:String = “http://localhost:8080/mywar;jsessionid=”+sessionID; uploadUrl += “?”+getClientCookies(); //put all client cookies on the

Flex Code :

var urlVars:URLVariables = new URLVariables();
urlVars.jsessionid = sessionID;

var uploadUrl:String = “http://localhost:8080/mywar;jsessionid=”+sessionID;
uploadUrl += “?”+getClientCookies(); //put all client cookies on the query string
var urlRequest:URLRequest = new URLRequest(uploadUrl);
urlRequest.method = URLRequestMethod.POST;
urlRequest.data = urlVars;

//will go first time and get the cookies set see flex docs
var testUpload:Boolean = true;
fileRef.upload(urlRequest,”Filedata”,testUpload);

JAVA CODE :

package com.mywar.fileupload;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @author orasio – spieler
* This filter comes to solve the Firefox,Chrome and SAFARI file upload issue
* The problem was that the file uploaded by the flex
* FileReference came with a different session and no cookies
* To solve this problem do the following :
*
*
* don’t forget to add this filter to the web.xml file
*/
public class FileUploadFilter implements Filter {

private static final String CONTENT_LENGTH = “content-length”;
private static final String UPLOAD_SITE_PATH = “/”;
private static final String JSESSIONID = “JSESSIONID”;

@Override
public void init(FilterConfig filterConfig) throws ServletException {
}

@Override
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain filterChain)
throws IOException,ServletException {
if ((request instanceof HttpServletRequest)
&& (response instanceof HttpServletResponse)) {
HttpServletRequest httpRequest = (HttpServletRequest) request;

//httpRequest.getHeader(“user-agent”); //Shockwave Flash
String contentLength = httpRequest.getHeader(CONTENT_LENGTH);
boolean isFlexTest = (contentLength!=null
&& Integer.parseInt(contentLength)==0);
if(isFlexTest){
HttpServletResponse httpResponse =
(HttpServletResponse) response;
setAllClientCookie((HttpServletResponse)response,httpRequest);
PrintWriter out = httpResponse.getWriter();
out.println(“OK”);
out.close();
return;
}
}
filterChain.doFilter(request,response);
}

/*
* write all cookies back to the flex test response
*/
@SuppressWarnings(“unchecked”)
private void setAllClientCookie(HttpServletResponse httpResponse,
HttpServletRequest httpRequest) {
Enumeration parameterNames =
(Enumeration)httpRequest.getParameterNames();
while (parameterNames.hasMoreElements()) {
String cookieName = (String) parameterNames.nextElement();
//since we get IllegalArgumentException: Cookie name “JSESSIONID” is a reserved token

if(!cookieName.contains(JSESSIONID)) {
Cookie cookie =
new Cookie(cookieName,httpRequest.getParameter(cookieName));
cookie.setPath(UPLOAD_SITE_PATH);
httpResponse.addCookie(cookie);
}
}
}

@Override
public void destroy() {
}

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读