0
votes

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>
1

1 Answers

0
votes

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.