如何显示的图像,在strtus外服务器目录

问题描述:

这个问题延续到我的previous问题Accessing外部文件到Web应用程序,其实我使用Struts标签上传文件< HTML:文件属性=文件/>

This question is continuation to my previous question Accessing External Files Into Our Web Application, actually I am uploading file using struts tag <html:file property="file" />

但现在我想表现的上传的图片从该位置,但我正在的src 位置的http://本地主机:9443 / D :/resources/images/img1.jpg 这是不是该图像的有效路径

But now I wanted to show the uploaded images from that location but I am getting src location as http://localhost:9443/D:/resources/images/img1.jpg which is not a valid path for that image.

如何访问图像这是我的服务器目录之外。

这是怎样的图像,我发送Ajax响应是绝对路径

This is how I am sending Ajax response with Absolute path of images

public ActionForward getAjaxUploadedFiles(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
    {

        String imagePath = "D:/resources/images/";
        ArrayList<String> path = new ArrayList<String>();

        File imageFile = new File(imagePath);
        File imageFiles[] = imageFile.listFiles();

        for (int i = 0; i < imageFiles.length; i++) {
            path.add(imageFiles[i].getAbsolutePath());
        }

        PrintWriter out = response.getWriter();
        response.setContentType("text/xml");
        response.setHeader("Cache-Control", "no-cache");
        response.setStatus(HttpServletResponse.SC_OK);

        StringBuffer strXMl = new StringBuffer();
        strXMl.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        strXMl.append("<start>"); 


        for (String imagePth : path) {
            strXMl.append("<imagePath>");
            strXMl.append(imagePth);
            strXMl.append("</imagePath>");
        }

        strXMl.append("</start>");

        if(strXMl != null){ 
            String Xml = strXMl.toString();
            out.write(Xml);
            System.err.println("XMl Reponse is: " + Xml);
        }
        else {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        }
        out.flush();

        return mapping.findForward(null);
    }

这是我如何在JSP呈现的图像

This is how I am rendering images at JSP

 $(response).find("imagePath").each(function() {
            row = tblReportList.insertRow(0);
            row.className="TableBordergray";
            row.style.width="100%";

            var imagePath = $(this).text();

            cell = row.insertCell(0);
            cell.innerHTML="<img src='" + imagePath + "' alt='" + imagePath + "' height='42' width='42'>";
        });

IMG 标签我得到的图像路径​​的http://本地主机:9443 / D:/resources/images/img1.jpg

but at img tag I am getting image path as http://localhost:9443/D:/resources/images/img1.jpg

您不能呈现这样的方式形象。 Web服务器处理图像路径作为相对的,在服务器上添加合格的URL位置。您应该创建服务于图像的动作,例如:

You can't render images in such way. Web server treated your image path as relative and add qualifying url location on the server. You should create an action to serve images, for example

<action path="/image" ... scope="request" validate="false"/>

然后渲染HTML一样

Then render HTML like

cell.innerHTML="<img src='" + '/image?path=' + imagePath + "' alt='" + imagePath + "' height='42' width='42'>";

现在,创建编写的二进制图象数据到响应输出流的动作。您可以在让你找到二进制输出文件的操作参数路径。冲洗输出返回后所以支柱不应该在行动进一步向前发展。你也可以添加页眉关掉一个的Cache-Control ,以确保图像是从服务器检索。

Now, create the action that write the binary image data to the response output stream. Take a parameter path in the action that let you find a file for binary output. After the flushing output return null so struts should not forward the action further. You could also add headers to turn off a Cache-Control to make sure the images are retrieved from the server.