转 ExtJS 文件浏览器,可以选择文件和文件夹

话说long long ago,在本人开发项目时,需要导入一个文件夹(目录)下的文件,通过解析其中的数据并入库。

选择一个文件目录,好像没有这个控件。开始想到了不选目录,选文件。但是要选多个文件哦,而且数目不固定。

用file文件浏览不好,想到了用swfUpload可以选择多个文件。可以做到,但是还是选择文件不是选择目录。

不过我想要的,想呀想的……

诶~可以用ExtJS,自己扩展一个还是可以的。于是就有了今天这篇文章和这个文件浏览器。

 

extFileBrowser.html

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  2. <html>  
  3.   <head>  
  4.     <title>Ext 文件浏览选择器</title>  
  5.       
  6.     <meta http-equiv="author" content="hoojo">  
  7.     <meta http-equiv="email" content="hoojo_@126.com">  
  8.     <meta http-equiv="blog" content="http://blog.csdn.net/IBM_hoojo">  
  9.     <meta http-equiv="ext-lib" content="v2.2.1">  
  10.     <meta http-equiv="version" content="v1.0">  
  11.     <meta http-equiv="content-type" content="text/html; charset=gbk">  
  12.     <link rel="stylesheet" type="text/css" href="ext-2.2/resources/css/ext-all.css" />  
  13.     <script type="text/javascript" src="ext-2.2/adapter/ext/ext-base.js"></script>  
  14.     <script type="text/javascript" src="ext-2.2/ext-all.js"></script>  
  15.     <script type="text/javascript" src="Ext.hoo.component.FileBrowserComponent.js"></script>  
  16.     <script type="text/javascript">  
  17.         Ext.onReady(function(){  
  18.             Ext.BLANK_IMAGE_URL = "ext-2.2/resources/images/default/s.gif";  
  19.             var fileBrowser = new Ext.hoo.component.FileBrowserWindow();  
  20.             //var fileBrowser = new Ext.hoo.component.FileBrowserPanel();  
  21.             fileBrowser.show();  
  22.             fileBrowser.tree.getSelectionModel().on("beforeselect", function (sm, node) {  
  23.                 //只能选择文件夹,如果要选择文件修改这里即可  
  24.                 var flag = ((!node || (!!node && !!node.leaf)) || !(node.attributes.path.indexOf(":") != -1)) ? true : false;  
  25.                 fileBrowser.buttons[0].setDisabled(flag);  
  26.                 fileBrowser.buttons[1].setDisabled(flag);  
  27.             }, fileBrowser.tree);  
  28.         });  
  29.     </script>  
  30.   </head>  
  31.   <body>  
  32.   </body>  
  33. </html>  

 

Ext.hoo.component.FileBrowserComponent.js

  1. /** 
  2.  * Ext.hoo.component.FileBrowserWindow 系统文件浏览选择组件,可以选定电脑上的文件或文件夹 
  3.  * @author: hoojo 
  4.  * @createDate 2010-10-17 
  5.  * @email: hoojo_@126.com 
  6.  * @blog: http://blog.csdn.net/IBM_hoojo 
  7.  * @ext_lib: v2.2 
  8.  * @version 1.0  
  9.  */  
  10. Ext.ns("Ext.hoo.component");  
  11. Ext.hoo.component.FileBrowserWindow = Ext.extend(Ext.Window, {  
  12.     constructor: function (config) {  
  13.         config = config || {};  
  14.         Ext.apply(this, config);  
  15.         this.tree = new Ext.hoo.tree.FileSystemTree();  
  16.         Ext.hoo.component.FileBrowserWindow.superclass.constructor.call(this, {  
  17.             renderTo: Ext.getBody(),  
  18.             width: 300,  
  19.             height: 300,  
  20.             frame: true,  
  21.             layout: "fit",  
  22.             border: false,  
  23.             title: "请选择",  
  24.             items: this.tree,  
  25.             buttons: [{  
  26.                 text: "新建",  
  27.                 disabled: true,  
  28.                 handler: this.onNewHandler,  
  29.                 scope: this  
  30.             }, {  
  31.                 text: "确定",  
  32.                 disabled: true,  
  33.                 handler: this.onOkHandler,  
  34.                 scope: this  
  35.             }, {  
  36.                 text: "取消",  
  37.                 handler: function () {  
  38.                     this.hide(Ext.getBody());  
  39.                 },  
  40.                 scope: this  
  41.             }]  
  42.         });  
  43.     },  
  44.     onNewHandler: function () {  
  45.         this.setPath();  
  46.         this.setFile();  
  47.         Ext.Msg.prompt("新建文件""请输入文件夹名称"this.onCreateDir, this);  
  48.     },  
  49.     onOkHandler: function () {  
  50.         this.setPath();  
  51.         this.setFile();  
  52.         Ext.Msg.alert("路径"this.getPath());  
  53.     },   
  54.     onCreateDir: function (btn, text) {  
  55.         if (btn == "ok") {  
  56.             var path = this.getPath();  
  57.             var node = this.getFile();  
  58.             var dirName = text;  
  59.             if (!!path && !!dirName) {  
  60.                 //本地添加模式  
  61.                 /*var newNode = new Ext.tree.AsyncTreeNode({ 
  62.                     text: dirName, 
  63.                     path: node.attributes.path + "/" + dirName 
  64.                 }); 
  65.                 node.expand(true, true); 
  66.                 node.appendChild(newNode);*/  
  67.                 //远程加载模式  
  68.                 Ext.Ajax.request({  
  69.                     url: Ext.hoo.tree.FileSystemTree.TREE_CREATE_DIR_URL,  
  70.                     params: {path: encodeURIComponent(path), dirName: encodeURIComponent(dirName)},//处理中文文件名,乱码问题  
  71.                     success: function (response, options) {  
  72.                         var returnNnode = Ext.decode(response.responseText);  
  73.                         node.appendChild(returnNnode);  
  74.                         node.expand(true);  
  75.                     },  
  76.                     failure: function (response) {  
  77.                         Ext.Msg.alert("程序异常", response.responseText);  
  78.                     }  
  79.                 });  
  80.             }  
  81.         }  
  82.     },  
  83.     setPath: function () {  
  84.         this.path = this.tree.getSelectedNode().attributes.path || "";  
  85.     },  
  86.     setFile: function () {  
  87.         this.nodeFile = this.tree.getSelectedNode() || {};  
  88.     },  
  89.     getPath: function () {  
  90.         return this.path;     
  91.     },  
  92.     getFile: function () {  
  93.         return this.nodeFile;  
  94.     }  
  95. });  
  96.   
  97. /** 
  98.  * Ext.hoo.component.FileBrowserPanel 系统文件浏览选择组件,可以选定电脑上的文件或文件夹 
  99.  * 不同于上面的是,这里是一个panel。有时候弹出window,并不能达到预想的效果。特别是window弹出在 
  100.  * iframe中的Object对象上面,如:在播放器上面弹出此组件,拖动windwo的效果不理想。 
  101.  * 这时就需要用模态,模态嵌入FileBrowserPanel组件即可 
  102.  * @author: hoojo 
  103.  * @createDate 2010-10-17 
  104.  * @email: hoojo_@126.com 
  105.  * @blog: http://blog.csdn.net/IBM_hoojo 
  106.  * @ext_lib: v2.2 
  107.  * @version 1.0  
  108.  */  
  109. Ext.hoo.component.FileBrowserPanel = Ext.extend(Ext.Panel, {  
  110.     constructor: function (config) {  
  111.         config = config || {};  
  112.         Ext.apply(this, config);  
  113.         this.tree = new Ext.hoo.tree.FileSystemTree();  
  114.         Ext.hoo.component.FileBrowserPanel.superclass.constructor.call(this, {  
  115.             renderTo: Ext.getBody(),  
  116.             border: false,  
  117.             width: 300,  
  118.             height: 400,  
  119.             layout: "fit",  
  120.             title: "请选择",  
  121.             items: this.tree,  
  122.             buttons: [{  
  123.                 text: "新建",  
  124.                 disabled: true,  
  125.                 handler: this.onNewHandler,  
  126.                 scope: this  
  127.             }, {  
  128.                 text: "确定",  
  129.                 disabled: true,  
  130.                 handler: function () {  
  131.                     this.path = this.tree.getSelectedNode().attributes.path || "";  
  132.                     this.nodeFile = this.tree.getSelectedNode() || {};  
  133.                     //window.returnValue = this.path;  
  134.                     //window.close();  
  135.                     Ext.Msg.alert("路径"this.path);  
  136.                 },  
  137.                 scope: this  
  138.             }, {  
  139.                 text: "取消",  
  140.                 handler: function () {  
  141.                     this.hide(Ext.getBody());  
  142.                     //window.close();  
  143.                 },  
  144.                 scope: this  
  145.             }]  
  146.         });  
  147.     },  
  148.     onNewHandler: function () {  
  149.         this.setPath();  
  150.         this.setFile();  
  151.         Ext.Msg.prompt("新建文件""请输入文件夹名称"this.onCreateDir, this);  
  152.     },  
  153.     onCreateDir: function (btn, text) {  
  154.         if (btn == "ok") {  
  155.             var path = this.getPath();  
  156.             var node = this.getFile();  
  157.             var dirName = text;  
  158.             if (!!path && !!dirName) {  
  159.                 //本地添加模式  
  160.                 /*var newNode = new Ext.tree.AsyncTreeNode({ 
  161.                     text: dirName, 
  162.                     path: node.attributes.path + "/" + dirName 
  163.                 }); 
  164.                 node.expand(true, true); 
  165.                 node.appendChild(newNode);*/  
  166.                 //远程加载模式  
  167.                 Ext.Ajax.request({  
  168.                     url: Ext.hoo.tree.FileSystemTree.TREE_CREATE_DIR_URL,  
  169.                     params: {path: encodeURIComponent(path), dirName: encodeURIComponent(dirName)},//处理中文文件名,乱码问题  
  170.                     success: function (response, options) {  
  171.                         var returnNnode = Ext.decode(response.responseText);  
  172.                         node.appendChild(returnNnode);  
  173.                         node.expand(truetrue);  
  174.                     },  
  175.                     failure: function (response) {  
  176.                         Ext.Msg.alert("程序异常", response.responseText);  
  177.                     }  
  178.                 });  
  179.             }  
  180.         }  
  181.     },  
  182.     setPath: function () {  
  183.         this.path = this.tree.getSelectedNode().attributes.path || "";  
  184.     },  
  185.     setFile: function () {  
  186.         this.nodeFile = this.tree.getSelectedNode() || {};  
  187.     },  
  188.     getPath: function () {  
  189.         return this.path;     
  190.     },  
  191.     getFile: function () {  
  192.         return this.nodeFile;  
  193.     }  
  194. });  
  195.   
  196. /** 
  197.  * Ext.hoo.tree.FileSystemTree 系统文件树,显示所有的文件 
  198.  * @author: hoojo 
  199.  * @createDate 2010-10-17 
  200.  * @email: hoojo_@126.com 
  201.  * @blog: http://blog.csdn.net/IBM_hoojo 
  202.  * @ext_lib: v2.2 
  203.  * @version 1.0  
  204.  */  
  205. Ext.ns("Ext.hoo.tree");  
  206. Ext.hoo.tree.FileSystemTree = Ext.extend(Ext.tree.TreePanel, {  
  207.     constructor: function () {        
  208.         Ext.hoo.tree.FileSystemTree.superclass.constructor.call(this, {  
  209.             //rootVisible: false,  
  210.             autoScroll: true,  
  211.             root: new Ext.tree.AsyncTreeNode({  
  212.                 text: "My System Files",  
  213.                 id: "0",  
  214.                 path: "root",  
  215.                 children:[]  
  216.             }),  
  217.             listeners: {  
  218.                 expandnode: {  
  219.                     fn: this.onExpandNode,  
  220.                     scope: this  
  221.                 }  
  222.             }  
  223.         });  
  224.     },  
  225.     onExpandNode: function (node) {  
  226.         //只对未加载过的添加子结点,加载后不在重复加载;避免增加请求,浪费资源  
  227.         if (!node.attributes.isLoad) {  
  228.             Ext.Ajax.request({  
  229.                 url: Ext.hoo.tree.FileSystemTree.TREE_DATA_URL,  
  230.                 params: {path: encodeURIComponent(node.attributes.path)},//处理中文文件名,乱码问题  
  231.                 success: function (response, options) {  
  232.                     node.attributes.isLoad = true;//设置加载标示  
  233.                     var nodes = Ext.decode(response.responseText);  
  234.                     node.appendChild(nodes);  
  235.                 },  
  236.                 failure: function (response) {  
  237.                     Ext.Msg.alert("程序异常", response.responseText);  
  238.                 }  
  239.             });  
  240.         }  
  241.     },   
  242.     getSelectedNode: function () {  
  243.         return this.getSelectionModel().getSelectedNode();  
  244.     }  
  245. });  
  246. Ext.hoo.tree.FileSystemTree.TREE_CREATE_DIR_URL = "http://localhost:8080/Test/FileBrowser?method=mkDir";  
  247. Ext.hoo.tree.FileSystemTree.TREE_DATA_URL = "http://localhost:8080/Test/FileBrowser?method=getData";  

 

服务器端java code:

FileBrowser Servlet:

  1. package com.hoo.servlet;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.io.PrintWriter;  
  6. import java.net.URLDecoder;  
  7.   
  8. import javax.servlet.ServletException;  
  9. import javax.servlet.http.HttpServlet;  
  10. import javax.servlet.http.HttpServletRequest;  
  11. import javax.servlet.http.HttpServletResponse;  
  12. import javax.swing.filechooser.FileSystemView;  
  13.   
  14. import net.sf.json.JSONArray;  
  15.   
  16. import com.hoo.entity.FileInfo;  
  17. import com.hoo.util.FileUtils;  
  18.   
  19. /** 
  20.  * <b>function:</b> 查询本地硬盘文件数据、创建目录 
  21.  * @project Test 
  22.  * @package com.hoo.servlet  
  23.  * @fileName FileBrowser.java 
  24.  * @author hoojo 
  25.  */  
  26. public class FileBrowser extends HttpServlet {  
  27.     private static final long serialVersionUID = 1599390137455995515L;  
  28.   
  29.     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  30.         response.setContentType("text/html");  
  31.         response.setCharacterEncoding("UTF-8");  
  32.         PrintWriter out = response.getWriter();  
  33.           
  34.         String path = request.getParameter("path");  
  35.         path = path == null ? "" : URLDecoder.decode(path, "UTF-8");  
  36.         String method = request.getParameter("method");  
  37.         FileInfo info = new FileInfo();  
  38.         if ("getData".equals(method)) {  
  39.             if ("root".equals(path)) {            
  40.                 FileSystemView fsv = FileSystemView.getFileSystemView();  
  41.                 File[] roots = fsv.getRoots(); //File.listRoots();  
  42.                 //桌面  
  43.                 for (File f : roots) {  
  44.                     info.getChildren().add(FileUtils.getFileInfo(f));  
  45.                 }  
  46.                 for (File f : roots[0].listFiles()) {  
  47.                     if (f.getName().contains("My Documents")) {  
  48.                         info.getChildren().add(FileUtils.getFileInfo(f));  
  49.                     }  
  50.                 }  
  51.                 FileInfo fileInfo = new FileInfo();  
  52.                 fileInfo.setName("我的电脑");  
  53.                 fileInfo.setPath("My Computer");  
  54.                 for (File fi : roots[0].listFiles()[0].listFiles()) {  
  55.                     fileInfo.getChildren().add(FileUtils.getFileInfo(fi));  
  56.                 }  
  57.                 info.getChildren().add(fileInfo);  
  58.                 fileInfo = new FileInfo();  
  59.                 fileInfo.setName("网上邻居");  
  60.                 fileInfo.setPath("Network Place");  
  61.                 for (File fi : roots[0].listFiles()[1].listFiles()) {  
  62.                     fileInfo.getChildren().add(FileUtils.getFileInfo(fi));  
  63.                 }  
  64.                 info.getChildren().add(fileInfo);  
  65.                   
  66.                 out.print(JSONArray.fromObject(info.getChildren()).toString());  
  67.             } else if (path != null && !"".equals(path)) {  
  68.                 FileUtils.getFileInfo(info, new File(path), new String[] {"*"});  
  69.                 out.print(JSONArray.fromObject(info.getChildren()).toString());  
  70.             }   
  71.         }  
  72.         if ("mkDir".equals(method)) {  
  73.             String dirName = request.getParameter("dirName");  
  74.             dirName = dirName == null ? "" : URLDecoder.decode(dirName, "UTF-8");  
  75.             boolean success = false;  
  76.             try {  
  77.                 success = FileUtils.mkDir(path, dirName);  
  78.                 FileInfo node = FileUtils.getFileInfo(new File(FileUtils.getDoPath(path) + dirName));  
  79.                 out.print(JSONArray.fromObject(node));  
  80.             } catch (Exception e) {  
  81.                 e.printStackTrace();  
  82.                 success = false;  
  83.             }  
  84.             System.out.println(success);  
  85.         }  
  86.         out.flush();  
  87.         out.close();  
  88.     }  
  89.   
  90.     public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  91.         this.doGet(request, response);  
  92.     }  
  93. }  

这个类用到了json-lib.jar工具包,此包可以帮我们把java对象,包括list、map、array序列化成json的字符串。

至少用到以下依赖包:

 

 

FileInfo 封装文件信息的java Bean:

  1. package com.hoo.entity;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. /** 
  7.  * <b>function:</b>文件信息 
  8.  * @author hoojo 
  9.  * @createDate Oct 10, 2010 9:53:51 PM 
  10.  * @file FileInfo.java 
  11.  * @package com.hoo.entity 
  12.  * @project MultiUpload 
  13.  * @blog http://blog.csdn.net/IBM_hoojo 
  14.  * @email hoojo_@126.com 
  15.  * @version 1.0 
  16.  */  
  17. public class FileInfo {  
  18.     //文件id  
  19.     private String id;  
  20.     //文件名称  
  21.     private String name;  
  22.     private String text;  
  23.     //文件路径  
  24.     private String path;  
  25.     //是否有目录,有无子节点  
  26.     private boolean leaf;  
  27.     //修改日期  
  28.     private String editDate;  
  29.     //后缀  
  30.     private String suffix;  
  31.     //长度  
  32.     private long length;  
  33.     // 子目录中所有文件  
  34.     private List<FileInfo> children = new ArrayList<FileInfo>();  
  35.     //setter、getter   
  36.     public String toString() {  
  37.         return "name:" + name + ", size:" + children.size();  
  38.     }  
  39. }  

 

FileUtils 文件操作工具类

  1. package com.hoo.util;  
  2.   
  3. import java.io.File;  
  4. import java.text.SimpleDateFormat;  
  5. import java.util.Date;  
  6. import java.util.UUID;  
  7. import com.hoo.entity.FileInfo;  
  8.   
  9. /** 
  10.  * <b>function:</b> 磁盘文件操作工具类 
  11.  * @project Test 
  12.  * @package com.hoo.util  
  13.  * @fileName FileUtils.java 
  14.  * @createDate 2010-10-4 下午03:32:42 
  15.  * @author hoojo 
  16.  */  
  17. @SuppressWarnings("unused")  
  18. public abstract class FileUtils {  
  19.       
  20.     /** 
  21.      * <b>function:</b>传递一个File,返回该文件的FileInfo实体类 
  22.      * @author hoojo 
  23.      * @createDate Oct 10, 2010 10:10:19 PM 
  24.      * @param file File 
  25.      * @return FileInfo 
  26.      */  
  27.     public static FileInfo getFileInfo(File file) {  
  28.         FileInfo info = new FileInfo();  
  29.         if (file != null) {  
  30.             info.setId(UUID.randomUUID().toString());  
  31.             if (file.getName() == null || "".equals(file.getName()) || "::".equals(file.getName())) {  
  32.                 info.setName(file.getAbsolutePath());  
  33.             } else {  
  34.                 info.setName(file.getName());  
  35.             }  
  36.             //info.setLeaf(file.isFile());  
  37.             info.setLeaf(!file.isDirectory());  
  38.             info.setLength(file.length());  
  39.             info.setPath(getDoPath(file.getAbsolutePath()));  
  40.             info.setSuffix(getType(file.getName()));  
  41.             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  42.             Date date = new Date();  
  43.             date.setTime(file.lastModified());  
  44.             info.setEditDate(sdf.format(date));  
  45.         }  
  46.         return info;  
  47.     }  
  48.       
  49.     public static void setFileInfo(File file, FileInfo info) {  
  50.         if (file != null && info != null) {  
  51.             info.setId(UUID.randomUUID().toString());  
  52.             if (file.getName() == null || "".equals(file.getName()) || "::".equals(file.getName())) {  
  53.                 info.setName(file.getAbsolutePath());  
  54.             } else {  
  55.                 info.setName(file.getName());  
  56.             }  
  57.             //info.setLeaf(file.isFile());  
  58.             info.setLeaf(!file.isDirectory());  
  59.             info.setLength(file.length());  
  60.             info.setPath(getDoPath(file.getAbsolutePath()));  
  61.             info.setSuffix(getType(file.getName()));  
  62.             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  63.             Date date = new Date();  
  64.             date.setTime(file.lastModified());  
  65.             info.setEditDate(sdf.format(date));  
  66.         }  
  67.     }  
  68.       
  69.     /** 
  70.      * <b>function:</b>处理后的系统文件路径 
  71.      * @author hoojo 
  72.      * @createDate Oct 10, 2010 12:49:31 AM 
  73.      * @param path 文件路径 
  74.      * @return 返回处理后的路径 
  75.      */  
  76.     public static String getDoPath(String path) {  
  77.         path = path.replace("//""/");  
  78.         String lastChar = path.substring(path.length() - 1);  
  79.         if (!"/".equals(lastChar)) {  
  80.             path += "/";  
  81.         }  
  82.         return path;  
  83.     }  
  84.       
  85.     /** 
  86.      * <b>function:</b>和文件后缀一样,不同的是没有“.” 
  87.      * @author hoojo 
  88.      * @createDate Oct 10, 2010 2:42:43 PM 
  89.      * @param fileName 文件名称 
  90.      * @return 
  91.      */  
  92.     public static String getType(String fileName) {  
  93.      int index = fileName.lastIndexOf(".");  
  94.         if (index != -1) {  
  95.             String suffix = fileName.substring(index + 1);//后缀  
  96.             return suffix;   
  97.         } else {  
  98.             return null;  
  99.         }  
  100.     }  
  101.       
  102.     /** 
  103.      * <b>function:</b> 得到指定目录下所有的文件集合 
  104.      * @createDate 2010-10-20 下午02:20:06 
  105.      * @author hoojo 
  106.      * @param info 将数据设置在该变量中 
  107.      * @param file 文件目录 
  108.      */  
  109.     public static void getAllFileInfo(FileInfo info, File file) {  
  110.         if (file.isDirectory()) {  
  111.             long size = 0;  
  112.             File[] allFiles = file.listFiles();  
  113.             for (File f : allFiles) {  
  114.                 size += f.length();  
  115.                 FileInfo fi = getFileInfo(f);  
  116.                 info.getChildren().add(fi);  
  117.                 getAllFileInfo(fi, f);  
  118.             }  
  119.             info.setLength(size);  
  120.         }  
  121.     }  
  122.       
  123.     /** 
  124.      * <b>function:</b> 得到当前目录所有文件 
  125.      * @createDate 2010-10-20 下午02:21:06 
  126.      * @author hoojo 
  127.      * @param info 文件对象 
  128.      * @param file 目录 
  129.      */  
  130.     public static void getFileInfo(FileInfo info, File file, String[] allowTypes) {  
  131.         if (file.isDirectory()) {  
  132.             long size = 0;  
  133.             File[] allFiles = file.listFiles();  
  134.             for (File f : allFiles) {  
  135.                 size += f.length();  
  136.                 FileInfo fi = getFileInfo(f);  
  137.                 if (f.isDirectory()) {  
  138.                     info.getChildren().add(fi);  
  139.                 } else {  
  140.                     if (validTypeByName(f.getName(), allowTypes, true)) {  
  141.                         info.getChildren().add(fi);  
  142.                     }  
  143.                 }  
  144.             }  
  145.             info.setLength(size);  
  146.         }  
  147.     }  
  148.       
  149.     /** 
  150.      * <b>function:</b> 根据文件名和类型数组验证文件类型是否合法,flag是否忽略大小写 
  151.      * @author hoojo 
  152.      * @createDate Oct 10, 2010 11:54:54 AM 
  153.      * @param fileName 文件名 
  154.      * @param allowTypes 类型数组 
  155.      * @param flag 是否获得大小写 
  156.      * @return 是否验证通过 
  157.      */  
  158.     public static boolean validTypeByName(String fileName, String[] allowTypes, boolean flag) {  
  159.         String suffix = getType(fileName);  
  160.         boolean valid = false;  
  161.         if (allowTypes.length > 0 && "*".equals(allowTypes[0])) {  
  162.             valid = true;  
  163.         } else {  
  164.             for (String type : allowTypes) {  
  165.                 if (flag) {//不区分大小写后缀  
  166.                     if (suffix != null && suffix.equalsIgnoreCase(type)) {  
  167.                         valid = true;  
  168.                         break;  
  169.                     }  
  170.                 } else {//严格区分大小写  
  171.                     if (suffix != null && suffix.equals(type)) {  
  172.                         valid = true;  
  173.                         break;  
  174.                     }  
  175.                 }  
  176.             }  
  177.         }  
  178.         return valid;  
  179.     }  
  180.       
  181.     /** 
  182.      * <b>function:</b> 在path目录下创建目录 
  183.      * @createDate 2010-11-3 下午04:03:34 
  184.      * @author hoojo 
  185.      * @param path 
  186.      * @param dirName 
  187.      * @return 
  188.      */  
  189.     public static boolean mkDir(String path, String dirName) {  
  190.         boolean success = false;  
  191.         File file = new File(getDoPath(path) + dirName);  
  192.         if (!file.exists()) {  
  193.             success = file.mkdirs();  
  194.         }   
  195.         return success;  
  196.     }  
  197. }  

 

点击新建可以创建新目录,确定可以获取选择的路径。

转载于:https://www.cnblogs.com/xiaohuzi2008/archive/2012/08/13/2637233.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值