我认为在 java 中获取树对象很困难。我尝试过这个。你可以用另一种方式。单击文件夹时,您将进入文件夹文件或文件夹。 这里我使用了 dropbox Api 来实现。请从中得到一个想法。试试你的想法。
treeview.html:
<!-- Nested list template -->
<script type="text/ng-template" id="items_renderer.html">
<div ui-tree-handle>
<span ng-show="item.file" class="glyphicon glyphicon-folder-close"></span>
<a ng-hide="item.file" class="btn btn-success btn-xs" data-nodrag ng-click="toggle(this);toggleMe(this)">
<span class="glyphicon glyphicon-folder-close" ng-class="{'glyphicon-chevron-right': collapsed, 'glyphicon-chevron-down': !collapsed}">
</span></a>
{{item.title}}
<a class="pull-right btn btn-danger btn-xs" data-nodrag ng-click="remove(this)"><span class="glyphicon glyphicon-remove"></span></a>
<a class="pull-right btn btn-primary btn-xs" data-nodrag ng-click="newSubItem(this)" style="margin-right: 8px;"><span class="glyphicon glyphicon-plus"></span></a>
</div>
<ol ui-tree-nodes="options" ng-model="item.items" ng-class="{hidden: collapsed}">
<li ng-repeat="item in item.items" collapsed="true" ui-tree-node ng-include="'items_renderer.html'">
</li>
</ol>
</script>
<div ui-tree="options">
<ol ui-tree-nodes ng-model="list" >
<li ng-repeat="item in list track by $index" ui-tree-node collapsed="true" ng-include="'items_renderer.html'"></li>
</ol>
</div>
</div>
</div>
</div>
</div>
</div>
treecontroller.js:
app.controller('treeController',['$scope', '$http','fileUpload', function($scope, $http,fileUpload) {
console.log("$$$ tree controller has been initialized $$$")
/** To get list of files from the dropbox */
var files = [];
$scope.list = [];
var foldername = '';
/*** we call dropbox cloud when the user wants and we get files and
* folders for an initialization*/
$scope.getListOfFiles = function() {
var foldername = "/";
$http.get("http://localhost:8080/dropbox_api_.10/dropbox/getListOfFiles/?folderPath=" + foldername)
.success(function(data, status) {
console.log("List of files from dropbox folder &&&&", angular.toJson(data));
$scope.list = data;
}).error(function(data, status, headers, config) {
alert('error');
});
}
var folders = [];
var buildFloderPath = function(scope) {
if (scope.$parentNodeScope != null) {
folders.push(scope.$parentNodeScope.$modelValue.title);
buildFloderPath(scope.$parentNodeScope);
}
};
/** When we call, we expand tree here and clear when collapse tree*/
$scope.toggleMe = function(scope) {
folders = [];
foldername="";
if (!scope.collapsed) {
var nodeData = scope.$modelValue;
folders.push(nodeData.title);
buildFloderPath(scope);
console.log(angular.toJson(folders));
for (var i = folders.length - 1; i >= 0; i--) {
foldername += "/" + folders[i];
}
/***/
//continueFileUploading(foldername);
$http.get("http://localhost:8080/dropbox_api_.10/dropbox/getListOfFiles/?folderPath=" + foldername)
.success(function(data, status) {
console.log(" @@@@ Selected path @@@",foldername);
console.log("List of files from dropbox folder &&&&", angular.toJson(data));
for (var i = 0; i < data.length; i++) {
nodeData.items.push(data[i]);
}
}).error(function(data, status, headers, config) {
alert('error');
});
}
else{
var nodeData = scope.$modelValue;
nodeData.items = [];
}
};
$scope.getListOfFiles();
/*****************/
$scope.remove = function(scope) {
scope.remove();
};
$scope.toggle = function(scope) {
scope.toggle();
};
$scope.newSubItem = function(scope) {
var nodeData = scope.$modelValue;
nodeData.items.push({
id : nodeData.id * 10 + nodeData.items.length,
title : nodeData.title + '.' + (nodeData.items.length + 1),
items : []
});
};
}]);
java:
public ArrayList<String> listDropboxFolders(@RequestParam("folderPath") String folderPath) throws DbxException {
ArrayList<String> fileList=new ArrayList<String>();
for (DbxEntry child : listing.children) {
//File child;
if(child.isFolder()){
fileList.add(child.name);
System.out.println("file is theere"+child.name);
}else{
System.out.println("file name **"+child.name);
}
child.toString());
}
return fileList;
}