关于flex 和java上传是用FileReference 时候遇到的问题

这段时间我们做文件上传,因为是用的flex 没有办法和java是用BlazeDS 进行流的通信传递信息,而且一旦文件很大我们内存就必须要足够大,

所以BlazeDS的方式我们必须要放弃的。

采用方案flex+serverlet 方式轻松进行传递。

其中遇到编码问题:

1 是用URLEncoding 解决(tomcat的类),

2 遇到flex传递过来文件连头解释内容文件都保存了,显然不行,经过查询很多资料

参见资料:http://help.adobe.com/zh_CN/FlashPlatform/reference/actionscript/3/flash/net/FileReference.html#upload

截图:

 POST /handler.cfm HTTP/1.1 
  Accept: text/*
  Content-Type: multipart/form-data; 
  boundary=----------Ij5ae0ae0KM7GI3KM7 
  User-Agent: Shockwave Flash 
  Host: www.example.com 
  Content-Length: 421 
  Connection: Keep-Alive 
  Cache-Control: no-cache
  
  ------------Ij5GI3GI3ei4GI3ei4KM7GI3KM7KM7
  Content-Disposition: form-data; name="Filename"
  
  MyFile.jpg
  ------------Ij5GI3GI3ei4GI3ei4KM7GI3KM7KM7
  Content-Disposition: form-data; name="Filedata"; filename="MyFile.jpg"
  Content-Type: application/octet-stream
  
  FileDataHere
  ------------Ij5GI3GI3ei4GI3ei4KM7GI3KM7KM7
  Content-Disposition: form-data; name="Upload"
  
  Submit Query
  ------------Ij5GI3GI3ei4GI3ei4KM7GI3KM7KM7--


是这种格式文件,所以必须要经过解析才能拿到文件内容,

这个也有想过自己手动写个解析类,但是分析了下这个结构,貌似不是很好解析啊,所以没有办法,但是在一分析,这个结构有点像是apache的开源上传组件的文件结构,

org.apache.commons.fileupload.servlet.ServletFileUpload  这个类,


这两个个包应该是必须的,经过测试果然能用。

C:\Users\yjy\.m2\repository\com\huadi\cscp\commons-fileupload\1.2.2\commons-fileupload-1.2.2.jar

C:\Users\yjy\.m2\repository\com\huadi\cscp\commons-io\1.4\commons-io-1.4.jar

下面是代码分享,这里的重点是servlet  之贴出servlet的代码:

package com.gzhdi.cscp.service;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.DefaultFileItemFactory;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.log4j.Logger;

import com.gzhdi.cscp.api.comm.def.CDMIRequestDefine;
import com.gzhdi.csmp.gate.DataOperate;


@SuppressWarnings("deprecation")
public class UpDownFFF extends HttpServlet
{
	private static final long serialVersionUID = 1L;

	private static final Logger logger = Logger.getLogger(UpDownFFF.class);

	DataOperate op = new DataOperate(null, null);

	public static final String METHOD_POST = "POST";

	public static final String METHOD_GET = "GET";

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UpDownFFF()
	{
		super();
	}

	/**
	 * 这里的意思就是我帮你请求的意思,请求转发
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		logger.info("Download START.......");
		String uri = request.getPathInfo();

		InputStream in = op.httpActionForInputStream(METHOD_GET, uri + "?"
				+ request.getQueryString(), false);
		OutputStream out = response.getOutputStream();

		int n = 0;
		byte[] b = new byte[1024];
		while ((n = in.read(b)) != -1)
		{
			out.write(b, 0, n);
		}
		in.close();
		out.close();
		logger.info("文件下载在客户端时刻:" + System.currentTimeMillis());
		logger.info("Download END.......");
	}

	/***
	 * 设计成为 proxy 统一使用
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		logger.info("Upload START.......");
		request.setCharacterEncoding("UTF-8");
		String uri = new String(request.getPathInfo().getBytes("iso8859-1"),"utf-8")+"?"+request.getQueryString();
		request.getParameter("Upload");
		InputStream in=null;
		try
		{ 
			FileItemFactory filefactory=new DefaultFileItemFactory() ;
			//解析request body拿出来文件体
			ServletFileUpload upload = new ServletFileUpload(filefactory);
			@SuppressWarnings("unchecked")
			List<FileItem> fileItems = upload.parseRequest(request);
			Iterator<FileItem> iter=fileItems.iterator();
			FileItem fileItem=null;
			while(iter.hasNext())
			{
				fileItem=iter.next();
				if(!fileItem.isFormField())
				{
					in=fileItem.getInputStream();
				}
			}
		} catch (FileUploadException e)
		{
			e.printStackTrace();
		}
		String re = op.proxyHttpAction(METHOD_POST, uri, in,
				null);
		
		OutputStream out = response.getOutputStream();
		out.write(re.getBytes());
		out.close();
		
		/test
//		InputStream sendDataStream=request.getInputStream();
//		int n = 0;
//		int count=0;
//		byte[] b = new byte[1024];
//		while ((n = sendDataStream.read(b)) != -1)
//		{
//			count+=n;
//			System.out.print("recevie:"+new String(b,0,n));
//		}
//		logger.info("Upload END.......");
	}

	/**
	 * 组装请求头 统一使用
	 * @param request
	 * @author yinlei |2012-9-3 下午4:21:59
	 * @version 0.1
	 */
	public Map<String, String> getHeaders(HttpServletRequest request)
	{
		Map<String, String> map = new HashMap<String, String>();
		String sign = request.getHeader(CDMIRequestDefine.HEADER_SIGN);
		if (sign != null)
		{
			map.put(CDMIRequestDefine.QUERY_APPID, sign);
		}

		return map;
	}

}




可以参见这个,这个兄弟写的很详细,非常感谢:http://www.blogjava.net/rainwindboys/archive/2008/09/18/229219.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值