Java 中 JavaScript 资源文件夹的等价物是什么?
我的 Wicket Web 应用程序包含 Flash (*.swf) FLV 播放器。以下代码:
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);
}
});
播放FLV。 swfobject.js
文件位于我的服务器的资源文件夹中。当我在本地主机上测试它时,资源文件夹的绝对路径是:/home/tapas/Desktop/proj/work/Jetty_0_0_0_0_80_proj.war__proj__qk44r3/webapp
。现在,如何使用 Java 将文件保存在服务器的资源文件夹中? JavaScript 将资源文件夹路径识别为 /proj/resources/
; Java 中该路径的等效表达式是什么?我已经尝试过:
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());
}
这不显示任何错误消息,但显示“文件不存在”。
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."
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议不要使用绝对文件路径来查找 WAR 文件中的资源。
如果您的 Java 应用程序需要资源,最好将其放入 CLASSPATH 中,并使用 servlet 上下文中的 getResourceAsStream() 来获取用于读取的 InputStream。
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.尝试找出您的 java 实际工作目录...这可能会有所帮助: 示例
我不认为java可以理解你的相对路径,因为它从与javascript不同的目录运行。只是我的意见,但尝试一下。
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")
((WebApplication) getApplication()).getServletContext().getRealPath("/resources/video/xml/video.xml")