繁体   English   中英

如何从Linux服务器Java Web应用程序访问本地路径?

[英]How to access local path from linux server java web application?

我创建了一个Web应用程序,该应用程序在填充目标输入时会创建文件夹(例如=> C:\\ xxx \\ xxx路径)。 当我在本地(http:\\ localhost:8080)上运行时,它运行正常。 它找到本地Windows路径并创建文件夹。 但是现在我想向一群人打开此webapp,并将其部署在内部unix服务器(http:\\ ipnumber \\ portnumber)上。 问题是,当用户用本地目标填充输入时,程序代码找不到路径或无法访问本地计算机文件夹结构,它看起来是unix服务器文件夹结构。

我怎样才能做到这一点? 我将angularjs用于带有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();
        }

    }

}

这是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;

    });

}

您可以在Windows上共享该文件夹,并在UNIX中安装该共享文件夹。 安装后,可以使用samba(smb://192.168.1.117/Your_Folder)轻松访问它。

Samba几乎是所有Linux发行版上的标准,并且通常作为其他基于Unix的操作系统上的基本系统服务而包含在内。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM