1

如何通过 ftp 使用跨域?

我正在尝试在 Flex 中对 FTP 进行“hello world”级别测试,但是三天以来,我无法克服如何强制 flex 接受我的跨域策略的问题——即使是出于测试目的。这是我的代码:下面是确切的错误文本。

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" initialize="onInitialize()" layout="vertical">

<mx:Script>
<![CDATA[
  import mx.utils.*;
  import mx.controls.Alert;
  private var fileRef:FileReference;
  private var fileSize:uint;
  private var fileContents:ByteArray;
  //you need to initiate two scokets one for sending
  //commands and second for sending data to FTP Server
  //socket for sending commands to FTP
  private var s:Socket
  //responce from FTP
  private var ftpResponce:String;
  //socket for sending Data to FTP
  private var dataChannelSocket:Socket;
  //responce from FTP when sending Data to FTP
  private var dataResponce:String;
  //will hold the IP address of new socket created by FTP
  private var dataChannelIP:String;
  //will hold the Port number created by FTP
  private var dataChannelPort:int;
  private var user:String="I have the right user"; //FTP usernae
  private var pass:String="the pw is correct"; //FTP Password

  private function receiveReply(e:ProgressEvent):void {
    ftpResponce=s.readUTFBytes(s.bytesAvailable)
    var serverResponse:Number=Number(ftpResponce.substr(0, 3));
    if (ftpResponce.indexOf('227') > -1) {
      //get the ip from the string response
      var temp:Object=ftpResponce.substring(ftpResponce.indexOf("(") + 1
        , ftpResponce.indexOf(")"));
      var dataChannelSocket_temp:Object=temp.split(",");
      dataChannelIP=dataChannelSocket_temp.slice(0, 4).join(".");
      dataChannelPort=parseInt(dataChannelSocket_temp[4]) * 256 +
        int(dataChannelSocket_temp[5]);
      //create new Data Socket based on dataChannelSocket and dataChannelSocket port
      dataChannelSocket=new Socket(dataChannelIP, dataChannelPort);
      dataChannelSocket.addEventListener(ProgressEvent.SOCKET_DATA, receiveData);
    }
    //few FTP Responce Codes
    switch (String(serverResponse)) {
      case "220":
        //FTP Server ready responce
        break;
      case "331":
        //User name okay, need password
        break;
      case "230":
        //User  logged in
        break;
      case "250":
        //CWD command successful
        break;
      case "227":
        //Entering Passive Mode (h1,h2,h3,h4,p1,p2).
        break;
      default:
    }
    //for more please
    //http://http://www.altools.com/image/support/alftp/ALFTP_35_help/
    //FTP_response_codes_rfc_959_messages.htm          
    traceData(ftpResponce);
  }

  private function receiveData(e:ProgressEvent):void {
    dataResponce=dataChannelSocket.readUTFBytes(
      dataChannelSocket.bytesAvailable);
    traceData("dataChannelSocket_response—&gt;" + dataResponce);
  }

  private function showError(e:IOErrorEvent):void {
    traceData("Error—&gt;" + e.text);
  }

  private function showSecError(e:SecurityErrorEvent):void {
    traceData("SecurityError–&gt;" + e.text);
  }

  private function onInitialize():void {
    Security.loadPolicyFile("http://www.myUrlIsCorrectInMyProgram.com/crossdomain.xml");
  }

  private function createRemoteFile(fileName:String):void {
    if (fileName != null && fileName != "") {
      s.writeUTFBytes("STOR " + fileName + "\n");
      s.flush();
    }
  }

  private function sendData():void {
    fileContents=fileRef.data as ByteArray;
    fileSize=fileRef.size;
    dataChannelSocket.writeBytes(fileContents, 0, fileSize);
    dataChannelSocket.flush();
  }

  //initialize when application load
  private function upLoad():void {
    fileRef=new FileReference();
    //some eventlistener
    fileRef.addEventListener(Event.SELECT, selectEvent);
    fileRef.addEventListener(Event.OPEN, onFileOpen);
    //this function connects to the ftp server
    connect();
    //send the usernae and password
    this.userName(user);
    this.passWord(pass);
    //if you want to change the directory for upload file
    this.changeDirectory("/test/"); //directory name
    //enter into PASSV Mode
    s.writeUTFBytes("PASV\n");
    s.flush();
  }

  private function onFileOpen(event:Event):void {
  }

  private function traceData(event:Object):void {
    var tmp:String="================================\n";
    ta.text+=event.toString() + "\n";
    ta.verticalScrollPosition+=20;
  }

  private function ioErrorEvent(event:IOErrorEvent):void {
    Alert.show("IOError:" + event.text);
  }

  private function selectEvent(event:Event):void {
    btn_upload.enabled=true;
    filename.text=fileRef.name;
    fileRef.load();
  }

  private function uploadFile():void {
    createRemoteFile(fileRef.name);
    sendData();
  }

  private function connect():void {
    s=new Socket("ftp.myUrlIsCorrectInMyProgram.com", 21);
    s.addEventListener(ProgressEvent.SOCKET_DATA, receiveReply);
    s.addEventListener(IOErrorEvent.IO_ERROR, showError);
    s.addEventListener(SecurityErrorEvent.SECURITY_ERROR, showSecError);
    s.addEventListener(Event.CONNECT, onSocketConnect);
    s.addEventListener(Event.CLOSE, onSocketClose);
    s.addEventListener(Event.ACTIVATE, onSocketAtivate);
  }

  private function onSocketConnect(evt:Event):void {
    //traceData("OnSocketConnect–&gt;"+evt.target.toString());
  }

  private function onSocketClose(evt:Event):void {
    //traceData("onSocketClose–&gt;"+evt.target.toString());
  }

  private function onSocketAtivate(evt:Event):void {
    //traceData("onSocketAtivate–&gt;"+evt.target.toString());
  }

  private function userName(str:String):void {
    sendCommand("USER " + str);
  }

  private function passWord(str:String):void {
    sendCommand("PASS " + str);
  }

  private function changeDirectory(str:String):void {
    sendCommand("CWD " + str);
  }

  private function sendCommand(arg:String):void {
    arg+="\n";
    s.writeUTFBytes(arg);
    s.flush();
  }
]]>

[SWF] /FTP-debug/FTP.swf - 解压后 739,099 字节 警告:域 www.myUrlIsCorrectInMyProgram.com 未指定元策略。应用默认元策略“仅限主”。此配置已弃用。请参阅http://www.adobe.com/go/strict_policy_files以解决此问题。

警告:等待套接字策略文件时 xmlsocket://ftp.myUrlIsCorrectInMyProgram.com:843(3 秒)超时。这应该不会导致任何问题,但请参阅http://www.adobe.com/go/strict_policy_files以获得解释。

警告:[严格] 由于语法不正确,忽略 xmlsocket://ftp.myUrlIsCorrectInMyProgram.com:21 中的策略文件。请参阅http://www.adobe.com/go/strict_policy_files以解决此问题。

* 违反安全沙箱 * 与 ftp.myUrlIsCorrectInMyProgram.com:21 的连接已停止 - 不允许来自错误:由于缺少策略文件权限http://localhost/FTP-debug/FTP.swf ,请求者在 xmlsocket://ftp.myUrlIsCorrectInMyProgram.com:21 处的资源请求被拒绝。http://localhost/FTP-debug/FTP.swf

上面列出的 URL 中的“信息”对我来说绝对无法理解。

请有人帮忙!

4

3 回答 3

1

我也遇到了同样的问题,但能够使用我从http://www.flash-resources.net/download.html下载的 flash 策略服务器来修复它。我在安装了我的 tomcat 服务器并调用 Security.loadPolicyFile("xmlsocket://:843"); 的同一台机器上运行它。从应用程序,它工作得很好。没有错误。

于 2011-07-19T15:11:08.867 回答
1

我也有同样的问题,但能够使用我从这里下载的 flash 策略服务器来修复它。我在安装了我的 tomcat 服务器并调用 Security.loadPolicyFile("xmlsocket://Machine Name:843"); 的同一台机器上运行它。从应用程序,它工作得很好。没有错误。

请注意上一篇文章中机器名称周围的拼写错误。

于 2011-07-19T15:14:10.210 回答
0

请参阅跨域规范:

http://learn.adobe.com/wiki/download/attachments/64389123/CrossDomain_PolicyFile_Specification.pdf?version=1

这涵盖了您收到的警告,并且可以帮助您完成这项工作。

于 2010-02-18T01:36:40.253 回答