gpt4 book ai didi

java - 为什么图像不显示在 JSP 页面上

转载 作者:行者123 更新时间:2023-12-02 05:18:43 24 4
gpt4 key购买 nike

我对以下情况有点迷茫:

  1. 用户上传图片 - upload.jsp (multipart/form-data)
  2. servlet 做所有肮脏的工作(保存图像、获取名称、保存名称、重定向到 display.jsp)
  3. 在display.jsp中,应该显示刚刚上传的图片

不幸的是,display.jsp 页面是空的。当我查看 firefox 下的源页面时,一切似乎都很好,提供了指向图像的有效链接。

<img src="/UploadTest/avatar/55_445194458350473498.png" border=0 width="48px" height="48px"/>

但是在媒体信息下我可以看到一些奇怪的信息:

Location:   http://localhost:8084/UploadTest/avatar/55_445194458350473498.png
Type: text/html
Size: Unknown (not cached)
Dimensions: 0px x 0px (scaled to 0px x 16px)

上传、处理和显示图片的代码如下:

上传.jsp

<form action="Upload" method="post" enctype="multipart/form-data">
<label for="file">File:</label>
<input type="file" id="file" name="file">
<input type="submit" value="submit">
</form>

上传.java

(MultipartMap servlet 属于 BalusC,http://balusc.blogspot.com.au/2009/12/uploading-files-in-servlet-30.html)

package test;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import test.MultipartMap;


@WebServlet(urlPatterns = { "/Users/Thomas/NetBeansProjects/UploadTest/web/Upload" })
@MultipartConfig(location = "/Users/Thomas/NetBeansProjects/UploadTest/web/avatar", maxFileSize = 10485760L) // 10MB.
public class UploadServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{

MultipartMap map = new MultipartMap(request, this);

File file = map.getFile("file");

String filename = file.getName();

HttpSession session = request.getSession();
session.setAttribute("filename", filename);

request.getRequestDispatcher("/display.jsp").forward(request, response);
}
}

显示.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<div>
<img src="${pageContext.request.contextPath}/avatar/${filename}" border=0 width="48px" height="48px"/>
<div>
</body>
</html>

如果我在 display.jsp 中将 ${filename} 替换为之前上传的特定图像的静态名称,则显示没有问题,所以我认为图像已正确处理,只是前端缺少某些内容?

顺便说一句:当调试器处于 Activity 状态时,一切正常,但当关闭时,问题又回来了。

干杯,

托马斯

最佳答案

很好的解释。我解决了你的问题并创建了它的样本。我和你面临同样的问题。解决方案是在 display.jsp 文件的下面一行:

<%@page isELIgnored="false" %>

我认为 EL 和页面的问题无法正确评估。下面是代码:upload.jsp 和 upload.java 和你的一样。

显示.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page isELIgnored="false" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<div>
<img src="${pageContext.request.contextPath}/images/${filename}" border=0 width="48px" height="48px" alt="Image Not found"/>
<div>
</body>
</html>

希望这对你也有用

谢谢

关于java - 为什么图像不显示在 JSP 页面上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12650434/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com