在 Tomcat 中时,GWT 无法从文件系统加载图形

发布于 2024-12-08 12:33:57 字数 3068 浏览 0 评论 0原文

我使用以下 Servlet 来映射文件系统文件夹中的图像。

public class ImageServlet extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse resp) 
    throws IOException {

        // Get the base path for graphics root folder
        String basePath = System.getenv(Constants.ENV_VAR);

        String specifier = req.getParameter(Constants.PARAMETER);


        resp.setContentType(Constants.CONTENT_TYPE);

        File file = new File(basePath+specifier+"."+Constants.IMG_TYPE);
        resp.setContentLength((int)file.length());

        FileInputStream in = new FileInputStream(file);
        OutputStream out = resp.getOutputStream();

        // Copy the contents of the file to the output stream
        byte[] buf = new byte[Constants.INPUT_BUFFER_SIZE];
        int count = 0;
        while ((count = in.read(buf)) >= 0) {
            out.write(buf, 0, count);
        }
        in.close();
        out.close();
    }
}

然后,我使用 GWT-RPC 来获取图像,方法是使用以下 AppServiceImpl 类:

public class AppServiceImpl extends RemoteServiceServlet implements AppService {

    //Get the base path for graphics root folder
    String basePath = System.getenv(Constants.ENV_VAR);

    @Override
    public ArrayList<String> getImageFiles(String folder) {
        ArrayList<String> list = new ArrayList<String>();
        File parent = new File(**basePath+folder**);
        if (!parent.isDirectory()) {
            return list;
        }
        File[] files = parent.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].isFile() && !files[i].isHidden()) {
                // save image title
                list.add(files[i].getName().substring(0, files[i].getName().indexOf(".")));
            }
        }
        return list;
    }
...

为了在 GWT 代码中获取图像,我使用: final Image lgImg = new Image(Constants.URL_PREFIX + imgPath);

  • URL_PREFIX = "http://localhost:8888/images?" + 参数 + "="

  • ENV_VAR = "APP_GFX_HOME"

  • PARAMETER = " path"

在开发模式下,一切工作正常,但是当我部署到 tomcat 服务器时,图像不会出现,我需要一些帮助找出问题。

ps:这是web.xml文件的一部分

<!-- Services -->
    <servlet>
        <servlet-name>theImageServlet</servlet-name>
        <servlet-class>com.app.server.ImageServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>theImageServlet</servlet-name>
        <url-pattern>/images</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>appService</servlet-name>
        <servlet-class>com.app.server.AppServiceImpl</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>appService</servlet-name>
        <url-pattern>/app/services</url-pattern>
    </servlet-mapping>

I use the following Servlet to map images from a folder of the file-system.

public class ImageServlet extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse resp) 
    throws IOException {

        // Get the base path for graphics root folder
        String basePath = System.getenv(Constants.ENV_VAR);

        String specifier = req.getParameter(Constants.PARAMETER);


        resp.setContentType(Constants.CONTENT_TYPE);

        File file = new File(basePath+specifier+"."+Constants.IMG_TYPE);
        resp.setContentLength((int)file.length());

        FileInputStream in = new FileInputStream(file);
        OutputStream out = resp.getOutputStream();

        // Copy the contents of the file to the output stream
        byte[] buf = new byte[Constants.INPUT_BUFFER_SIZE];
        int count = 0;
        while ((count = in.read(buf)) >= 0) {
            out.write(buf, 0, count);
        }
        in.close();
        out.close();
    }
}

Then, I use GWT-RPC to get images, by using the following AppServiceImpl class:

public class AppServiceImpl extends RemoteServiceServlet implements AppService {

    //Get the base path for graphics root folder
    String basePath = System.getenv(Constants.ENV_VAR);

    @Override
    public ArrayList<String> getImageFiles(String folder) {
        ArrayList<String> list = new ArrayList<String>();
        File parent = new File(**basePath+folder**);
        if (!parent.isDirectory()) {
            return list;
        }
        File[] files = parent.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].isFile() && !files[i].isHidden()) {
                // save image title
                list.add(files[i].getName().substring(0, files[i].getName().indexOf(".")));
            }
        }
        return list;
    }
...

In order to get an image in GWT code, i use: final Image lgImg = new Image(Constants.URL_PREFIX + imgPath);

where:

  • URL_PREFIX = "http://localhost:8888/images?" + PARAMETER + "="

  • ENV_VAR = "APP_GFX_HOME"

  • PARAMETER = "path"

In development mode, all work good, but when i deploy to a tomcat server, images do not appear, and I need some help to figure out the problem.

p.s: Here is part of web.xml file

<!-- Services -->
    <servlet>
        <servlet-name>theImageServlet</servlet-name>
        <servlet-class>com.app.server.ImageServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>theImageServlet</servlet-name>
        <url-pattern>/images</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>appService</servlet-name>
        <servlet-class>com.app.server.AppServiceImpl</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>appService</servlet-name>
        <url-pattern>/app/services</url-pattern>
    </servlet-mapping>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凑诗 2024-12-15 12:33:57

我不会对 URL 进行硬编码,因为上下文部分在开发模式下不存在。
只需使用:

String url = GWT.getModuleBaseURL() + "images?";

我在类似的情况下使用它从报告 servlet 下载 Excel 文件,并且它在所有情况下都能正常工作。

I wouldn't hard-code the URL, because the context portion is not present in dev mode.
Just use:

String url = GWT.getModuleBaseURL() + "images?";

I use this in a similar situation to download Excel files from a reporting servlet and it works fine in all situations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文