简体   繁体   中英

What is the Java equivalent of JavaScript's resource folder?

My Wicket web application contains a Flash (*.swf) FLV player. The following code:

final String script = "var swfVersionStr = '10.0.0';"
    + "var xiSwfUrlStr = 'playerProductInstall.swf';"
    + "var flashvars = {file:'/proj/resources/video.flv'};"
    + "var params = {};"
    + "params.wmode = 'transparent';"
    + "params.quality = 'high';"
    + "params.allowscriptaccess = 'sameDomain';"
    + "params.allowfullscreen = 'true';"
    + "var attributes = {};"
    + "attributes.id = 'test';"
    + "attributes.name = 'test';"
    + "attributes.align = 'left';"
    + "swfobject.embedSWF('/proj/resources/mediaplayer.swf', 'movieDiv', '640', '480', swfVersionStr, xiSwfUrlStr, flashvars, params, attributes);"
    + "swfobject.createCSS('#flashContent', 'display:block;text-align:left;');";

add(new AbstractBehavior() {
    public void renderHead(IHeaderResponse response) {
        super.renderHead(response);
        response.renderOnLoadJavascript(script);
    }
});

plays the FLV. The swfobject.js file is placed in the resource folder of my server. As I am testing it on localhost, the absolute path of resource folder is: /home/tapas/Desktop/proj/work/Jetty_0_0_0_0_80_proj.war__proj__qk44r3/webapp . Now, how can I save a file in the resource folder of my server by using Java? JavaScript identifies the resource folder path as /proj/resources/ ; what is the equivalent expression of this path in Java? I have tried:

try{
    File file=new File("/proj/resources/joymaa.txt");
    if(file.exists()){
        System.out.println("File exists");
    }else{
        System.out.println("File does not exists");
    }
}catch(Exception exception){
    System.out.println(exception.getMessage());
}

This is not displaying any error message, but it shows "File does not exists."

I'd recommend not using an absolute file path to find a resource in a WAR file.

If your Java app needs a resource, best to put it in the CLASSPATH and use getResourceAsStream() from the servlet context to get an InputStream for reading.

try to find out what is your actual working directory of java... this might help: example I dont think that java could understand your relative path because it runs from different directory than javascript. Just my opinion, but give it a try.

(((WebApplication)getApplication())。getServletContext()。getRealPath(“ / resources / video / xml / video.xml”)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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