2017-12-05 8 views
0

宛先入力を埋めるときにフォルダを作成するWebアプリケーションを作成しました(たとえば、=> C:\ xxx \ xxxパス)。 ローカル(http:\ localhost:8080)で実行すると、完全に動作します。ローカルウィンドウのパスを見つけてフォルダを作成します。 しかし、私は人のグループにこのWebアプリケーションを開き、内部Unixサーバ(http:\ ipnumber \ portnumber)にTomcatをデプロイします。 問題は、ユーザーが入力をローカル宛先に入力すると、プログラムコードがパスを見つけることができないか、ローカルコンピュータのフォルダ構造にアクセスできないということです。LinuxサーバーのJava Webアプリケーションからローカルパスにアクセスする方法は?

これをどのように達成できますか?私は、フロントエンド用のanglejsをhttp.postでコールrestapiとともに使用します。バックエンド側はjavaです。

package com.ama.ist.controller; 
    import java.util.HashMap; 
    import java.util.Map; 
    import java.util.UUID; 
    import org.springframework.beans.factory.annotation.Autowired; 
    import org.springframework.http.HttpStatus; 
    import org.springframework.http.ResponseEntity; 
    import org.springframework.web.bind.annotation.CrossOrigin; 
    import org.springframework.web.bind.annotation.RequestBody; 
    import org.springframework.web.bind.annotation.RequestMapping; 
    import org.springframework.web.bind.annotation.RequestMethod; 
    import org.springframework.web.bind.annotation.RestController; 
    import com.ama.ist.model.CustomErrorType; 
    import com.ama.ist.model.Patch; 
    import com.ama.ist.service.PatchService; 
    @RestController 
    public class PatchController { 

    @Autowired 
    private PatchService patchService; 


    @CrossOrigin(origins = "http://ipnumber:portnumber") 
    @RequestMapping(value = "/mk", method = RequestMethod.POST) 
    public ResponseEntity<?> createFolder(@RequestBody Patch patch) { 


     System.out.println("patch ddest: => " + patch.getDestination()); 
     String iscreatedstatus = patchService.create(patch); 
     System.out.println("iscreatedstatus" + iscreatedstatus); 
     if (!(iscreatedstatus.equals("Success"))) { 
      System.out.println("if success"); 
      return new ResponseEntity<Object>(new CustomErrorType("ER",iscreatedstatus), HttpStatus.NOT_FOUND); 
     } 
     System.out.println("if disinda success"); 
      return new ResponseEntity<Object>(new CustomErrorType("OK",iscreatedstatus), HttpStatus.CREATED); 
    } 

// 
    @RequestMapping("/resource") 
     public Map<String,Object> home() { 
     Map<String,Object> model = new HashMap<String,Object>(); 
     model.put("id", UUID.randomUUID().toString()); 
     model.put("content", "Hello World"); 
     return model; 
     } 

} 

これはサービス

package com.ama.ist.service; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.text.SimpleDateFormat; 
import java.util.Date; 

import org.springframework.stereotype.Service; 
import org.tmatesoft.svn.core.SVNDepth; 
import org.tmatesoft.svn.core.SVNException; 
import org.tmatesoft.svn.core.SVNProperties; 
import org.tmatesoft.svn.core.SVNURL; 
import org.tmatesoft.svn.core.auth.BasicAuthenticationManager; 
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager; 
import org.tmatesoft.svn.core.wc.SVNCommitClient; 
import org.tmatesoft.svn.core.wc.SVNWCUtil; 

import com.ama.ist.model.Patch; 
import com.ama.ist.model.User; 

@Service 
public class PatchService { 

    public String create(Patch patch) { 

     String ConstantPath = patch.getDestination(); 


     File testFile = new File(""); 
     String currentPath = testFile.getAbsolutePath(); 
     System.out.println("current path is: " + currentPath); 


     System.out.println("ConstantPath => " + ConstantPath); 
//  if (!(isValidPath(ConstantPath))) { 
//   return "invalid Path"; 
//  } 

     // System.out.println("Valid mi " + isValidPath(ConstantPath)); 

     String foldername = patch.getWinNum() + " - " + patch.getWinName(); 
     System.out.println(ConstantPath + foldername); 

     File files = new File(ConstantPath + foldername); 
     if (files.exists()) { 
      return "The Folder is already created in that path"; 
     } 

     File files1 = new File(ConstantPath + foldername + "\\Patch"); 
     File files2 = new File(ConstantPath + foldername + "\\Backup"); 
     File files3 = new File(ConstantPath + foldername + "\\Backup\\UAT"); 
     File files4 = new File(ConstantPath + foldername + "\\Backup\\PROD"); 

     if (!files.exists()) { 
      if (files.mkdirs()) { 
       files1.mkdir(); 
       files2.mkdir(); 
       files3.mkdir(); 
       files4.mkdir(); 

       createReadme(ConstantPath + foldername, patch); 

       if (patch.isChecked()) { 

        System.out.println("patch.getDestination => " + patch.getDestination()); 
        System.out.println("patch.getDetail => " + patch.getDetail()); 
        System.out.println("patch.getSvnPath => " + patch.getSvnPath()); 
        System.out.println("patch.getWinName => " + patch.getWinName()); 
        System.out.println("patch.getWinNum => " + patch.getWinNum()); 

        System.out.println("patch.getUserName => " + patch.getUser().getUserName()); 
        System.out.println("patch.getPassword => " + patch.getUser().getPassword()); 
        ImportSvn(patch); 

       } 

       System.out.println("Multiple directories are created!"); 
       return "Success"; 
      } else { 
       System.out.println("Failed to create multiple directories!"); 
       return "Unknwon error"; 
      } 
     } else { 
      return "File name is already exists"; 
     } 

    } 

    public static boolean isValidPath(String path) { 
     System.out.println("path => " + path); 
     File f = new File(path); 

     if (f.isDirectory()) { 
      System.out.println("true => "); 
      return true; 
     } else { 
      System.out.println("false => "); 
      return false; 
     } 

    } 

    public void createReadme(String path, Patch patch) { 

     try { 
      ClassLoader classLoader = getClass().getClassLoader(); 
      File file = new File(classLoader.getResource("Readme.txt").getFile()); 

      // System.out.println("!!!!!!!!!!" + new java.io.File("").getAbsolutePath()); 
      // File file = new File("resources/Readme.txt"); 
      System.out.println(file.getAbsolutePath()); 

      FileReader reader = new FileReader(file); 
      BufferedReader bufferedReader = new BufferedReader(reader); 

      String line; 
      PrintWriter writer = new PrintWriter(path + "\\Readme.txt", "UTF-8"); 
      System.out.println(path + "\\Readme.txt"); 
      while ((line = bufferedReader.readLine()) != null) { 

       line = line.replace("#Winnumber", Integer.toString(patch.getWinNum())); 
       line = line.replace("#NameSurname", " "); 
       line = line.replace("#Type", "Package"); 
       line = line.replace("#detail", patch.getDetail()); 


       SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); 
       String date = sdf.format(new Date()); 
       line = line.replace("#Date", date); 

       line = line.replace("#Desc", patch.getWinName()); 

       writer.println(line); 

       System.out.println(line); 
      } 
      reader.close(); 
      writer.close(); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 

    public void ImportSvn(Patch patch) { 

     String name = patch.getUser().getUserName(); 
     String password = patch.getUser().getPassword(); 
     // String filename = patch.getWinName() 
     String filename = patch.getWinNum() + " - " + patch.getWinName(); 
     String url = patch.getSvnPath() + "/" + filename; 

     ISVNAuthenticationManager authManager = new BasicAuthenticationManager(name, password); 

     SVNCommitClient commitClient = new SVNCommitClient(authManager, SVNWCUtil.createDefaultOptions(true)); 
     File f = new File(patch.getDestination() + filename); 
     try { 
      String logMessage = filename; 
      commitClient.doImport(f, // File/Directory to be imported 
        SVNURL.parseURIEncoded(url), // location within svn 
        logMessage, // svn comment 
        new SVNProperties(), // svn properties 
        true, // use global ignores 
        false, // ignore unknown node types 
        SVNDepth.INFINITY); 
      // SVNClientManager cm = 
      // SVNClientManager.newInstance(SVNWCUtil.createDefaultOptions(true),authManager); 
      // 
      // SVNUpdateClient uc = cm.getUpdateClient(); 
      // long[] l = uc.doUpdate(new File[]{dstPath}, 
      // SVNRevision.HEAD,SVNDepth.INFINITY, true,true); 
     } catch (SVNException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 

} 

これは、Windows上でそのフォルダを共有し、UNIXの場合はその共有フォルダをマウントすることができAngularjs側

$scope.Create = function() { 
    $scope.obj = []; 
    console.log("$scope.svnPath" + $scope.patch.svnPath); 
    console.log("$scope.userName" + $scope.patch.user.userName); 
    $http({ 
     method : "POST", 
     url : "http://ipnumber:port/patchinit/mk", 
     data : $scope.patch 
    }).then(function mySuccess(response) { 

     console.log("Success!! "); 
     $scope.obj = response.data; 
     $scope.errorMessage = response.data.errorMessage; 
     $scope.errorCode = response.data.errorCode; 

    }, function myError(response) { 

     //$scope.obj = response.statusText; 
     $scope.errorMessage = response.data.errorMessage; 
     $scope.errorCode = response.data.errorCode; 

    }); 

} 
+0

webappを配備したユーザーが、上記のパスとフォルダに読み書きできるかどうかを確認してください。 – neo

答えて

0

です。実装後は、samba(smb://192.168.1.117/Your_Folder)を使用して簡単にアクセスできます。

SambaはほぼすべてのLinuxディストリビューションで標準であり、他のUnixベースのオペレーティングシステムでも基本的なシステムサービスとして一般に含まれています。

+0

ご回答いただきありがとうございます –

関連する問題