在战争项目中加载文件图像 [英] Loading a file image in a war project

查看:142
本文介绍了在战争项目中加载文件图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个javaEE应用程序,我正在使用spring来注入我的ejbs和WAS7,目前我正在Windows环境中运行它,最后它将运行在unix环境中。所以我创建了一个java类,作为我的控制器的春天,我应该从一个jsp获得一些数据,并把它变成一个excel。所以,当我尝试从我的web内容文件夹中获取图像时,我可以将其放入我的excel文件找不到。
这是我迄今为止没有成功的尝试:

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆