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

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() {
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值