Java 如何在strtus中显示服务器目录外的映像
这个问题是我上一个问题的继续,实际上我正在使用struts标签上传文件 但是现在我想显示从那个位置上传的图像,但是我得到了Java 如何在strtus中显示服务器目录外的映像,java,ajax,jsp,jakarta-ee,struts,Java,Ajax,Jsp,Jakarta Ee,Struts,这个问题是我上一个问题的继续,实际上我正在使用struts标签上传文件 但是现在我想显示从那个位置上传的图像,但是我得到了srclocation作为http://localhost:9443/D:/resources/images/img1.jpg这不是该图像的有效路径 如何访问服务器目录之外的映像。 这就是我用图像的绝对路径发送Ajax响应的方式 public ActionForward getAjaxUploadedFiles(ActionMapping mapping, ActionFor
src
location作为http://localhost:9443/D:/resources/images/img1.jpg
这不是该图像的有效路径
如何访问服务器目录之外的映像。
这就是我用图像的绝对路径发送Ajax响应的方式
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);
}
public ActionForward getAjaxUploadedFiles(ActionMapping映射、ActionForm表单、HttpServletRequest请求、HttpServletResponse响应)引发异常
{
字符串imagePath=“D:/resources/images/”;
ArrayList路径=新建ArrayList();
文件imageFile=新文件(imagePath);
File imageFiles[]=imageFile.listFiles();
对于(int i=0;i
这就是我在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'>";
});
$(响应).find(“imagePath”).each(函数(){
行=tblReportList.insertRow(0);
row.className=“TableBordergray”;
row.style.width=“100%”;
var imagePath=$(this.text();
单元格=行。插入单元格(0);
cell.innerHTML=“”;
});
但是在
img
标签上,我得到的图像路径是http://localhost:9443/D:/resources/images/img1.jpg
不能以这种方式渲染图像。Web服务器将图像路径视为相对路径,并在服务器上添加符合条件的url位置。例如,您应该创建一个服务图像的操作
<action path="/image" ... scope="request" validate="false"/>
然后呈现HTML格式
cell.innerHTML="<img src='" + '/image?path=' + imagePath + "' alt='" + imagePath + "' height='42' width='42'>";
cell.innerHTML=“”;
现在,创建将二进制图像数据写入响应输出流的操作。在操作中使用参数
path
,以便查找二进制输出文件。刷新输出后返回null
,因此struts不应进一步转发操作。您还可以添加标题以关闭缓存控制
,以确保从服务器检索图像 Hi下面是我问题的答案,我已经创建了用于显示图像的ImageServlet
,要执行的步骤:
1。您需要在web.xml文件中添加映射:
<servlet-name>ImageServlet</servlet-name>
<url-pattern>/ImageServlet/*</url-pattern>
<input type="image" alt='No image found' src='../ImageServlet/append image name that you want to display' />
3。在jsp端,您需要在步骤1中将映射添加到您的img
标记中,即输入type='image':
<servlet-name>ImageServlet</servlet-name>
<url-pattern>/ImageServlet/*</url-pattern>
<input type="image" alt='No image found' src='../ImageServlet/append image name that you want to display' />
您甚至可以创建
Action
类,并使用execute
方法来执行同样的操作。什么是src位置?感谢快速回复src位置是D:/resources/images/i.e.在服务器目录之外。将代码与注释一起发布到您得到的位置。很抱歉,我在发布时遇到此错误:少于10的用户问了8个小时后,声誉无法回答自己的问题。你可以在7小时内回答。在此之前,请使用评论或编辑您的问题。除非您有问题的答案,否则不应回答您的问题。如果要添加详细信息,请使用“编辑”功能。