利用arcserver 自带tomcat实现上传shapefile、cad等文件,然后用soe解析。

一、功能实现分析

1、soe中传入指定路径目录和文件名就能读取shp、cad并解析,然后返回JSON格式数据给flex端生成图形。(soe读取的是本地绝对路径)

2、所以首先要上传文件到soe发布所在的arcserver服务器。实现上传用java因此考虑用arcserver for java 自带的tomcat。

3、用arcserver自带tomcat上传要找到相应的tomcat路径并添加java包和jsp页面。

二、找到arcserver自带的tomcat并引入相应java包和jsp页面。

arcserver 9.3配置    路径:D:Program FilesArcGisjavamanagerservice omcatserverwebappsmanager  最前面的目录可能不相同

在manager下的WIN-INF/lib下加入java包 ,并把你的jsp部署在manager目录下面(我的是放在mapPortal文件夹下),记得要在manager下面加入跨域文件crossdomain.xml。

flex中上传文件的URL:http://localhost:8399/manager/mapPortal/fileUp.jsp

arcserver 10.2配置  路径:C:Program FilesArcGISServerframework untime omcatwebappsarcgis#manager     最前面的目录可能不相同 

在arcgis#manager下的WIN-INF/lib下加入java包 ,并把你的jsp部署在arcgis#manager目录下面(我的是放在mapPortal文件夹下)。跨域文件10.2已经配置好了不需要再添加。

flex中上传文件的URL:http://192.168.179.224:6080/arcgis/manager/mapPortal/fileUp.jsp

这样配置以后就可以将shp或cad文件传到发布soe的服务器上了,而且不需要额外部署一个应该服务。

jsp上传代码:

<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page import="org.apache.commons.fileupload.FileUploadException" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%
    String realPath = application.getRealPath("/");    
    //文件的保存目录
    String fileRootPath = realPath + "mapPortal\upFiles\";        
    int maxPostSize = 500 * 1024 * 1024;    
    DiskFileItemFactory factory = new DiskFileItemFactory();  
      factory.setSizeThreshold(4096);
      ServletFileUpload upload = new ServletFileUpload(factory);  
      upload.setSizeMax(maxPostSize);
      try {
          //检查上载文件的目录是否存在 
        File fileDir = new File(fileRootPath); 
        if(!fileDir.exists()){ 
            fileDir.mkdirs(); 
        }
          //读入上传的数据 
        int zipDataLength = request.getContentLength();
        //上载文件的最大字节 
        if(zipDataLength > maxPostSize){ 
            System.out.println("上传的文件字节数不可以超过500M");
            out.println("false@上传的文件字节数不可以超过500M");
            return; 
        }
        List fileItems = upload.parseRequest(request);  
        Iterator iter = fileItems.iterator();  
        while (iter.hasNext()) {  
            FileItem item = (FileItem) iter.next();  
            if (!item.isFormField()) {  
                String name = item.getName();
                //检查上载文件是否存在 
                File checkFile = new File(fileRootPath + name); 
                if(checkFile.exists()){
                    checkFile.delete();  
                    System.out.println(fileRootPath + name + " 文件已经存在,被删除"); 
                }                                        
                try {  
                    int index = name.lastIndexOf(".");
                    String extName = name.substring(index,name.length());
                    String newName = String.valueOf(System.currentTimeMillis())+extName; //用时间重命名(当前的毫秒)
                    item.write(new File(fileRootPath + newName)); 
                    out.println("success@" + fileRootPath + newName);//成功后返回路径和新的文件名
                    System.out.println("true:" + fileRootPath + newName); 
              } catch (Exception e) {  
                   e.printStackTrace();
                   out.println("false@"+e.getMessage());
                   return;  
              }  
          }  
      }  
  } catch (FileUploadException e) {  
      e.printStackTrace();  
      out.println("false@"+e.getMessage()); 
  }
%>

flex端用FileReference指向jsp所在的服务器

<?xml version="1.0" encoding="utf-8"?>
<mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" 
                xmlns:mytasks="pageaction.soe.*"
                width="290" height="120" showCloseButton="true" fontSize="12" 
                borderColor="#6489C6" borderAlpha="0.9" borderThickness="2" 
                borderThicknessBottom="2" borderThicknessLeft="2" titleIcon="@Embed(source='../images/importFile.png')"
                borderThicknessRight="2" headerHeight="22" title="导入ShapeFile或CAD文件" 
                close="this.visible=false" color="#FFFFFF" fontFamily="Microsoft YaHei UI">
    
    <mx:Script>
        <![CDATA[
            import com.esri.ags.Graphic;
            import com.esri.ags.geometry.Geometry;
            import com.esri.ags.geometry.MapPoint;
            import com.esri.ags.geometry.Polygon;
            import com.esri.ags.layers.GraphicsLayer;
            
            import mx.collections.ArrayCollection;
            import mx.controls.Alert;
            import mx.events.CloseEvent;
            import mx.managers.PopUpManager;
            import mx.rpc.AsyncResponder;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;
            import mx.rpc.http.HTTPService;
            import mx.rpc.remoting.mxml.RemoteObject;
            
            import pageaction.soe.soeEvent;
            import pageaction.soe.soeParameters;
            import pageaction.soe.soeResult;
            
            private var myFileReference:FileReference = new FileReference(); 
            
            private var filter_shp:FileFilter = new FileFilter("shapefile文件 (*.shp)", "*.shp"); 
            private var filter_dwg:FileFilter = new FileFilter("CAD文件 (*.dwg)", "*.dwg"); 
            private var filter_DWG:FileFilter = new FileFilter("CAD文件 (*.DWG)", "*.DWG"); 
            private var _soeUrl:String = "";
            
            public function get soeUrl():String{                
                return _soeUrl;
            }
            
            public function set soeUrl(value:String):void{
                _soeUrl = value;                    
            }
            
            private function soeExecute(filePath:String):void{
                var soeparameter:soeParameters = new soeParameters();
                //soeparameter.url = "http://localhost:8399/arcgis/rest/services/xz/MapServer/2/query";
                soeparameter.url = "http://192.168.179.169:6080/arcgis/rest/services/SampleWorldCities/MapServer/exts/ConvertShapeToJson/ConvertShapeToJsonOperation";
                /*soeparameter.outFields = "*";
                soeparameter.returnGeometry = true;
                var whereStr:String;
                if(this.txtWhere.text != ""){
                whereStr = this.txtWhere.text;
                }else{
                whereStr = "objectid = 780";
                }
                soeparameter.where = whereStr;*/
                //soeparameter.shapePath = "E:\ShapeTest\zz_test.shp";
                soeparameter.shapePath = filePath;
                soetask.showBusyCursor = true;
                soetask.execute(soeparameter);
            }
            
            private function compleShowSoeResult(event:soeEvent):void{
                var soeRe:soeResult = event.soeresult;
                var obj:Object = soeRe.obj;        
                var arrGraphics:Array = new Array();
                for(var i:int = 0;i<obj.geometries.length;i++){
                    var parentRing:Array = new Array();
                    var rings:Array = Array(obj.geometries[i].geometry.rings);//获取每个geometry的ring对象
                    for(var j:int=0;j<rings[0].length;j++){//每个geometry的ring组成部分
                        var partRing:Array = new Array();
                        var childRing:Array = rings[0][j];
                        for(var k:int=0;k<childRing.length;k++){//每个ring的点个数
                            var mapPoint:MapPoint = new MapPoint(childRing[k][0],childRing[k][1]);
                            partRing.push(mapPoint);
                        }
                        parentRing.push(partRing);
                    }
                    var ply:Polygon = new Polygon(parentRing);
                    var graphic:Graphic = new Graphic(ply);
                    arrGraphics.push(graphic);
                }    
                this.parentApplication.spatialAnalyByImportFileGraphic(arrGraphics);
                /*if(obj.features.length != 0){
                var temp:String = obj.features[0].attributes.KSMC;
                Alert.show(temp);
                }else{
                Alert.show("结果为空!");
                }*/    
            }
            
            private function btnSelectFileOnClick(event:Event):void{             
                myFileReference.browse([filter_shp,filter_dwg,filter_DWG]);
                myFileReference.addEventListener(Event.SELECT, onFileSelect);
                myFileReference.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, completeHandler);
                myFileReference.addEventListener(IOErrorEvent.IO_ERROR,ioerror);
                myFileReference.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            }
            
            private function onFileSelect(event:Event):void{
                var fileName:String = myFileReference.name;
                this.txtFileName.text = fileName;                                  
            }
            
            private function completeHandler(evt:DataEvent):void{
                var strResult:String = String(evt.data);
                //截掉左右空格
                strResult = strResult.replace(/^s*|s*$/g,"").split(" ").join("");
                var returnArr:Array = strResult.split("@");
                var flag:String = returnArr[0];
                if(flag == "success"){
                    //promptMessage("上传成功!",this);
                    var filePath:String = returnArr[1];
                    if(filePath != "" || filePath != null){
                        soeExecute(filePath);
                    }                    
                }else{
                    promptMessage("上传失败!",this);
                }
                this.bar.visible = false;
            }
            
            private function ioerror(event:Event):void{    
                this.bar.visible = false;
                promptMessage("IO错误!
 上传失败!",this);
                
            }
            
            private function progressHandler(event:ProgressEvent):void {
                var file:FileReference = FileReference(event.target);
                var baifenbi:Number=(event.bytesLoaded /event.bytesTotal)*100;
                bar.label=baifenbi.toString().substring(0,3)+"%";
                bar.setProgress(baifenbi,100);
                
            }
            
            private function importFile(event:Event):void{
                if (this.txtFileName.text == ""){
                    promptMessage("请选择要上传文件!",this);
                    return;
                }
                this.bar.visible = true;
                var requestUrl:String = "";
                var fileName:String = this.txtFileName.text;
                var fileType:String = fileName.substr(fileName.indexOf(".")+1,fileName.length);
                if(fileType == "shp"){
                    //requestUrl = "http://192.168.179.156:8080/mapPortal/fileUp.jsp";
                    requestUrl = "http://192.168.179.224:6080/arcgis/manager/mapPortal/fileUp.jsp";//arcserver 10.2
                    //requestUrl = "http://localhost:8399/manager/mapPortal/fileUp.jsp";////arcserver 9.3
                }else{//dwg
                    requestUrl = "http://192.168.179.156:8080/mapPortal/fileUp.jsp";
                }
                
                var request:URLRequest = new URLRequest(requestUrl);      
                try{
                    this.bar.visible = true;
                    myFileReference.upload(request);
                }catch (error:Error){ 
                    promptMessage("上传文件失败!",this);
                }        
            }
            
            private function promptMessage(msg:String, parent:DisplayObject):void{
                Alert.okLabel = "确定";
                var alert:Alert = new Alert();
                alert.setStyle("messageStyleName","AlertMessage");
                alert.setStyle("titleStyleName","AlertTitle");
                alert.title = "提示信息";
                alert.text = msg;
                PopUpManager.addPopUp(alert,parent,true);
                PopUpManager.centerPopUp(alert);                
            }     
        ]]>
    </mx:Script>
    <mytasks:soeTask id="soetask" executeComplete="compleShowSoeResult(event)"/>
    <mx:Canvas width="100%" height="100%" verticalScrollPolicy="off" horizontalScrollPolicy="off" color="#0B333C">
        <mx:Label x="3.5" y="15" text="选择导入文件:" fontWeight="bold"/>
        <mx:TextInput id="txtFileName" y="13" width="150" height="25" borderStyle="solid" right="50" editable="false" backgroundColor="#F9F9F9"/>
        <mx:Button id="btnSelectFile" y="13" label="……" width="35" click="btnSelectFileOnClick(event)" right="7" height="25"/>
        <mx:ProgressBar id="bar" labelPlacement="bottom"
                        minimum="0" visible="false" maximum="100" label="" trackHeight="15"
                        direction="right" mode="manual" width="100%" x="0" height="15" bottom="1" borderColor="#F6F7F7" themeColor="#FD250F"/>
        <mx:Button id="btnOk" x="87" y="50" label="确 定" click="importFile(event)" height="22"/>
        <mx:Button id="btnCancel" y="50" label="取 消" click="this.visible = false" height="22" right="50"/>
    </mx:Canvas>
</mx:TitleWindow>
原文地址:https://www.cnblogs.com/aegisada/p/4932304.html