idea上传图片显示问题
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>添加菜品</title> <style type="text/css"> </style> </head> <body> <center> <h1>菜品添加</h1> <form action="/foods/foodAddServlet" method="post" enctype="multipart/form-data"> <table border="1px" width="400px" cellspacing="0px" cellpadding="0px"> <tr> <td>菜品 ID</td> <td><input type="text" name="id"></td> </tr> <tr> <td>菜 名</td> <td><input type="text" name="foodName"></td> </tr> <tr> <td>口 味</td> <td> <input type="radio" name="taste" value="香辣">香辣 <input type="radio" name="taste" value="微辣">微辣 <input type="radio" name="taste" value="麻辣">麻辣 <input type="radio" name="taste" value="不辣">不辣 </td> </tr> <tr> <td>菜品图片</td> <td><input type="file" name="foodImage"></td> </tr> <tr> <td>价 格</td> <td><input type="text" name="price"></td> </tr> <tr> <td>菜品描述</td> <td> <textarea name="description"></textarea> </td> </tr> <tr style="text-align:center;width:20px"> <td colspan="2"> <input type="submit" value="添加"> <input type="reset" value="重置"> </td> </tr> </table> </form> </center> </body> </html>
package com.imooc.servlet;
import com.imooc.entity.Food;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
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.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@WebServlet(name = "FoodAddServlet", urlPatterns = "/foodAddServlet")
public class FoodAddServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//判断表单提交方式
if(ServletFileUpload.isMultipartContent(request)){
Food food = new Food();
Map<String,String> map = new HashMap<>();
List<Food> listFood = new ArrayList<>();
ServletFileUpload servletFileUpload = new ServletFileUpload(new DiskFileItemFactory());
try {
List<FileItem> list = servletFileUpload.parseRequest(request);
for(FileItem fileItem : list){
if(fileItem.isFormField()){//普通项
String name = fileItem.getFieldName();
String value = fileItem.getString("UTF-8");
map.put(name, value);
}else{//文件上传项
String name = fileItem.getName();
String path = this.getServletContext().getRealPath("/upload");
File file = new File(path);
if(file.exists() == false){
file.mkdir();
}
String url = path+"\\"+System.currentTimeMillis()+name;
System.out.println("url:"+url);
InputStream is = fileItem.getInputStream();
OutputStream os = new FileOutputStream(url);
int len = 0;
byte[] b = new byte[1024];
while((len=is.read())!=-1){
os.write(b,0,len);
}
is.close();
os.close();
map.put("_path", url);
}
}
food.setId(map.get("id"));
food.setName(map.get("foodName"));
food.setTaste(map.get("taste"));
food.setPrice(map.get("price"));
food.setContent(map.get("description"));
food.setPath(map.get("_path"));
listFood.add(food);
System.out.println("listFood:"+listFood);
this.getServletContext().setAttribute("listFood",listFood);
request.getRequestDispatcher("/showFoodList.jsp").forward(request,response);
} catch (FileUploadException e) {
e.printStackTrace();
}
}else{
}
}
}<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.imooc.entity.Food" %>
<%@ page import="java.util.List" %>
<%
List<Food> listFood = (List<Food>) request.getSession().getServletContext().getAttribute("listFood");
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>菜品信息展示</title>
<style type="text/css">
</style>
</head>
<body>
<center>
<h1>菜品查询</h1>
<table border="1px" cellspacing="0px" cellpadding="0px" width="800px">
<thead>
<tr>
<th>菜品ID</th>
<th>菜名</th>
<th>口味</th>
<th>菜品图片</th>
<th>价格</th>
<th>菜品描述</th>
</tr>
</thead>
<tbody>
<%
for(Food food : listFood){
%>
<tr>
<td><%=food.getId() %></td>
<td><%=food.getName() %></td>
<td><%=food.getTaste() %></td>
<td>
<img src="/foods/upload/<%=food.getPath().substring(food.getPath().lastIndexOf("\\")+1) %>">
</td>
<td><%=food.getPrice() %></td>
<td><%=food.getContent() %></td>
</tr>
<%
}
%>
</tbody>
</table>
</center>
</body>
</html>

问题:
1、图片不显示。
2、方便告知图片显示,应该用什么路径。好难理解感觉。
0
收起
正在回答
3回答
不是路径问题,是同学上传的图片是错的,打不开。
上传图片的时候,同学忘记传参数b 了:

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
好帮手慕阿莹
2018-12-05 15:38:36
同学可以在菜品展示的页面添加代码:
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
在图片的展示处:
<img
src="<%=basePath %>/upload/<%=food.getPath().substring(food.getPath().lastIndexOf("\\")+1) %>">
改成这个路径试试
<%=basePath %>中的代码是获取发布的路径,包括项目名。
food.getPath().substring(food.getPath().lastIndexOf("\\")+1) 这个其实是在获取图片的名称。
拼接上upload文件的名字。
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10204 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程

恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星