图片还是不能显示

来源:3-5 用户头像上传的代码实现

SomnusL

2020-07-15 23:45:04

Servlet的代码:

http://img.mukewang.com/climg/5f0f23f509f027cd07770237.jpg

http://img.mukewang.com/climg/5f0f23f5090b684b09080069.jpg

http://img.mukewang.com/climg/5f0f24420958e3e607210567.jpg

package web.servlet;

import java.io.IOException;
import java.util.List;

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 domain.User;
import service.UserService;
import service.impl.UserServiceImpl;

/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//接收数据
String username=request.getParameter("username");
String password=request.getParameter("password");
//数据封装
User user=new User();
user.setUsername(username);
user.setPassword(password);

//处理数据 完成登录
UserService userService=new UserServiceImpl();
//获得用户列表的集合
List<User> userList=(List<User>)getServletContext().getAttribute("userList");
User existUser=userService.login(userList,user);

//显示结果
if(existUser==null) {
request.setAttribute("msg", "用户名或者密码错误");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}else {
//将用户信息保存
request.getSession().setAttribute("existUser", existUser);
response.sendRedirect(request.getContextPath()+"/index.jsp");
}
}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}

Upload的代码

package utils;

import java.util.UUID;

public class UploadUtils {

public static String getUuidFileName(String fileName) {
//解决文件重名 获得后缀名 生成随机一段字符串将之前的替换掉
int idx=fileName.lastIndexOf(".");
String exName=fileName.substring(idx);
//生成随机字符串
String uuidFileName=UUID.randomUUID().toString().replace("-", "")+exName;
return uuidFileName;
}

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(UploadUtils.getUuidFileName("a.jpg"));
}

}

Jsp的代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c"%>
<div class="header">
<div class="logo fl">
<img src="image/logo.png">
</div>
<div class="menu fl">
<div class="menu-title"><a href="#">内容分类</a></div>
<ul>
<li><a href="#">现实主义</a></li>
<li><a href="#">抽象主义</a></li>
</ul>
</div>
<div class="auth fr">

<ul>
<c:if test="${ empty existUser }">
<li><a href="${pageContext.request.contextPath }/login.jsp">登录</a></li>
<li><a href="${pageContext.request.contextPath }/register.jsp">注册</a></li>
</c:if>
<c:if test="${ not empty existUser}">
<li>
<img class="profile_item" src="${ existUser.path }">
</li>
</c:if>

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


路径啥的没问题 控制台也显示 但是upload里的并不是图片文件

写回答

2回答

好帮手慕阿慧

2020-07-17

同学你好,

1、建议同学检查一下UploadUtils类getUuidFileName方法是否没有加文件后缀名,如下:

http://img.mukewang.com/climg/5f110afe090b728806840225.jpg

2、建议同学检查一下在RegistServlet类中,是否自己添加了文件后缀名。

如果问题没有解决,建议同学贴一下UploadUtils类和RegistServlet类的代码。

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

0

好帮手慕阿慧

2020-07-16

同学你好,建议同学检查一下页面上图片路径是否正确。如下:

http://img.mukewang.com/climg/5f0fc40f093ba34306570256.jpg

http://img.mukewang.com/climg/5f0fc46109cbaf5507760160.jpg

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

0
homnusL
h 路径是没问题啊。。。但就是不显示
h020-07-16
共1条回复

0 学习 · 9666 问题

查看课程