2013-02-16 25 views
1

在我的項目,我現在使用的是直接鏈接直接鏈接,即獲得上傳到服務器上存儲這些特定的文件,這些位置是所有的項目(文件上傳)的文件夾路徑的web應用程序裏面沒有

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

會發生什麼事,當你保存到「FILENAME.TXT」,而不是硬編碼的目錄?它應該加載/保存到當前目錄。 – NoBugs 2013-02-16 23:00:25

回答

2

只是使其外部配置。有多種方法可以實現這一點。

  1. 在服務器啓動期間設置環境變量。

    SET UPLOAD_LOCATION=C:\path\to\uploads 
    

    它的問世如下:

    String uploadLocation = System.getenv("UPLOAD_LOCATION"); 
    
  2. 設置服務器啓動時的VM參數。

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

    它的問世如下:

    String uploadLocation = System.getProperty("upload.location"); 
    
  3. 將其設置爲properties file條目。

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

    這是中常用的Properties API方式:

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

    屬性文件的位置本身其實是在自己的一個整體的問題,這已經在這裏找到答案:Where to place and how to read configuration resource files in servlet based application?

無論哪種方式,你可以很容易地引用文件內容如下:

File some = new File(uploadLocation, "some.ext"); 
相關問題