# nk-files
CCTP产品的文件服务,默认端口:9631
## 命令脚本
Linux环境下执行 nk-ms.sh start|stop|restart|status
Windows环境下执行 start.bat (暂未提供)
## 使用方式
### 上传文件:
WEB页面的形式
编辑一个html页面,代码如下:
```html
测试文件上传功能
```
使用浏览器打开该页面,通过页面表单的形式上传文件,上传成功后会返回文件的唯一ID,返回的报文如下:
```json
{
"success":true,
"code":"000000",
"message":"接口调用成功",
"timestamp":1585132341906,
"data":"5c2e20f0cfd04ba5a5cc86ed128c9564"
}
```
JAVA编码的形式
引用Maven依赖:
```xml
net.northking.commons
nk-files-client
2.0.0-SNAPSHOT
```
JAVA示例代码如下:
```java
String ipAddress = "192.168.0.40";
int port = 9631;
// File file = new File("F:\\工作笔记\\项目资料\\图片\\京北方云测试平台\\监控中心.png");
File file = new File("F:\\软件\\GamingCenter_1.2.3.4_Mechrevo.zip");
// 返回文件的唯一ID,此ID用于文件下载、查询等功能
String fileId = NKFilesClient.uploadFile(ipAddress, port, file);
if (fileId != null)
{
System.out.println("文件上传成功,文件ID:" + fileId);
}
```
### 下载文件
WEB页面的形式
编辑一个html页面,代码如下:
```html
测试文件下载功能
下载文件
```
JAVA编码的形式
引用Maven依赖:
```xml
net.northking.commons
nk-files-client
2.0.0-SNAPSHOT
```
JAVA示例代码如下:
```java
public void downloadFile01() throws IOException
{
String ipAddress = "192.168.0.40";
int port = 8512;
// 文件唯一ID
String fileId = "07d6d3736ea444069ada1452e7e4a167";
// 本地目录,用于存放下载下来的文件目录
String localPath = "D:\\TEST\\NKFilesClient";
// 下载成功后得到的本地文件对象
File localFile = NKFilesClient.downloadFile(ipAddress, port, fileId, localPath);
if (localFile != null)
{
System.out.println("文件下载成功,位置:" + localFile.getAbsoluteFile());
}
}
public void downloadFile02() throws IOException
{
String ipAddress = "192.168.0.40";
int port = 8512;
// 文件唯一ID
String fileId = "07d6d3736ea444069ada1452e7e4a167";
// 指定存放到本地文件系统具体的文件名称
String filePath = "D:\\TEST\\NKFilesClient\\" + UUID.randomUUID().toString() + ".nkf";
File localFile = new File(filePath);
NKFilesClient.downloadFile(ipAddress, port, fileId, localFile);
if (localFile != null)
{
System.out.println("文件下载成功,位置:" + localFile.getAbsoluteFile());
}
}
public void downloadFile03() throws IOException
{
String url = "http://111.203.253.33:9631/download/07d6d3736ea444069ada1452e7e4a167";
String localPath = "D:\\TEST\\NKFilesClient";
File localFile = NKFilesClient.downloadFile(url, localPath);
if (localFile != null)
{
System.out.println("文件下载成功,位置:" + localFile.getAbsoluteFile());
}
}
```