「已注销」 2019-10-01 22:10 采纳率: 50%
浏览 1637
已采纳

请教如何解决idea javaWeb项目无法找到指定位置的图片问题

JDBC把图片放进数据库


代码

Servlet

@WebServlet(
        name = "Servlet",
        urlPatterns = "/Servlet",
        loadOnStartup = 1
)//其他配置没有问题


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String sql = "INSERT INTO blob VALUES(DEFAULT,?,?)";
        JDBC.blob(connection, sql, "/resource/1.png", "image1");
        JDBC.disconnect(connection);
    }

JDBC类

    public static boolean blob(Connection connection, String sql, String resource, Object... args) {
        PreparedStatement ps = null;
        try {
            ps = connection.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }


                        //主要就这一句,resource是传入的文件路径字符串
            FileInputStream fis = new FileInputStream(new File(resource));





                        ps.setBlob(args.length + 1, fis);
            ps.execute();
            return true;
        } catch (SQLException | FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>$Title$</title>
</head>
<body>
<form action="Servlet" method="post">
    <input type="submit" value="submit">
    <img src="resource/1.png" alt="dada">
</form>
</body>
</html>

其他说明

在index.jsp界面可以显示图片



图片说明

configurations配置好了,下面的context和上面代码里填的一样



图片说明

文件已经在artifacts文件夹下面,但是找不到图片



图片说明

在浏览器中输入http://localhost:8080/resource/1.png或者http://localhost:8080/JavaWeb_war_exploded/resource/1.png都可以找到图片


试了其他路径都找不到~~

../../resource/1.png
/JavaWeb_war_exploded/resource/1.png
/JavaWeb_war_exploded/index.jsp
index.jsp

  • 写回答

2条回答 默认 最新

  • Json-Huang 2019-10-02 21:27
    关注

    src图片路经使用绝对路经试试,加上<%=request.getContextPath() %>,即类似:

    <img src=“<%=request.getContextPath() %>/resource/1.png" alt="dada">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵