前端图片无法展示

前端图片无法展示

相关截图:

http://img1.sycdn.imooc.com//climg/61169aaf099113c219201039.jpg

图片在路径中查看是存在的

http://img1.sycdn.imooc.com//climg/61169ad8096a95b219201039.jpg

数据库:

http://img1.sycdn.imooc.com//climg/61169b3b093902c619201039.jpg

项目目录:

http://img1.sycdn.imooc.com//climg/61169b9c095921ec05150731.jpg

http://img1.sycdn.imooc.com//climg/61169bb20968ac9d04780580.jpg

相关代码:

<%@page contentType="text/html;charset=utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<!-- Custom Theme files -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="" />

<!-- //Custom Theme files -->
<link href="css/bootstrap.css" type="text/css" rel="stylesheet" media="all">
<link href="css/style.css" type="text/css" rel="stylesheet" media="all">
<!-- js -->
<script src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/bootstrap-3.1.1.min.js"></script>
<!-- //js -->
<!-- cart -->
<script src="js/simpleCart.min.js"> </script>
<!-- cart -->
<script type="text/javascript">

</script>
</head>
<body>
<!--header-->
<div class="header">
<div class="container">
<nav class="navbar navbar-default" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<h1 class="navbar-brand"><a href="">IMOOC</a></h1>
</div>
<!--navbar-header-->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="/book/list.do" class="active">首页</a></li>

</ul>
</div>
</nav>




</div>
<div class="clearfix"> </div>
</div>
</div>
<!--//header-->
<!--banner-->
<div class="banner">
<div class="container" style="height: 300px">
<h2 class="hdng">IMOOC <span>图书</span></h2>
<p>读万卷书,行万里路</p>
<a href="#">SHOP NOW</a>
<!--
<div class="banner-text">
<img src="../../../images/tushu.jpeg" alt=""/>
</div>
-->
</div>
</div>
<!--//banner-->
<!--gallery-->
<div class="gallery">
<div class="container">
<div class="gallery-grids">
<c:forEach items="${book_list}" var="book">
<div class="col-md-3 gallery-grid " style="float:left">
<a href="#">
<img src="${book.imgPath}" class="img-responsive" alt=""/>
<div class="gallery-info">
<p><span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span> view</p>
<a class="shop" href="single.html">SHOP NOW</a>
<div class="clearfix"> </div>
</div>
</a>
<div class="galy-info">
<p style="size: 30px">${book.name}</p>
<div class="galry">
<div class="prices">
<h5 class="item_price">¥<fmt:formatNumber value="${book.price}" pattern="#,000.00" ></fmt:formatNumber></h5>
</div>
<div class="rating">
<c:forEach begin="1" end="${book.level}" var="i">
<span>☆</span>
</c:forEach>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
</c:forEach>
</div>
<div align="right">
<!-- 分页部分可以参照MyBatis拦截器的例子-->

</div>
</div>

</div>
<!--//gallery-->
<!--subscribe-->
<!--//subscribe-->
<!--footer-->
<!--//footer-->
<div class="footer-bottom">
<div class="container">
<p>Copyright © 2017 imooc.com All Rights Reserved | 京ICP备 13046642号-2</p>
</div>
</div>
</body>
</html>

相关代码:

package com.imooc.book.controller;

import com.imooc.book.entity.Book;
import com.imooc.book.service.BookService;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@WebServlet(name = "BookServlet", value = "/book")
public class BookServlet extends HttpServlet {
private BookService bookService = new BookService();

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

}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//使用FileUpload处理表单上传数据
//初始化
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload servletFileUpload = new ServletFileUpload(factory);//提供HTTP的请求解析
Book book = new Book();
//遍历表单输入项对象FileItem
try {
List<FileItem> list = servletFileUpload.parseRequest(request);
for (FileItem fi:list){
if (fi.isFormField()){
//处理普通输入项
switch (fi.getFieldName()){
case "name":
book.setName(fi.getString("utf-8"));
break;
case "categoryId":
book.setCategoryId(Long.parseLong(fi.getString()));
break;
case "level":
book.setLevel(Integer.parseInt(fi.getString()));
break;
case "price":
book.setPrice(Integer.parseInt(fi.getString()));
}
}else {
//处理文件上传输入项
String path = request.getServletContext().getRealPath("/img/");
System.out.println(path);
String fileName = UUID.randomUUID().toString();
String suffix = fi.getName().substring(fi.getName().lastIndexOf("."));//获取文件后缀名
fi.write(new File(path,fileName+suffix));
book.setImgPath(path+fileName+suffix);

}


}
book.setCreateTime(new Date());
book.setUpdateTime(new Date());

bookService.insertBook(book);
request.getRequestDispatcher("/category.jsp").forward(request,response);
} catch (Exception e) {
e.printStackTrace();
}


}
}


尝试过的解决方式:

查看过前面同学的问答。https://class.imooc.com/course/qadetail/288405 但是没看太懂,是因为在保存out目录中的就不能被web在启动时加载吗?看同学回答中的解决方法是将数据的img_path手动更改为webapp目录下的图片资源目录,那这样手动修改了,不就和程序的出发点相违背了吗?还是说要想办法在保存图片路径时将其同步保存到webapp目录下的img目录中去呢?

正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好,如果项目启动前将图片存在了out目录下,项目启动时会重新编译,重新加载out目录,会将项目的img目录同步到out目录下,所以这里同学按照上面同学回答的方式修改下路径试试

祝学习愉快~


  • 加1s的阿呱 提问者 #1

    老师我明白你的意思,我的问题是,在我理解中因为我使用的是FileUpload组件处理文件上传,其上传的图片被保存在了服务器实际发布运行时的目录中,也就是out目录中的img文件夹下,但是没有同步保存在本地webapp目录下的img目录中,当重启应用后去加载前端页面,就获取不到此图片,因为图片没有同步保存到本地的webapp目录下的img目录中,您看我这个理解对吗? 如果理解没问题的话,那我想加载图片的话,应该将图片同步保存至本地的webapp目录下的img目录中,那这个操作应该如何在新建图书.图片上传时完成呢?因为我不想手动去修改数据库中的图片路径,想在图片上传时就能获得一个正常的前端能够加载的路径


    2021-08-14 14:28:02
  • 好帮手慕阿园 回复 提问者 加1s的阿呱 #2

    同学你好,同学理解的是正确的,上传图片时获取到要上传的路径,然后将其保存到数据库中http://img1.sycdn.imooc.com//climg/5fbf0ec3091bed0a10190783.jpg

    上传的图片后会记录到数据库中,查询数据时会查询到数据库中的记录,将其返回给前端页面即可

    祝学习愉快~

    2021-08-14 18:29:08
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师