【问题标题】:GWT displaying image specified from servletGWT 显示从 servlet 指定的图像
【发布时间】:2025-12-22 07:35:11
【问题描述】:

我使用 servlet 访问 Web 容器外部的文件夹,以使用 GWT 将一些图形加载到 Web 应用程序。我在 servlet 中使用以下 sn-p 来测试这个想法:

        String s = null;
        File inputFile = new File("C:\\Documents and Settings\\User\\My Documents\\My Pictures\\megan-fox.jpg");
        FileInputStream fin = null;
        try {
            fin = new FileInputStream(inputFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        byte c[] = new byte[(int) inputFile.length()];
        try {
            fin.read(c);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            fin.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        String imgFolderPath = getServletContext().getRealPath("/")+"img";
        File imgFolder = new File(imgFolderPath);
        imgFolder.mkdir();

        File newImage = new File("megan-fox.jpg");
        FileOutputStream fout = null;
        try {
            fout = new FileOutputStream(newImage);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            fout.write(c);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            fout.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        boolean success = newImage.renameTo(new File(imgFolderPath, newImage.getName()));

servlet 中的代码从硬盘中的指定文件夹读取图像文件,在war 文件夹中创建一个名为“img”的新文件夹,并将jpg 文件复制到该文件夹​​。然后它将图像的路径(现在硬编码为)'/img/megan-fox.jpg'返回给客户端。 然后客户端使用 GWT 中的 Image 类和返回的路径字符串来显示图像,如下面的 sn-p:

public void onSuccess(String result) {
    String myImage = result;
    image = new Image(myImage);
    RootPanel.get().add(image);
    closeButton.setFocus(true);
}

我需要知道是否有一种方法可以在不使用在 Web 容器根目录中创建文件夹(可选)并将文件复制到那里以便使用 Image GWT 类访问它的“中间”步骤的情况下实现相同的结果并显示出来?

更新:原始 servlet 类。

public class GreetingServiceImpl extends RemoteServiceServlet implements
        GreetingService {

    // This method is called by the servlet container to process a GET request.
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // Get the absolute path of the image
        ServletContext sc = getServletContext();
            // i want to load the image in the specified folder (outside the web container)
        String filename = sc.getRealPath("C:\\Documents and Settings\\User\\My Documents\\My Pictures\\megan-fox.jpg");

        // Get the MIME type of the image
        String mimeType = sc.getMimeType(filename);
        if (mimeType == null) {
            sc.log("Could not get MIME type of "+filename);
            resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        }

        // Set content type
        resp.setContentType(mimeType);

        // Set content size
        File file = new File(filename);
        resp.setContentLength((int)file.length());

        // Open the file and output streams
        FileInputStream in = new FileInputStream(file);
        OutputStream out = resp.getOutputStream();

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

    // This is the method that is called from the client using GWT-RPC
    public String greetServer(String input) throws IllegalArgumentException {
        HttpServletRequest req = this.getThreadLocalRequest();
        HttpServletResponse res = this.getThreadLocalResponse();
        try {
            doGet(req, res);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // actually i dont know what that means but i thought i would have to returned something like the image's url?
        return res.encodeURL("/img/image0.png"); 
    }
}

我在逻辑上滥用了为解决我的问题而提出的方法。正确的方法是什么?

【问题讨论】:

    标签: gwt servlets gwt-rpc


    【解决方案1】:

    当然,让您的 servlet 直接提供图像:

    1. Content-Type 标头设置为image/jpeg
    2. 将图像文件内容写入 servlet 响应编写器。

    这里是an example

    【讨论】:

    • 抱歉,我无法编写您的想法 :( 我将 doPost 方法添加到我的 servlet 类中,但我认为在 String filename = sc.getRealPath("image. jpg") 是与 web 容器根目录相关的。如果我想访问文件夹 c:\\myimage.jpg 中的图像怎么办?可能我不明白这个方法,你能告诉我如何使用我更新的第一篇文章中包含的 servlet 类中的这个方法?
    • 这段代码只是一个例子——你不需要照字面理解。只需对文件使用绝对路径:File imageFile = new File("c:\\myimage.jpg") 并手动设置内容类型resp.setContentType("image/jpeg")
    • 链接断开 - 请修复