2017-05-29 15 views
0

私はあるディレクトリから別のディレクトリにファイルをコピーするアプリケーションを作成しています。Javaサービスを使用して、あるディレクトリから別のディレクトリにファイルをコピーするにはどうすればいいですか?

JSON入力は次のとおりです。

{ "accountName" : "name", 
    "accountKey" : "key", 
    "source" : "directory1/directory2/directory3/directory4", 
    "destination" : "directory1/directory2", 
    "fileToCopy" : "1" 
} 

ディレクトリ4は、ディレクトリ3,3の下にある2及び2の下でされる1

下のディレクトリに移動4から「1」という名前のファイルをコピーしたいれます2.

私のJavaコードは次のとおりです。私のようにエラーを取得しています

@Override 
    public JSONObject copyFile(JSONObject jsonInput) throws IOException, InvalidKeyException, URISyntaxException { 
     CloudFileClient fileClient = null; 
     String storageConnectionString = "DefaultEndpointsProtocol=https;AccountName="+jsonInput.get("accountName")+";"+"AccountKey="+jsonInput.get("accountKey"); 
     System.out.println(storageConnectionString); 
     CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString); 
     JSONObject jsonOutput = new JSONObject(); 
     try { 
      fileClient = storageAccount.createCloudFileClient(); 

      String source = jsonInput.get("source").toString(); 
      String destination = jsonInput.get("destination").toString(); 
      String fileToCopy = jsonInput.get("fileToCopy").toString(); 
      String[] sourceNameArray = source.split("\\s*/\\s*"); 
      System.out.println(sourceNameArray.length); 
      String[] destinationNameArray = destination.split("\\s*/\\s*"); 
      System.out.println(destinationNameArray.length); 
      CloudFileShare share = fileClient 
        .getShareReference(sourceNameArray[0].toLowerCase().replaceAll("[-+.^:,[email protected]#$%&*()_~`]", "")); 
      CloudFileDirectory rootDir = share.getRootDirectoryReference(); 
      for (int i=0; i< sourceNameArray.length; i++) 
      { 
       String directoryToCreate = sourceNameArray[i]; 
       CloudFileDirectory directory = rootDir.getDirectoryReference(directoryToCreate); 
       if(i==sourceNameArray.length-1) 
       { 
        CloudFile fileToCopyFromSorce = directory.getFileReference(fileToCopy); 

        for (int j=0; j< destinationNameArray.length; j++) 
        { 
         String directoryToCreateForDestination = destinationNameArray[j]; 
         CloudFileDirectory directoryForDestination = rootDir.getDirectoryReference(directoryToCreateForDestination); 
         if(j==destinationNameArray.length-1){ 
         CloudFile fileDestination = directoryForDestination.getFileReference(fileToCopy); 
         // is next line required? 
       //fileToCopyFromSorce.create(1); 
         fileDestination.startCopy(fileToCopyFromSorce); 
         System.out.println("copied to destination"); 
         jsonOutput.put("status", "successful"); 
         } 
         rootDir = directoryForDestination; 
        } 
       } 
       rootDir = directory; 
      } 

     } catch (Exception e) { 
      System.out.println("Exception is " + e); 
      jsonOutput.put("status", "unsuccessful"); 
      jsonOutput.put("exception", e.toString()); 
     } 
     return jsonOutput; 
    } 

Exception is com.microsoft.azure.storage.StorageException: The specified parent path does not exist. 

私の紺色のストレージアカウントに親パスを指定しました。

可能であれば、コードおよび参考コードに関する提案が必要です。

答えて

3

この問題は、ファイルの親ディレクトリがAzureファイルストレージに存在しないことが原因で発生しました。下の図はhereです。

enter image description here

だから、あなたは、ディレクトリの参照を取得する必要がある場合は、そのような先のパスについては、以下のコードとして、まず子供たちに、ルートからこれらの親ディレクトリを一つずつチェックして作成する必要があります。

String destination = "directory1/directory2"; 
CloudFileDirectory rootDir = share.getRootDirectoryReference(); 
String[] destinationNameArray = destination.split("/"); 
CloudFileDirectory kidDir = rootDir; 
for(String name: destinationNameArray) { 
    kidDir = kidDir.getDirectoryReference(name); 
    kidDir.createIfNotExists(); 
} 

次に、次のようにソースからターゲットにファイルを直接コピーできます。

String source = "directory1/directory2/directory3/directory4"; 
String destination = "directory1/directory2"; 
String fileName = "1"; 
CloudFileDirectory sourceDir = rootDir.getDirectoryReference(source); 
CloudFileDirectory destinationDir = rootDir.getDirectoryReference(destination); 
CloudFile sourceFile = sourceDir.getFileReference(fileName); 
CloudFile destinationFile = destinationDir.getFileReference(fileName); 
destinationFile.startCopy(sourceFile); 
+0

ありがとうございます。出来た。しかし、それらが既に存在することがわかっていても、ディレクトリを作成するコードを書く必要がある理由を教えてください。 –

+0

@AnuragBondeただチェックして、存在しなければ作成してください。それは 'createIfNotExists'メソッドを意味します。 –

+0

ええ、あなたのポイントを持っています。もう一度ありがとうございます。 –

関連する問題