Web应用程序内部的路径不是直接链接 [英] Path inside an web app not a direct link

查看:137
本文介绍了Web应用程序内部的路径不是直接链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我目前使用直接链接,这些存储特定的文件,上传到服务器,这些位置都在项目的文件夹(fileupload)

  destinationPDF = D:/ Documents / NetBeansProjects / printing〜subversion / fileupload / web / resources / pdf / 
destination = D:/ Documents / NetBeansProjects / printing〜subversion / fileupload / Uploaded /
fileList = D:/ Documents / NetBeansProjects / printing〜subversion / fileupload / web / resources / Directory Files / directoryFiles.txt

但这不是一个理想的情况,因为我目前在多台机器上测试,每台机器都需要不同的路径,所以我想知道,是否有可能创建一个路径不要紧,它是什么机器

解决方案

只要使其在外部可配置。有很多方法可以达到这个目的。


  1. 在服务器启动期间设置一个环境变量。

      SET UPLOAD_LOCATION = C:\path\to\uploads 


    $ b

    字符串uploadLocation = System.getenv ( UPLOAD_LOCATION);


  2. 在服务器启动期间设置VM参数。 $ b

      -Dupload.location =C:\path\to\uploads



    $ b

    字符串uploadLocation = System.getProperty( upload.location);


  3. 将其设置为属性文件条目。

      upload.location = C:\path\to\uploads 

    可以使用通常的 属性 API方式:

     字符串uploadLocation = properties.getProperty(upload。位置); 

    属性文件本身的位置实际上是一个完整的问题,这已经在这里得到了解答:去哪儿放置以及如何在基于servlet的应用程序中读取配置资源文件?



  4. 无论哪种方式,您都可以轻松地参考文件如下:

      File some = new File(uploadLocation,some.ext); 


    in my project i am currently using direct links, these store specific files that get uploaded to the server, these locations are all in the folder of the project (fileupload)

    destinationPDF=D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/pdf/
    destination=D:/Documents/NetBeansProjects/printing~subversion/fileupload/Uploaded/
    fileList =D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/Directory Files/directoryFiles.txt
    

    but this is not an ideal situation, as i am currently testing on multiple machines and each machine needs a different path, so i am wondering, is it possible to create a path so that it does not matter what machine it is on

    解决方案

    Just make it externally configurable. There are various ways to achieve that.

    1. Set an environment variable during server startup.

      SET UPLOAD_LOCATION=C:\path\to\uploads
      

      It's available as follows:

      String uploadLocation = System.getenv("UPLOAD_LOCATION");
      

    2. Set a VM argument during server startup.

      -Dupload.location="C:\path\to\uploads"
      

      It's available as follows:

      String uploadLocation = System.getProperty("upload.location");
      

    3. Set it as a properties file entry.

      upload.location=C:\path\to\uploads
      

      It's available the usual Properties API way:

      String uploadLocation = properties.getProperty("upload.location");
      

      The location of the properties file itself is actually a whole question at its own, which is already answered here: Where to place and how to read configuration resource files in servlet based application?

    Either way, you can easily reference the files as follows:

    File some = new File(uploadLocation, "some.ext");
    

    这篇关于Web应用程序内部的路径不是直接链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆