- var url:String="http://202.197.108.65:8080/Flex/uploadmp3.jsp? musicname="+music_name.text+"
- &singername="+singer_name.text;
- var url:String="http://202.197.108.65:8080/Flex/uploadmp3.jsp? musicname="+music_name.text+"
- &singername="+singer_name.text;
var url:String="http://202.197.108.65:8080/Flex/uploadmp3.jsp? musicname="+music_name.text+" &singername="+singer_name.text;
显然这里是上传音乐过程,顺便把音乐名及歌手名上传上去,其中音乐名与哥手名是中文,我用自己的机子开启TOMCAT做服务器,然后开启本地浏览器登陆,上传音乐,没一点问题,中文可以在后台打印出来.但是通过IP访问我的服务器时,就出现问题了,浏览器将"musicname="+music_name.text+"&singername="+singer_name.text"
两个参数musicname与singername解析成了一个参数,于是后者在JSP中用request.getParameter("singername");
其结果为null所以就报了空指针错误.自然上传过程中也会报错.那么我是怎么解决的呢?用代码来说明问题.
FLEX中代码如下:
var url:String="http://202.197.108.65:8080/Flex/uploadmp3.jsp? musicname="+
encodeURIComponen(music_name.text) +"&singername="+
encodeURIComponent (singer_name.text);
var request:URLRequest = new URLRequest(url); request.method="post";
大家注意看,第二次代码与第一次代码中URL的不同,见红色部分.这里就是对中文参数进行编码,在FLEX中默认编码方式为
UTF-8,这里是对中文参数用UTF-8进行编码,那么在后台我们要对其行进解码.
JSP中,核心代码如下:
String mp3name=new String(musicname.getBytes("ISO8859-1"),"utf-8");
String mp3singer=new String(singername.getBytes("ISO8859-1"),"utf-8");
这是一个解码的过程.
然后经过印,就可得到我们梦寐以求的浏览器传过来的中文参数了.到此,FLEX中上传文件,附代中文参数的问题得到解决.
有些朋友照上面进行操作还可能出现乱码问题,原因可能如下:
即在你的FLEX页面中有如下设置:
System.useCodePage=true;
System.useCodePage=true;是用你浏览器的编码方式来解行编码,那么你在后台用UTF-8,或者gbk解码都可能出现乱码问题,甚至出现解析参数出错.我们提供的解决方法如下
或者将 System.useCodePage=false;
或者将这句话注释即可(默认为false).
这个问题困惑了我两天,今天终于解决,希望有同样困惑的朋友能早日解决其问题.
下面附上完整代码:
Flex:(附中文参数文件上传部份,这里只写了函数部份,触发函数的组件大家可以自己写)
- //上传音乐部份
- public var myFileReference:FileReference = new FileReference();
- public var mp3Type:FileFilter=new FileFilter("MP3(*.mp3)","*.mp3");
- public var allowType:Array=new Array(mp3Type);
- private function uploadMP3():void{
- // System.useCodePage=false;//GBK
- var musicname:String=music_name.text;
- var singername:String=singer_name.text;
- if(musicname==""){
- mx.controls.Alert.show("音乐名称不能为空");
- }elseif(singername==""){
- mx.controls.Alert.show("歌手不能为空");
- }elseif(musci_sName.text==""){
- mx.controls.Alert.show("上传音乐不能为空");
- }else{
- // Alert.show("歌手名:"+singername);
- // Alert.show("歌曲名"+musicname);
- var url:String="http://202.197.108.65:8080/Flex/uploadmp3.jsp?musicname="+encodeURIComponent(music_name.text)+"&singername="+encodeURIComponent(singer_name.text);
- var request:URLRequest = new URLRequest(url);
- request.method="post";
- try
- {
- //上传文件
- myFileReference.upload(request);
- CursorManager.setBusyCursor();
- // showDetail.text = "upload";
- }catch (error:Error){
- cursorManager.removeBusyCursor();
- removeAllEvent();
- mx.controls.Alert.show("上传出错");
- }
- }
- }
- public function browse():void{
- //浏览本地本地文件
- myFileReference.browse(allowType);
- //为打开选择框定义事件处理
- myFileReference.addEventListener(Event.SELECT, selectHandler);
- //在上传过程中触发的事件处理
- myFileReference.addEventListener(ProgressEvent.PROGRESS, onProcess);
- // myFileReference.addEventListener(Event.COMPLETE, onComplete);
- //在上传过程中出错处理
- myFileReference.addEventListener(IOErrorEvent.IO_ERROR,ioErrerHandler);
- //处理返回信息
- myFileReference.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,returnmsgHandler);
- }
- //先择文件
- public function selectHandler(event:Event):void{
- musci_sName.text=myFileReference.name;
- }
- //取消文件上传
- public function uploadCancel():void{
- myFileReference.cancel();
- CursorManager.removeBusyCursor();
- removeAllEvent();
- }
- //上传过程中的事件
- public function onProcess():void{
- mx.managers.CursorManager.setBusyCursor();
- }
- import mx.managers.CursorManager;
- public function returnmsgHandler(event:DataEvent):void{
- CursorManager.removeBusyCursor();
- var result:Boolean=event.data.toString() as Boolean;
- mx.controls.Alert.show("上传成功");
- sendRequestToGetMusicInfo();
- }
- //上传过程中出错处理
- public function ioErrerHandler(event:IOErrorEvent):void{
- removeAllEvent();
- CursorManager.removeBusyCursor()
- Alert.show("上传出错","错误");
- }
- //remove all the listerners
- public function removeAllEvent():void{
- myFileReference.removeEventListener(Event.SELECT, selectHandler);
- //在上传过程中触发的事件处理
- myFileReference.removeEventListener(ProgressEvent.PROGRESS, onProcess);
- // myFileReference.removeEventListener(Event.COMPLETE, onComplete);
- myFileReference.removeEventListener(IOErrorEvent.IO_ERROR,ioErrerHandler);
- myFileReference.removeEventListener(DataEvent.UPLOAD_COMPLETE_DATA,returnmsgHandler);
- }
- //上传音乐部份
- public var myFileReference:FileReference = new FileReference();
- public var mp3Type:FileFilter=new FileFilter("MP3(*.mp3)","*.mp3");
- public var allowType:Array=new Array(mp3Type);
- private function uploadMP3():void{
- // System.useCodePage=false;//GBK
- var musicname:String=music_name.text;
- var singername:String=singer_name.text;
- if(musicname==""){
- mx.controls.Alert.show("音乐名称不能为空");
- }elseif(singername==""){
- mx.controls.Alert.show("歌手不能为空");
- }elseif(musci_sName.text==""){
- mx.controls.Alert.show("上传音乐不能为空");
- }else{
- // Alert.show("歌手名:"+singername);
- // Alert.show("歌曲名"+musicname);
- var url:String="http://202.197.108.65:8080/Flex/uploadmp3.jsp?musicname="+encodeURIComponent(music_name.text)+"&singername="+encodeURIComponent(singer_name.text);
- var request:URLRequest = new URLRequest(url);
- request.method="post";
- try
- {
- //上传文件
- myFileReference.upload(request);
- CursorManager.setBusyCursor();
- // showDetail.text = "upload";
- }catch (error:Error){
- cursorManager.removeBusyCursor();
- removeAllEvent();
- mx.controls.Alert.show("上传出错");
- }
- }
- }
- public function browse():void{
- //浏览本地本地文件
- myFileReference.browse(allowType);
- //为打开选择框定义事件处理
- myFileReference.addEventListener(Event.SELECT, selectHandler);
- //在上传过程中触发的事件处理
- myFileReference.addEventListener(ProgressEvent.PROGRESS, onProcess);
- // myFileReference.addEventListener(Event.COMPLETE, onComplete);
- //在上传过程中出错处理
- myFileReference.addEventListener(IOErrorEvent.IO_ERROR,ioErrerHandler);
- //处理返回信息
- myFileReference.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,returnmsgHandler);
- }
- //先择文件
- public function selectHandler(event:Event):void{
- musci_sName.text=myFileReference.name;
- }
- //取消文件上传
- public function uploadCancel():void{
- myFileReference.cancel();
- CursorManager.removeBusyCursor();
- removeAllEvent();
- }
- //上传过程中的事件
- public function onProcess():void{
- mx.managers.CursorManager.setBusyCursor();
- }
- import mx.managers.CursorManager;
- public function returnmsgHandler(event:DataEvent):void{
- CursorManager.removeBusyCursor();
- var result:Boolean=event.data.toString() as Boolean;
- mx.controls.Alert.show("上传成功");
- sendRequestToGetMusicInfo();
- }
- //上传过程中出错处理
- public function ioErrerHandler(event:IOErrorEvent):void{
- removeAllEvent();
- CursorManager.removeBusyCursor()
- Alert.show("上传出错","错误");
- }
- //remove all the listerners
- public function removeAllEvent():void{
- myFileReference.removeEventListener(Event.SELECT, selectHandler);
- //在上传过程中触发的事件处理
- myFileReference.removeEventListener(ProgressEvent.PROGRESS, onProcess);
- // myFileReference.removeEventListener(Event.COMPLETE, onComplete);
- myFileReference.removeEventListener(IOErrorEvent.IO_ERROR,ioErrerHandler);
- myFileReference.removeEventListener(DataEvent.UPLOAD_COMPLETE_DATA,returnmsgHandler);
- }
//上传音乐部份 public var myFileReference:FileReference = new FileReference(); public var mp3Type:FileFilter=new FileFilter("MP3(*.mp3)","*.mp3"); public var allowType:Array=new Array(mp3Type); private function uploadMP3():void{ // System.useCodePage=false;//GBK var musicname:String=music_name.text; var singername:String=singer_name.text; if(musicname==""){ mx.controls.Alert.show("音乐名称不能为空"); }else if(singername==""){ mx.controls.Alert.show("歌手不能为空"); }else if(musci_sName.text==""){ mx.controls.Alert.show("上传音乐不能为空"); }else{ // Alert.show("歌手名:"+singername); // Alert.show("歌曲名"+musicname); var url:String="http://202.197.108.65:8080/Flex/uploadmp3.jsp?musicname="+encodeURIComponent(music_name.text)+"&singername="+encodeURIComponent(singer_name.text); var request:URLRequest = new URLRequest(url); request.method="post"; try { //上传文件 myFileReference.upload(request); CursorManager.setBusyCursor(); // showDetail.text = "upload"; }catch (error:Error){ cursorManager.removeBusyCursor(); removeAllEvent(); mx.controls.Alert.show("上传出错"); } } } public function browse():void{ //浏览本地本地文件 myFileReference.browse(allowType); //为打开选择框定义事件处理 myFileReference.addEventListener(Event.SELECT, selectHandler); //在上传过程中触发的事件处理 myFileReference.addEventListener(ProgressEvent.PROGRESS, onProcess); // myFileReference.addEventListener(Event.COMPLETE, onComplete); //在上传过程中出错处理 myFileReference.addEventListener(IOErrorEvent.IO_ERROR,ioErrerHandler); //处理返回信息 myFileReference.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,returnmsgHandler); } //先择文件 public function selectHandler(event:Event):void{ musci_sName.text=myFileReference.name; } //取消文件上传 public function uploadCancel():void{ myFileReference.cancel(); CursorManager.removeBusyCursor(); removeAllEvent(); } //上传过程中的事件 public function onProcess():void{ mx.managers.CursorManager.setBusyCursor(); } import mx.managers.CursorManager; public function returnmsgHandler(event:DataEvent):void{ CursorManager.removeBusyCursor(); var result:Boolean=event.data.toString() as Boolean; mx.controls.Alert.show("上传成功"); sendRequestToGetMusicInfo(); } //上传过程中出错处理 public function ioErrerHandler(event:IOErrorEvent):void{ removeAllEvent(); CursorManager.removeBusyCursor() Alert.show("上传出错","错误"); } //remove all the listerners public function removeAllEvent():void{ myFileReference.removeEventListener(Event.SELECT, selectHandler); //在上传过程中触发的事件处理 myFileReference.removeEventListener(ProgressEvent.PROGRESS, onProcess); // myFileReference.removeEventListener(Event.COMPLETE, onComplete); myFileReference.removeEventListener(IOErrorEvent.IO_ERROR,ioErrerHandler); myFileReference.removeEventListener(DataEvent.UPLOAD_COMPLETE_DATA,returnmsgHandler); }
后代JSP中代码如下:
- String musicname = request.getParameter("musicname");
- String singername = request.getParameter("singername");
- String mp3name=new String(musicname.getBytes("ISO8859-1"),"utf-8");
- String mp3singer=new String(singername.getBytes("ISO8859-1"),"utf-8");
- System.out.println("the muicsname:---"+mp3name);
- System.out.println("the singer name:++++++++"+mp3singer);