// 1文件上传
/**
* 文件上传
*
* @param IDc 用户ID
* @param localPath 本地文件完整路径
* @param cloudPath hdfs文件保存完整路径
* @return 返回json格式字符串,包含operation和log两个属性
* @throws IOException
* @throws InterruptedException
* @throws URISyntaxException
*/
public String Upload(String IDc, String localPath, String cloudPath) throws IOException, InterruptedException, URISyntaxException {
// 1获取fs对象
Configuration conf = new Configuration();
// conf.set("dfs.replication", "2");
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.1.102:9000"), conf, "hadoop");
Path findf = new Path(cloudPath);
//FileSystem.exists指定HDFS文件是否存在
boolean isExists = fs.exists(findf);
// System. out.println(isExists?"文件存在" :"文件不存在" );
if (isExists) {
fs.close();
return "{\"operation\":\"The file/folder you are uploading already exists\",\"log\":{}}";
}
// 2 执行上传API
fs.copyFromLocalFile(new Path(localPath), new Path(cloudPath));//前本后云
// 3关闭资源
fs.close();
return "{\"operation\":\"Upload\",\"log\":{\"IDc\":\"" + IDc + "\",\"ticket\":\"null\","
+ "\"file\":\"" + cloudPath + "\",\"operation\":\"Upload\","
+ "\"time\":\"" + System.currentTimeMillis() + "\"}}";
}
// 2文件下载
/**
* 文件下载子操作
*
* @param cloudPath hdfs文件下载完整路径
* @param localPath 文件下载到本地完整路径
* @throws IOException
* @throws InterruptedException
* @throws URISyntaxException
*/
private void down(String cloudPath, String localPath) throws IOException, InterruptedException, URISyntaxException {
// 1获取对象
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.1.102:9000"), conf, "hadoop");
// 2执行下载操作
// fs.copyToLocalFile(new Path("/banhua.txt"), new Path("D:/banhua.txt"));
fs.copyToLocalFile(false, new Path(cloudPath), new Path(localPath), true);
// 3关闭资源
fs.close();
}
// 3文件删除
/**
* 文件删除
*
* @param IDc 用户ID
* @param cloudPath hdfs文件完整路径
* @return 返回json格式字符串,包含operation和log两个属性
* @throws IOException
* @throws InterruptedException
* @throws URISyntaxException
*/
public String Delete(String IDc, String cloudPath) throws IOException, InterruptedException, URISyntaxException {
// 1获取对象
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.1.102:9000"), conf, "hadoop");
//判断文件是否存在
if (!fs.exists(new Path(cloudPath))) {
fs.close();
return "{\"operation\":\"The file/folder you are deleting does not exist\",\"
JAVA HDFS 文件上传、下载、删除、更名、移动/剪切、获取文件列表、复制文件/文件夹、查找、创建文件夹、获取文件夹大小
最新推荐文章于 2023-07-19 10:52:18 发布