flash上传在spring mvc中出现的问题2
转载请注明: TheViper http://www.cnblogs.com/TheViper
这两天本屌在做flash拼图上传遇到点坑
上传原理很简单,就是把上图右边画布区域BitmapData.draw()画下来,然后用as3corelib的JPGEncoder将BitmapData编码成ByteArray,ByteArray就是要上传的图片数据。然后
var req:URLRequest = new URLRequest("http://localhost:8080/qzone/photo/upload1?width=555&height=420");
req.data = Data;
req.method = URLRequestMethod.POST;
loader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.load(req);
loader.addEventListener(Event.COMPLETE, upload_complete);
由于传的是ByteArray 对象的二进制数据,后台注入MultipartFile
public @ResponseBody String upload1(MultipartFile filedata,HttpServletRequest request,
HttpSession session)throws JSONException,IOException{
............
}
可以看到这里首先是不能decode解码,然后是请求的content-type不是multipart.
先说解码问题,很容易想到用Base64编码图片,把编码成的字符串当成请求的参数值传给后台。
var req:URLRequest = new URLRequest("http://localhost:8080/qzone/photo/upload");
var variables:URLVariables = new URLVariables();
variables.name ='puzzle_image.jpeg';
variables.imgData =base64String;
variables.width =555;
variables.height =420;
req.data = variables;
后台取出请求中的imgData参数,解码,写入文件就可以了。
而actionscript3 Base64编码要用到mx.utils包里的Base64Encoder类,这个是flex里面的,不过可以从flex里面取出来作为独立的类使用。附上下载。api.
使用时注意不能看到api里面的
就直接
new Base64Encoder().encodeBytes(Data);
variables.imgData =Data;
后面还写着Encodes a ByteArray in Base64 and adds the result to an internal buffer. Subsequent calls to this method add on to the internal buffer. After all data have been encoded, call toString()
to obtain a Base64 encoded String.
意思是Base64编码后的字符串在Base64Encoder类里面,需要通过toString()方法去取,具体的
var b64:Base64Encoder = new Base64Encoder();
b64.encodeBytes(Data);
variables.imgData =b64.toString();
这里其实已经把问题解决了,但是本屌还是要画蛇添足,试试从上面的第二个问题入手,解决图片不能上传的问题。
先试试把请求的content-type设为multipart/form-data。
var req:URLRequest = new URLRequest("http://localhost:8080/qzone/photo/upload1?width=555&height=420");
req.data = Data;
req.method = URLRequestMethod.POST;
req.contentType="multipart/form-data";
这时会出现错误org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found。
这个平时没注意到,原来还要向post体内加东西才能让后台正确解析post里的二进制数据。比如
这里用到UploadPostHelper.as。
package utils
{
import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.utils.*;
public class UploadPostHelper
{ private static var _boundary:String = ""; /**
* Get the boundary for the post.
* Must be passed as part of the contentType of the UrlRequest
*/
public static function getBoundary():String
{ if (_boundary.length == 0)
{
for (var i:int = 0; i < 0x20; i++)
{
_boundary += String.fromCharCode( int( 97 + Math.random() * 25 ) );
}
} return _boundary;
} /**
* Create post data to send in a UrlRequest
*/
public static function getPostData(fileName:String, byteArray:ByteArray, parameters:Object = null):ByteArray
{ var i:int;
var bytes:String; var postData:ByteArray = new ByteArray();
postData.endian = Endian.BIG_ENDIAN; //add Filename to parameters
if (parameters == null)
{
parameters = new Object();
}
parameters.Filename = fileName; //add parameters to postData
for (var name:String in parameters)
{
postData = BOUNDARY(postData);
postData = LINEBREAK(postData);
bytes = 'Content-Disposition: form-data; name="' + name + '"';
for (i = 0; i < bytes.length; i++)
{
postData.writeByte( bytes.charCodeAt(i) );
}
postData = LINEBREAK(postData);
postData = LINEBREAK(postData);
postData.writeUTFBytes(parameters[name]);
postData = LINEBREAK(postData);
} //add Filedata to postData
postData = BOUNDARY(postData);
postData = LINEBREAK(postData);
bytes = 'Content-Disposition: form-data; name="Filedata"; filename="';
for (i = 0; i < bytes.length; i++)
{
postData.writeByte( bytes.charCodeAt(i) );
}
postData.writeUTFBytes(fileName);
postData = QUOTATIONMARK(postData);
postData = LINEBREAK(postData);
bytes = 'Content-Type: application/octet-stream';
for (i = 0; i < bytes.length; i++)
{
postData.writeByte( bytes.charCodeAt(i) );
}
postData = LINEBREAK(postData);
postData = LINEBREAK(postData);
postData.writeBytes(byteArray, 0, byteArray.length);
postData = LINEBREAK(postData); //add upload filed to postData
postData = LINEBREAK(postData);
postData = BOUNDARY(postData);
postData = LINEBREAK(postData);
bytes = 'Content-Disposition: form-data; name="Upload"';
for (i = 0; i < bytes.length; i++)
{
postData.writeByte( bytes.charCodeAt(i) );
}
postData = LINEBREAK(postData);
postData = LINEBREAK(postData);
bytes = 'Submit Query';
for (i = 0; i < bytes.length; i++)
{
postData.writeByte( bytes.charCodeAt(i) );
}
postData = LINEBREAK(postData); //closing boundary
postData = BOUNDARY(postData);
postData = DOUBLEDASH(postData); return postData;
} //--------------------------------------
// EVENT HANDLERS
//-------------------------------------- //--------------------------------------
// PRIVATE & PROTECTED INSTANCE METHODS
//-------------------------------------- /**
* Add a boundary to the PostData with leading doubledash
*/
private static function BOUNDARY(p:ByteArray):ByteArray
{
var l:int = UploadPostHelper.getBoundary().length; p = DOUBLEDASH(p);
for (var i:int = 0; i < l; i++)
{
p.writeByte( _boundary.charCodeAt( i ) );
}
return p;
} /**
* Add one linebreak
*/
private static function LINEBREAK(p:ByteArray):ByteArray
{
p.writeShort(0x0d0a);
return p;
} /**
* Add quotation mark
*/
private static function QUOTATIONMARK(p:ByteArray):ByteArray
{
p.writeByte(0x22);
return p;
} /**
* Add Double Dash
*/
private static function DOUBLEDASH(p:ByteArray):ByteArray
{
p.writeShort(0x2d2d);
return p;
} } }
使用方法
req.method = URLRequestMethod.POST;
req.contentType="multipart/form-data; boundary="+UploadPostHelper.getBoundary();
req.data=UploadPostHelper.getPostData(img_name,Data);
注意到UploadPostHelper.as里面bytes = 'Content-Disposition: form-data; name="Filedata"; filename="';
public @ResponseBody String upload1(@RequestParam("Filedata") MultipartFile filedata,HttpServletRequest request,
HttpSession session)throws JSONException,IOException{
..........
}
这里Filedata就像FileReference.upload()
是在上载传操作中位于文件数据之前的字段名.FileReference.upload()上传时,后台也应该有@RequestParam("Filedata")注解。
flash上传在spring mvc中出现的问题2的更多相关文章
- 文件上传--基于Spring MVC框架+SmartUpload
这篇文章是介绍文件上传的,由于在spring MVC上实现起来和直接在servlet中写有些不同,所以特地写了一下这篇文章,关于不同点,大家可以先阅读一下上一篇文章.好了,下面直接上代码. jab包是 ...
- Spring MVC中,事务是否可以加在Controller层
一般而言,事务都是加在Service层的,但是爱钻牛角尖的我时常想:事务加在Controller层可不可以.我一直试图证明事务不止可以加在Service层,还可以加在Controller层,但是没有找 ...
- spring mvc中的文件上传
使用commons-fileupload上传文件所需要的架包有:commons-fileupload 和common-io两个架包支持,可以到Apache官网下砸. 在配置文件spring-mvc.x ...
- IntelliJ IDEA上创建maven Spring MVC项目
IntelliJ IDEA上创建Maven Spring MVC项目 各软件版本 利用maven骨架建立一个webapp 建立相应的目录 配置Maven和SpringMVC 配置Maven的pom.x ...
- Http请求中Content-Type讲解以及在Spring MVC中的应用
引言: 在Http请求中,我们每天都在使用Content-type来指定不同格式的请求信息,但是却很少有人去全面了解content-type中允许的值有多少,这里将讲解Content-Type的可用值 ...
- 文件上传和下载(可批量上传)——Spring(二)
针对SpringMVC的文件上传和下载.下载用之前“文件上传和下载——基础(一)”的依然可以,但是上传功能要修改,这是因为springMVC 都为我们封装好成自己的文件对象了,转换的过程就在我们所配置 ...
- Http请求中Content-Type讲解以及在Spring MVC中的应用【转】
完全引用自: http://blog.csdn.net/blueheart20/article/details/45174399#t1 此文讲得很清晰,赞! 引言: 在Http请求中,我们每天都在 ...
- Http请求中Content-Type和Accept讲解以及在Spring MVC中的应用
在Http请求中,我们每天都在使用Content-type来指定不同格式的请求信息,但是却很少有人去全面了解content-type中允许的值有多少,这里将讲解Content-Type的可用值,以及在 ...
- [转]Http请求中Content-Type讲解以及在Spring MVC中的应用
本文转自:http://blog.csdn.net/blueheart20/article/details/45174399 引言: 在Http请求中,我们每天都在使用Content-type来指定不 ...
随机推荐
- 剑指offer系列47---翻转单词顺序
[题目]输入“I am a student.”>>>“.tneduts a ma I”.>>输出:student. a am I package com.exe9.off ...
- 【原创】Quartz代码详解
阅读目录 简单介绍 章节1:Quartz简单实例 章节2:Job.JobDetail.JobBuilder 章节3:Trigger.TriggerBuilder 章节4:Scheduler 章节5:J ...
- bzoj3163: [Heoi2013]Eden的新背包问题
Description “寄没有地址的信,这样的情绪有种距离,你放着谁的歌曲,是怎样的心心静,能不能说给我听.”失忆的Eden总想努力地回忆起过去,然而总是只能清晰地记得那种思念的感觉,却不能回忆起她 ...
- free命令、buffer与cache的区别
freefree 命令相对于top 提供了更简洁的查看系统内存使用情况: # free total used free shared buffers cached Mem: 255988 231704 ...
- MySQL优化—工欲善其事,必先利其器之EXPLAIN
最近慢慢接触MySQL,了解如何优化它也迫在眉睫了,话说工欲善其事,必先利其器.最近我就打算了解下几个优化MySQL中经常用到的工具.今天就简单介绍下EXPLAIN. 内容导航 id select_t ...
- Mongodb集群搭建及spring和java连接配置记录
一.基本环境: mongdb3.0.5数据库 spring-data-mongodb-1.7.2.jar mongo-java-driver-3.0.2.jar linux-redhat6.3 tom ...
- aptana studio 3 自动换行(无需插件)
菜单-Window-Preferences-Aptana Studio-Editors,勾选“Enable word wrap”,然后重启编辑器.
- Mongodb(2)创建数据库,删除数据库,创建集合,删除集合,显示文档内容
显示所有数据库列表:show dbs > show dbs local .078GB runoob .078GB > 显示当前数据库:db > db runoob > 显示所有 ...
- jQuery实现的美观的倒计时实例代码
<!DOCTYPE html><html><head><meta charset=" utf-8"><meta name=&q ...
- php启动时候提示PHP startup的解决方法
最近在学习php,配置好php环境后,每次开机都有警告提示说 PHP startup.如下图: 显然这是个小问题,是关于php配置的. 解决这个问题很简单只需要在php.ini 文件中修改 exten ...