§ 常用工具类介绍
§ ConfigPreference 配置
ConfigPreference是Fox移动平台中获取和设置配置的工具类型,我们可以通过该工具类获取client.properties中的配置。
例如: client.poperties配置如下:
#主工程
web/mainApp=fox
#回退键事件
web/backAction=javascript:fox.custom.back()
#安全配置
security/scopes=[Http]
#security/scopes=[]
#是否启用更新
version/updateEnabled=false
#版本服务器地址
version/address=http://139.199.79.118:9705
获取client.properties中的配置
// 获取配置
ConfigPreference pref = ConfigPreference.getInstance();
//获取回退action
String backAction = pref.getString("web", "backAction", "");
§ FileAccessor 文件访问器
FileAccessor文件访问器,为每个应用的沙箱提供了操作的接口。 其使用方法如下:
步骤一:获取应用对应的FileAccessor
FileAccessor fileAccessor = FileAccessor.get("应用名")
步骤二:操作文件系统,常用接口如下
/**
* 获取应用的根目录
* @return
*/
public String getRoot()
/**
* 获取应用的根目录的文件路径
* @return
*/
public File getRootFile(String path)
/**
* 获取应用的根目录的文件路径
* @return
*/
public String getRootFilePath(String path)
/**
* 获取应用的工作目录
* @return
*/
public String getWorkspace()
/**
* 获取文件路径
* @param path
* @return
*/
public File getWorkspaceFile(String path)
/**
* 获取文件路径
* @param path
* @return
*/
public String getWorkspaceFilePath(String path)
//判断是否属于保护区
public boolean isProtectionZone(String path)
/**
* 创建此抽象路径名指定的目录,包括创建必需但不存在的父目录
*
* @param path
* @return
*/
public boolean mkdirs(String path)
/**
* 创建此抽象路径名指定的目录,包括创建必需但不存在的父目录
*
* @param file
* @return
*/
public boolean mkdirs(File file)
/**
* 创建文件
*
* @param path
* @return
* @throws IOException
*/
public File createNewFile(String path) throws IOException
/**
* 创建文件
*
* @param file
* @return
* @throws IOException
*/
public File createNewFile(File file) throws IOException
/**
* 设置文件循环数
* @param maxCircleCount
*/
public void setMaxCircleCount(int maxCircleCount)
/**
* 创建循环临时文件
* @param parent 父亲目录
* @param extension 扩展名
* @return
*/
public File createCircleFile(String parent, String extension) throws IOException
/**
* 创建时间戳临时文件
* @param parent 父亲目录
* @param extension 扩展名
* @return
*/
public File createTimeStampFile(String parent, String extension) throws IOException
/**
* 判断文件或文件夹是否存在
*
* @param path
* @return
*/
public boolean exists(String path)
/**
* 测试此抽象路径名表示的文件是否是一个目录
*
* @param path
* @return
*/
public boolean isDirectory(String path)
/**
* 测试此抽象路径名表示的文件是否是一个文件
*
* @param path
* @return
*/
public boolean isFile(String path)
/**
* 打开文件
*
* @param path
* @return
*/
public boolean openFile(String path)
/**
* 打开文件
*
* @param file
* @return
*/
public boolean openFile(File file)
/**
* 打开输入流
*
* @param path
* @return
*/
public InputStream openInputStream(String path) throws IOException
/**
* 打开输入流
*
* @param file
* @return
*/
public InputStream openInputStream(File file) throws IOException
/**
* 打开输出流
*
* @param path
* @return
*/
public OutputStream openOutputStream(String path) throws IOException
/**
* 打开输出流
*
* @param file
* @return
*/
public OutputStream openOutputStream(File file) throws IOException
/**
* 以BASE64字符串返回内容
*
* @param path
* @return
*/
public String getContentAsBase64(String path)
throws IOException
/**
* 以BASE64字符串返回内容
*
* @param file
* @return
*/
public String getContentAsBase64(File file)
throws IOException
/**
* 以字符串返回内容
*
* @param path
* @param encoding
* @return
*/
public String getContentAsString(String path, String encoding)
throws IOException
/**
* 以字符串返回内容
*
* @param file
* @param encoding
* @return
*/
public String getContentAsString(File file, String encoding)
throws IOException
/**
* 以字节数组返回内容
*
* @param path
* @return
* @throws
*/
public byte[] getContentAsBytes(String path) throws IOException
/**
* 以字节数组返回内容
*
* @param file
* @return
* @throws
*/
public byte[] getContentAsBytes(File file) throws IOException
/**
* 设置内容
*
* @param path
* @param content
*/
public void setContentAsBase64(String path, String content) throws IOException
/**
* 设置内容
*
* @param file
* @param content
*/
public void setContentAsBase64(File file, String content) throws IOException
/**
* 设置内容
*
* @param path
* @param content
*/
public void setContentAsString(String path, String content, String encoding) throws IOException
/**
* 设置内容
*
* @param file
* @param content
*/
public void setContentAsString(File file, String content, String encoding) throws IOException
}
/**
* 设置内容
*
* @param path
* @param content
*/
public void setContentAsBytes(String path, byte[] content) throws IOException
/**
* 设置内容
*
* @param file
* @param content
*/
public void setContentAsBytes(File file, byte[] content) throws IOException
/**
* 返回此抽象路径名表示的文件最后一次被修改的时间
*
* @return
*/
public long lastModified(String path)
/**
* 返回由此抽象路径名表示的文件的长度。如果此路径名表示一个目录,则返回值是不确定的
*
* @return
*/
public long length(String path)
/**
* 删除此抽象路径名表示的文件或目录
*
* @param path
* @return
*/
public boolean delete(String path)
/**
* 删除此抽象路径名表示的文件或目录
*
* @param file
* @return
*/
public boolean delete(File file)
/**
* 返回一个抽象路径名数组,这些路径名表示此抽象路径名所表示目录中的文件
*
* @param path
* @return
*/
public File[] listFiles(String path)
/**
* 返回一个抽象路径名数组,这些路径名表示此抽象路径名所表示目录中的文件
*
* @param file
* @return
*/
public File[] listFiles(File file)
/**
* 拷贝文件
*
* @param srcPath
* @param destPath
* @return
*/
public void copy(String srcPath, String destPath) throws Exception
/**
* 拷贝文件
*
* @param srcFile
* @param destFile
* @return
*/
public void copy(File srcFile, File destFile) throws Exception
/**
* 判断是否需要申请扩展存储空间的授权
*
* @param path
* @return
*/
public boolean needApplyExtStoragePermission(String path)
/**
* 申请访问扩展存储空间权限
*
* @param callbackContext
*/
public void applyExtStoragePermission(final ICallback callbackContext)
/**
* 加密文件
*
* @param srcPath
* @param destPath
* @return
*/
public boolean encryptFile(String srcPath, String destPath) throws IOException
/**
* 加密文件
*
* @param srcFile
* @param destFile
* @return
*/
public boolean encryptFile(File srcFile, File destFile) throws IOException
/**
* 解密文件
*
* @param srcPath
* @param destPath
* @return
*/
public boolean decryptFile(String srcPath, String destPath) throws IOException
/**
* 解密文件
*
* @param srcFile
* @param destFile
* @return
*/
public boolean decryptFile(File srcFile, File destFile) throws IOException
§ HttpRequester HTTP访问器
Http访问器,为每个应用提供访问Fox Server的能力。其使用步骤如下:
§ 步骤一:获取应用对应的HttpReuester(session共享)
HttpRequester httpRequester = HttpRequester.get("应用名")
§ 步骤二:进行http通信,其接口如下
/**
* ping测试
*
* @param url
* @param timeout
* @return
*/
public boolean ping(String url, int timeout)
/**
* GET方式请求HTTP服务
*
* @param url
* @param params
* @param data
* @param encoding
* @param timeout
* @return
*/
public HttpResponse get(String url, Map<String, String> params,
Map<String, String> data, String encoding, int timeout) throws Exception
/**
* POST方式请求HTTP服务
*
* @param url
* @param header
* @param data
* @param encoding
* @param timeout
* @param processData
* @return
*/
public HttpResponse post(String url, Map<String, String> header,
Object data, String encoding, int timeout, boolean processData)
throws Exception
/**
* HTTP请求请求
*
* @param url
* @param header
* @param data
* @param encoding
* @param timeout
* @param processData
* @return
*/
public HttpResponse send(String url, Map<String, String> header,
Object data, String encoding, int timeout, boolean processData)
throws Exception
/**
* 请求服务
*
* @param url
* @param headers
* @param data
* @param encoding
* @param timeout
* @param processData
* @param traceId
* @return
*/
public JSONObject request(String url, Map<String, String> headers,
Object data, String encoding, int timeout,
boolean processData, String traceId)
/**
* 远程文件是否存在
*
* @param address
* @param path
* @param timeout
* @return
* @apram name
*/
public boolean isRemoteFileExists(String address, String name, String path, int timeout) throws Exception
/**
* 列举远程文件夹
*
* @param address
* @param name
* @param path
* @param timeout
* @return
*/
public String[] listRemoteFile(String address, String name, String path, int timeout)
throws Exception
/**
* 列举远程文件夹
*
* @param address
* @param name
* @param path
* @param scale
* @param timeout
* @return
*/
public String[] listRemoteFile(String address, String name, String path, String scale, int timeout)
throws Exception
/**
* 获取远程文件的大小
*
* @param address
* @param name
* @param path
* @param timeout
* @return
*/
public long getRemoteFileSize(String address, String name, String path, int timeout) throws Exception
/**
* 获取远程文件的MD5值
*
* @param address
* @param name
* @param path
* @param timeout
* @return
*/
public String getRemoteFileStamp(String address, String name, String path, int timeout) throws Exception
/**
* 列举远程目录的MD5值
*
* @param address
* @param name
* @param path
* @param timeout
* @return
*/
public String[] listRemoteFileStamps(String address, String name,
String path, int timeout) throws Exception
/**
* 列举远程目录的MD5值
*
* @param address
* @param name
* @param path
* @param scale
* @param timeout
* @return
*/
public String[] listRemoteFileStamps(String address, String name,
String path, String scale, int timeout) throws Exception
/**
* 直接下载文件
*
* @param url (下载地址)
* @param data (参数)
* @param out (输出流)
* @param timeout (超时限制)
*/
public boolean directDownload(String url, Map<String, String> data, OutputStream out, int timeout) throws Exception
/**
* 直接下载文件
*
* @param url (下载地址)
* @param data (参数)
* @param out (输出流)
* @param timeout (超时限制)
* @param method (请求方法)
*/
public boolean directDownload(String url,Map<String,String> data,
OutputStream out, int timeout, String method) throws Exception
/**
* 直接下载文件
*
* @param url (下载地址)
* @param data (参数)
* @param saveFile (保存路径)
* @param timeout (超时限制)
*/
public boolean directDownload(String url,Map<String,String> data, File saveFile,
int timeout) throws Exception
/**
* 直接下载文件
*
* @param url (下载地址)
* @param data (参数)
* @param saveFile (保存路径)
* @param timeout (超时限制)
* @param method (请求method)
*/
public boolean directDownload(String url,Map<String,String> data, File saveFile,
int timeout, String method) throws Exception
/**
* 上传文件
*
* @param address (上传地址)
* @param uploadFile (上传文件)
* @param timeout (超时限制)
*/
public HttpResponse directUpload(String address, Map<String,String> params, File uploadFile, int timeout) throws Exception
/**
* 上传文件
* @param url
* @param fileName
* @param in
* @param params
* @param timeout
* @return
*
*/
public HttpResponse directUpload(String url,String fileName, InputStream in,
Map<String,String> params, int timeout) throws Exception
/**
* 下载文件
*
* @param address (下载地址)
* @param name (文件名)
* @param path (下载文件)
* @param saveFile (保存路径)
* @param timeout (超时限制)
* @param thumbnail (是否缩略图)
*/
public boolean download(String address, String name, String path,
File saveFile, int timeout, boolean thumbnail) throws Exception
、
* 下载文件
*
* @param address (下载地址)
* @param name (服务名)
* @param path (下载文件)
* @param out (输出流)
* @param timeout (超时限制)
* @param thumbnail (是否缩略图)
*/
public boolean download(String address,String name, String path,
OutputStream out, int timeout, boolean thumbnail) throws Exception
/**
* 上传文件
*
* @param address (上传地址)
* @param name (服务名)
* @param savePath (文件保存路径)
* @param uploadFile (上传文件)
* @param timeout (超时限制)
*/
public boolean upload(String address, String name, String savePath,
String fileName, File uploadFile, int timeout)
throws Exception
/**
* 上传文件
*
* @param address (上传地址)
*
* @param savePath (文件保存路径)
* @param in (输入流)
* @param timeout (超时限制)
*/
public boolean upload(String address, String name, String savePath,
String fileName, InputStream in, int timeout)
throws Exception
/**
* 批量下载文件
*
* @param address (下载地址)
* @param name (服务名称)
* @param downloadDirectory (下载目录下)
* @param downloadFilePaths (下载目录下指定需要下载的文件路径)
* @param saveDir (保存路径)
* @param checkValidity (是否校验)
*/
public boolean batchDownload(String address, String name, String downloadDirectory,
String[] downloadFilePaths, File saveDir, boolean checkValidity, int timeout)
throws Exception
/**
* 断点上传文件
*
* @param address
* @param name
* @param uploadPath
* @param fileName
* @param uploadFile
* @param timeout
* @param pageSize
* @param processContext
* @return
*
*/
public boolean breakpointUpload(String address, String name,String uploadPath,
String fileName, File uploadFile, int timeout,
int pageSize, IProcess processContext) throws Exception
/**
* 上传文件片段
*
* @param address
* @param name
* @param path
* @param fileName
* @param content
* @param index
* @param length
* @param timeout
* @return
*
*/
private boolean uploadSegment(String address, String name, String path,
String fileName, byte[] content, long index,
long length, long timeout) throws Exception
/**
* 断点下载文件
*
* @param address (下载地址)
* @param name (名称)
* @param path (文件路径以程序的安装目录文起点路径)
* @param saveFile (保存目录)
* @param timeout
* @param pageSize (页面大小KB)
* @param processContext
*/
public boolean breakpointDownload(String address, String name, String path,
File saveFile,int timeout, int pageSize,
IProcess processContext) throws Exception
§ 安全管理器 SecurityManager
安全管理器,提供获取加密/解密套件的接口。我们可以通过期提高了方法对内容进行加密或解密。
§ SecurityManager接口说明
/**
* 获取公共密钥套件
* @return
*
*/
public ICipher getPublicCipher() throws Exception
/**
* 获取秘钥套件
* @param zpk
* @return
*
*/
public ICipher getCipher(String zpk) throws Exception
/**
* 获取秘钥套件
* @param pair
* @return
*/
public ICipher getCipher(SecKeyPair pair) throws Exception
/**
* 获取秘钥套件
*
* @param zpk
* @return
*/
public ICipher getCipher(byte[] zpk) throws Exception
§ ICipher接口说明
/**
* 加密
* @param content
* @return
* @throws
*/
byte[] encrypt(byte[] content) throws Exception;
/**
* 解密
* @param content
* @return
* @throws
*/
byte[] decrypt(byte[] content) throws Exception;