Java 无法在jsp中加载图像?
我遇到了这个小问题,我发现在我的控制台中加载图片是不可能的,我不知道如何解决这个问题 我从数据库中获取图像名称,作为控制器中的字符串,只是名称,类似于“image.jpg”,它存储在我的文件夹“images”中 这是我的jsp文件:Java 无法在jsp中加载图像?,java,html,jsp,jakarta-ee,Java,Html,Jsp,Jakarta Ee,我遇到了这个小问题,我发现在我的控制台中加载图片是不可能的,我不知道如何解决这个问题 我从数据库中获取图像名称,作为控制器中的字符串,只是名称,类似于“image.jpg”,它存储在我的文件夹“images”中 这是我的jsp文件: <c:if test="${ !(post.cover == 'empty')}"> <div class="imgPub"> <img src="Assets/images/${post.cover }">
<c:if test="${ !(post.cover == 'empty')}">
<div class="imgPub">
<img src="Assets/images/${post.cover }">
</div>
</c:if>
在我的检查器中,我可以看到整个src都写得很准确,但旁边有一条消息说无法加载图像
任何帮助都将不胜感激。HttpServlet将完成这项工作 使用: youraddress.xxx/images/filename.png 这在@WebServlet(“/images/*”)中很重要 它将自动引导到路径中定义的文件夹,并根据名称检索图像
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
@WebServlet("/images/*")
public class ImageServlet extends HttpServlet {
public static final String PATH = "C:/"
/*
linux
public static final String PATH = "/home/images/"
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String filename = request.getPathInfo().substring(1);
File file = new File(PATH,filename);
response.setHeader("Content-Type", getServletContext().getMimeType(filename));
response.setHeader("Content-Length",String.valueOf(file.length()));
response.setHeader("Content-Disposition","inline; filename=\""+filename +"\"");
Files.copy(file.toPath(),response.getOutputStream());
}
}
尝试直接通过图像的url加载图像。问题与Jsp无关-您说它输出图像标记,所以问题是无法从您使用的url加载图像。对不起!通过其url加载我的图像?我不明白,如果我觉得这听起来很愚蠢,我很抱歉。。