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 renderHea开发者_StackOverflow中文版d(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")
精彩评论