| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 
 | @Slf4j@Service
 public class HDFSServiceImpl implements HDFSService {
 
 private static final int bufferSize = 1024 * 1024 * 64;
 
 @Autowired
 private FileSystem fileSystem;
 
 @Override
 public boolean makeFolder(String path) {
 boolean target = false;
 if (StringUtils.isEmpty(path)) {
 return false;
 }
 if (existFile(path)) {
 return true;
 }
 Path src = new Path(path);
 try {
 target = fileSystem.mkdirs(src);
 } catch (IOException e) {
 log.error(e.getMessage());
 }
 return target;
 }
 
 @Override
 public boolean existFile(String path) {
 if (StringUtils.isEmpty(path)){
 return false;
 }
 Path src = new Path(path);
 try {
 return fileSystem.exists(src);
 } catch (IOException e) {
 log.error(e.getMessage());
 }
 return false;
 }
 
 @Override
 public List<Map<String, Object>> readCatalog(String path) {
 if (StringUtils.isEmpty(path)){
 return Collections.emptyList();
 }
 if (!existFile(path)){
 log.error("catalog is not exist!!");
 return Collections.emptyList();
 }
 
 Path src = new Path(path);
 FileStatus[] fileStatuses = null;
 try {
 fileStatuses = fileSystem.listStatus(src);
 } catch (IOException e) {
 log.error(e.getMessage());
 }
 List<Map<String, Object>> result = new ArrayList<>(fileStatuses.length);
 
 if (null != fileStatuses && 0 < fileStatuses.length) {
 for (FileStatus fileStatus : fileStatuses) {
 Map<String, Object> cataLogMap = new HashMap<>();
 cataLogMap.put("filePath", fileStatus.getPath());
 cataLogMap.put("fileStatus", fileStatus);
 result.add(cataLogMap);
 }
 }
 return result;
 }
 
 @Override
 public boolean createFile(String path, MultipartFile file) {
 boolean target = false;
 if (StringUtils.isEmpty(path)) {
 return false;
 }
 String fileName = file.getName();
 Path newPath = new Path(path + "/" + fileName);
 
 FSDataOutputStream outputStream = null;
 try {
 outputStream = fileSystem.create(newPath);
 outputStream.write(file.getBytes());
 target = true;
 } catch (IOException e) {
 log.error(e.getMessage());
 } finally {
 if (null != outputStream) {
 try {
 outputStream.close();
 } catch (IOException e) {
 log.error(e.getMessage());
 }
 }
 }
 return target;
 }
 
 @Override
 public String readFileContent(String path) {
 if (StringUtils.isEmpty(path)){
 return null;
 }
 
 if (!existFile(path)) {
 return null;
 }
 
 Path src = new Path(path);
 
 FSDataInputStream inputStream = null;
 StringBuilder sb = new StringBuilder();
 try {
 inputStream = fileSystem.open(src);
 String lineText = "";
 while ((lineText = inputStream.readLine()) != null) {
 sb.append(lineText);
 }
 } catch (IOException e) {
 log.error(e.getMessage());
 } finally {
 if (null != inputStream) {
 try {
 inputStream.close();
 } catch (IOException e) {
 log.error(e.getMessage());
 }
 }
 }
 return sb.toString();
 }
 
 @Override
 public List<Map<String, Object>> listFile(String path) {
 if (StringUtils.isEmpty(path)) {
 return Collections.emptyList();
 }
 if (!existFile(path)) {
 return Collections.emptyList();
 }
 List<Map<String,Object>> resultList = new ArrayList<>();
 
 Path src = new Path(path);
 try {
 RemoteIterator<LocatedFileStatus> fileIterator = fileSystem.listFiles(src, true);
 while (fileIterator.hasNext()) {
 LocatedFileStatus next = fileIterator.next();
 Path filePath = next.getPath();
 String fileName = filePath.getName();
 Map<String, Object> map = new HashMap<>();
 map.put("fileName", fileName);
 map.put("filePath", filePath.toString());
 resultList.add(map);
 }
 } catch (IOException e) {
 log.error(e.getMessage());
 }
 
 return resultList;
 }
 
 @Override
 public boolean renameFile(String oldName, String newName) {
 boolean target = false;
 if (StringUtils.isEmpty(oldName) || StringUtils.isEmpty(newName)) {
 return false;
 }
 Path oldPath = new Path(oldName);
 Path newPath = new Path(newName);
 try {
 target = fileSystem.rename(oldPath, newPath);
 } catch (IOException e) {
 log.error(e.getMessage());
 }
 
 return target;
 }
 
 @Override
 public boolean deleteFile(String path) {
 boolean target = false;
 if (StringUtils.isEmpty(path)) {
 return false;
 }
 if (!existFile(path)) {
 return false;
 }
 Path src = new Path(path);
 try {
 target = fileSystem.deleteOnExit(src);
 } catch (IOException e) {
 log.error(e.getMessage());
 }
 return target;
 }
 
 @Override
 public boolean uploadFile(String path, String uploadPath) {
 if (StringUtils.isEmpty(path) || StringUtils.isEmpty(uploadPath)) {
 return false;
 }
 
 Path clientPath = new Path(path);
 
 Path serverPath = new Path(uploadPath);
 
 try {
 fileSystem.copyFromLocalFile(false,clientPath,serverPath);
 return true;
 } catch (IOException e) {
 log.error(e.getMessage(), e);
 }
 return false;
 }
 
 @Override
 public boolean downloadFile(String path, String downloadPath) {
 if (StringUtils.isEmpty(path) || StringUtils.isEmpty(downloadPath)) {
 return false;
 }
 
 Path clienPath = new Path(path);
 
 Path targetPath = new Path(downloadPath);
 
 try {
 fileSystem.copyToLocalFile(false,clienPath, targetPath);
 return true;
 } catch (IOException e) {
 log.error(e.getMessage());
 }
 return false;
 }
 
 @Override
 public boolean copyFile(String sourcePath, String targetPath) {
 if (StringUtils.isEmpty(sourcePath) || StringUtils.isEmpty(targetPath)) {
 return false;
 }
 
 Path oldPath = new Path(sourcePath);
 
 Path newPath = new Path(targetPath);
 
 FSDataInputStream inputStream = null;
 FSDataOutputStream outputStream = null;
 
 try {
 inputStream = fileSystem.open(oldPath);
 outputStream = fileSystem.create(newPath);
 
 IOUtils.copyBytes(inputStream,outputStream,bufferSize,false);
 return true;
 } catch (IOException e) {
 log.error(e.getMessage());
 } finally {
 if (null != inputStream) {
 try {
 inputStream.close();
 } catch (IOException e) {
 log.error(e.getMessage());
 }
 }
 if (null != outputStream) {
 try {
 outputStream.close();
 } catch (IOException e) {
 log.error(e.getMessage());
 }
 }
 }
 return false;
 }
 
 @Override
 public byte[] openFileToBytes(String path) {
 
 if (StringUtils.isEmpty(path)) {
 return null;
 }
 
 if (!existFile(path)) {
 return null;
 }
 
 Path src = new Path(path);
 byte[] result = null;
 FSDataInputStream inputStream = null;
 try {
 inputStream = fileSystem.open(src);
 result = IOUtils.readFullyToByteArray(inputStream);
 } catch (IOException e) {
 log.error(e.getMessage());
 } finally {
 if (null != inputStream){
 try {
 inputStream.close();
 } catch (IOException e) {
 log.error(e.getMessage());
 }
 }
 }
 
 return result;
 }
 
 @Override
 public BlockLocation[] getFileBlockLocations(String path) {
 if (StringUtils.isEmpty(path)) {
 return null;
 }
 if (!existFile(path)) {
 return null;
 }
 BlockLocation[] blocks = null;
 Path src = new Path(path);
 try{
 FileStatus fileStatus = fileSystem.getFileStatus(src);
 blocks = fileSystem.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
 }catch(Exception e){
 log.error(e.getMessage());
 }
 return blocks;
 }
 }
 
 |