0

大家好,我有一个问题,我有一个属性文件,它存储了所有的保存位置,我通过以下方式从这个文件中获取数据:

public void loadProp() {
        System.out.println("Loading properties");
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("config.properties"); //points to a properties file, this will load up destinations instead of having to declare them here
        try {
            configProp.load(in);
            System.out.println(configProp.getProperty("destinationPDF"));
            System.out.println(configProp.getProperty("destination"));
            System.out.println(configProp.getProperty("fileList"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("called get username");
        username = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
        System.out.println(username);


    }

然后我这样做以将值分配给目标

public String destination = configProp.getProperty("destination");

但是每当我使用目的地时,我都会得到一个空值,但是如果我使用 configProp.getProperty("destination") 我会得到完整的路径,我在这里做错了什么,因为我希望该值指向目的地,因为其他类取决于它

编辑 :

This class is called on by a command button (web app)

    @ViewScoped
    @ManagedBean(name = "fileUploadController")
    public class FileUploadController {

        public boolean isUploadComplete() { //to enable the next button once finished
            return uploadComplete;
        }

        public void setUploadComplete(boolean uploadComplete) {
            this.uploadComplete = uploadComplete;
        }

        public boolean isUploadComplete2() {
            //to disable the file upload button, this will stop users uploading multiple files and over writing them as only the last file uploaded will be used
            return uploadComplete;
        }

        public void setUploadComplete2(boolean uploadComplete) {
            this.uploadComplete = uploadComplete;
        }
        /*
         public void handleFileUpload(FileUploadEvent event) {
         System.out.println("called");
         FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
         FacesContext.getCurrentInstance().addMessage(null, msg);
         }
         }
         */
        //
        //Strings for fileUpload
        //oadProp() 
        //public String fileList = "D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/Directory Files/directoryFiles.txt"; //
        private Properties configProp = new Properties();

        @PostConstruct
        //System.out.println(destinationPDF);
        //System.out.println(destination);
    // Get the username from the login page, this is used to create a folder for each user
        public void loadProp() {
            System.out.println("Loading properties");
            InputStream in = this.getClass().getClassLoader().getResourceAsStream("config.properties"); //points to a properties file, this will load up destinations instead of having to declare them here
            try {
                configProp.load(in);
                System.out.println(configProp.getProperty("destinationPDF"));
                System.out.println(configProp.getProperty("destination"));
                System.out.println(configProp.getProperty("fileList"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("called get username");
            username = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
            System.out.println(username);


        }
//String destinationPDF = configProp.getProperty("destinationPDF"); Always makes a null no idea why yet
    //private String destinationPDF = configProp.getProperty("destinationPDF");
    public String destination = configProp.getProperty("destination");
    private String username;
    //public static String destination = "D:/Documents/NetBeansProjects/printing~subversion/fileupload/uploaded/"; // main location for uploads//TORNADO ONLY //"D:/My Documents/NetBeansProjects/printing~subversion/fileupload/uploaded/"; // USE ON PREDATOR ONLY 
    public static String NewDestination;
    public static String UploadedfileName;
    public static String CompletefileName;
    //
    //Strings for file copy
    //
    //private String destinationPDF = "D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/pdf/"; //USE ON TORNADO//"D:/My Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/pdf/";//USE ON PREDATOR    
    private String NewdestinationPDF;
    public static String PdfLocationViewable;
    private boolean uploadComplete;
    private boolean uploadComplete2;

    //
    public void File() {

以上是该类的第一段代码

控制台中的输出是:

INFO: buttonToUploadText invoked
INFO: Loading properties
INFO: D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/pdf/
INFO: D:/Documents/NetBeansProjects/printing~subversion/fileupload/Uploaded/
INFO: D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/Directory Files/directoryFiles.txt
INFO: called get username
INFO: null
INFO: destination is null
4

2 回答 2

1

替换此行

public String destination = configProp.getProperty("destination");

public String destination;

并提供一个构造函数:

public FileUploadController() {
    loadProp();
    this.destination = configProp.getProperty("destination");
}

现在,loadProp()将由构造函数调用,您不必再这样做了。

于 2013-02-19T16:36:32.123 回答
1

您的订单已关闭。这是当你的 bean 被容器实例化时会发生的情况:

  1. 构造函数将被调用
  2. 所有注入的字段都将被解析
  3. PostConstruct 将被调用。

目前,您在加载属性之前destination设置值。这个问题的一个非常简单的解决方案是简单地在你的 @PostConstruct 处理程序中设置值。destination

@PostConstruct
public void loadProp() {
    InputStream in = this.getClass().getClassLoader()
            .getResourceAsStream("config.properties");
    try {
        configProp.load(in);
    } catch (IOException e) {
        e.printStackTrace();
    }

    destination = configProp.getProperty("destination");
}

与其他方法相比,此方法的一个优点是destination每次loadProp调用该方法时都会正确设置该属性(而不是只调用一次)。

于 2013-02-19T16:40:50.537 回答