在战争项目中加载文件图像 [英] Loading a file image in a war project
问题描述
这是我迄今为止没有成功的尝试:
String path = File.separatorChar +img + File.separatorChar + File.separatorChar +logoCorporativo.jpg;
System.out.println(path);
尝试{
FileInputStream fis = new FileInputStream(path);
} catch(FileNotFoundException e){
// TODO自动生成的catch块
e.printStackTrace();
code $
$ b $ p
$ pre> String path = File.separator +img+ File.separator + File.separator +logoCorporativo.jpg;
System.out.println(path);
尝试{
FileInputStream fis = new FileInputStream(path);
} catch(FileNotFoundException e){
// TODO自动生成的catch块
e.printStackTrace();
}
我发现当我输入http:// localhost:9080 / AdminMapasWeb /img/logoCorporativo.jpg到我的浏览器,我可以看到图像。
有没有人得到一个FileNotFound异常???
在此先感谢。
使用
<$ p $文件file = getRequest()。getServletContext()。getRealPath(/ img / logoCorporativo.jpg);
getRealPath将URL根相对路径(使用斜杠/)转换为文件。
如果战争在部署时没有解包,getRealPath返回null。
I´m doing a javaEE application and I´m using spring to inject my ejbs and WAS7, currently I´m running it in a windows environment altough in the end it will run in a unix environment. So I create a java class that acts as my controller for spring and I´m supposed to get some data from a jsp and turn it into an excel. So when I try to get an image from my web-content folder so I can put it into my excel y get file not found. This is what I´ve tried so far without success:
String path = File.separatorChar + "img" + File.separatorChar + File.separatorChar + "logoCorporativo.jpg";
System.out.println(path);
try {
FileInputStream fis = new FileInputStream(path);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
and
String path = File.separator + "img" + File.separator + File.separator + "logoCorporativo.jpg";
System.out.println(path);
try {
FileInputStream fis = new FileInputStream(path);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I found that when I typped "http://localhost:9080/AdminMapasWeb/img/logoCorporativo.jpg" into my browser I can see the image.
Does anyone am I getting a FileNotFound Exception??? Thanks in advance.
Use
File file = getRequest().getServletContext().getRealPath("/img/logoCorporativo.jpg");
getRealPath convert a URL root relative path (using slashes "/") to a File. If the war does not unpack itself on deployment, getRealPath returns null.
这篇关于在战争项目中加载文件图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!