Jsp<img>标签加载中文图片失败

本文介绍了解决JSP中&lt;img&gt;标签加载中文名图片失败的方法,通过将图片路径指向Servlet来实现正确加载,避免了中文路径引起的编码问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Jsp中<img>标签加载中文名字的图片失败解决办法,将img标签的src路径给一个Servlet负责显示图片即可

例子:

<img src="userPhotos/美女.jpg">

在WebRoot目录下有一个存放用户头像的目录userPhotos,其中一用户的头像图片命名"美女.jpg"发现路径没问题,但图片在页面上无法加载,将src的路径指向一Servlet的路径即可:

  <!-- 当加载的图片文件名为中文的时候无法加载,将src地址设为一服务器地址即可 -->
    <img src="imageServlet?flag=showImage&path=userPhotos/美女.jpg }" width="150px">
package com.test.action;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.log4j.Logger;

import com.test.pojo.User;
import com.test.service.UserService;
import com.test.service.UserServiceImpl;
import com.test.util.UUIDUtil;
import com.test.util.UploadUtil;

/**
 * 采用maven构建项目,并使用servlet3.0新特性采用注解的形式
 * @author QuLei
 *
 */
@WebServlet(name="ImageWeb",urlPatterns="/imageServlet")
public class ImageServlet extends HttpServlet {
	private Logger logger = Logger.getLogger(this.getClass());

/**
		 * The doGet method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to get.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
		 * The doPost method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to post.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		Class<? extends ImageServlet> clazz = this.getClass();
		String flag = request.getParameter("flag");
		try {
			Method method = clazz.getMethod(flag, HttpServletRequest.class,HttpServletResponse.class);
			method.invoke(this, request,response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

/**
	 * 查看图片
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void showImage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ServletOutputStream out = response.getOutputStream();
		int len = 0;
		InputStream in = this.getServletContext().getResourceAsStream(request.getParameter("path"));
		byte b [] = new byte[1024];
		while(-1!=(len = in.read(b))){
			out.write(b,0,len);
		}
		out.flush();
		out.close();
		in.close();
	}
}		

 

<%-- Created by IntelliJ IDEA. User: Administrator Date: 2025/6/4 Time: 22:04 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="java.sql.*"%> <html> <head> <title>Title</title> <style> body{ background-color: aquamarine; } .main{ background-color: antiquewhite; margin:0 auto; width:60%; padding-left: 40px; padding-right: 40px; } .main div{ background-color: #F0F8FF; padding:10px; } </style> </head> <body> <% response.setContentType("text/html;charset=UTF-8"); request.setCharacterEncoding("utf-8"); String bookid = request.getParameter("bookid"); Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/book"; String un = "root"; String pass = "root"; Connection con = DriverManager.getConnection(url,un,pass); String sql = "select * from books where id = ?" ; PreparedStatement pre = con.prepareStatement(sql); pre.setString(1,bookid); ResultSet rs = pre.executeQuery(); while(rs.next()){ String id = rs.getString("id"); String urls = rs.getString("url"); String name = rs.getString("bookname"); String writer = rs.getString("writer"); String syn = rs.getString("syn"); float price = rs.getFloat("price"); String stock = rs.getString("stock"); %> <form action="BOOKXIUGAI_OK.jsp?id=<%=id%>" method="post"> <div class="main"> <img src="<%=urls%>" width="180" height="256" alt="图片"/> <div>图片:<input type="text" name="urls" value="<%=urls%>"></div> <div>ID:<input type="text" name="id" disabled="disabled" value="<%=id%>"></div> <div>商品名:<input type="text" name="name" value="<%=name%>"></div> <div>品类:<input type="text" name="writer" value="<%=writer%>"></div> <div>简介:<textarea type="text" name="syn" rows="4" cols="50"><%=syn%></textarea></div> <div>价格:<input type="text" name="price" value="<%=price%>">元/件</div> <div> 库存:<input type="text" name="stock" value="<%=stock%>"></div> <div> <input type="submit" name="submit" value="提交"></div> <div> <a href="REMOVEBOOKS.jsp?id=<%=id%>">删除此书</a></div> <div><hr></div> </div> </form> <% }pre.close();rs.close(); %> </body> </html> 简单优化一下不要太复杂
最新发布
06-13
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值