Spire.Cloud.Storage Java SDK给开发人员提供了FileApi接口,支持操作Word, Excel, PowerPoint, PDF等文件,主要包括文件的上传、移动、复制、下载及删除。本文将介绍如何调用FileApi接口上传文件。
首先:通过Maven仓库安装Spire.Cloud.SDK jar包及依赖,详细步骤可参考这篇文章。
其次:通过冰蓝云官网(https://cloud.e-iceblue.cn/)注册账号并登陆,在“我的应用”创建应用程序,获得App ID及App Key。
import spire.cloud.storage.sdk.Configuration;
import spire.cloud.storage.sdk.api.FileApi;
import java.io.File;
public class CloudWord {
static String appId = "Your App ID";
static String appKey = "Your App Key";
static String baseUrl = "https://api.e-iceblue.cn";
//配置App ID和App Key
static Configuration configuration = new Configuration(appId, appKey, baseUrl);
//创建FileApi实例
static FileApi fileApi = new FileApi(configuration);
public static void main(String[] args) throws Exception {
//设置上传文件的路径及名称,无如文件夹,直接设置文件名称及格式
String path = "NewWord.docx";
//本地文件路径及名称
String localFilePath = "D:/Sample.docx";
File data = new File(localFilePath);
String storageName = null;
//上传文件
fileApi.uploadFile(path, data, storageName);
}
public static void copyFile() throws Exception{
//复制文件
String srcPath = "input/test.docx";
String destPath = "input/test/test2.docx";
String srcStorageName = null;
String destStorageName = null;
fileApi.copyFile(srcPath, destPath, srcStorageName, destStorageName);
}
public static void deleteFile() throws Exception{
//删除文件
String path = "input/test.docx"; ;
String storageName = null;
fileApi.deleteFile(path, storageName);
}
public static void downloadFile() throws Exception{
//下载文件
String path = "input/test.docx";
String storageName = null;
File response = fileApi.downloadFile(path, storageName);
}
public static void moveFile() throws Exception{
//移动文件
String srcPath = "input/test.pptx";
String destPath = "output/test/test.pptx";
String srcStorageName = null;
String destStorageName = null;
fileApi.moveFile(srcPath, destPath, srcStorageName, destStorageName);
}
}
上传Word文档效果图: