jsp下显示中文文件名的图片解决办法
(1)jsp显示中文文件名的图片
方法一、将Tomcat中的server.xml文件中改为:
<Connector port="8080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="GBK" />
jsp页面:
<%@ page import="java.net.URLEncoder" %>
<img src="...../.../URLEncoder.encode("图片名.jpg","GBK")"/>
方法二、在tomcat的server.xml
增加一个属性:URIEncoding="UTF-8"
修改之后为:
<Connector port="8080" protocol="HTTP/1.1"
maxThreads="150" connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>
(2)显示绝对路径下的图片
思路:将本地硬盘下的文件读取文件流,使用servlet读取图片显示在jsp页面上
servlet代码:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8");
response.setContentType("image/jpeg"); //设置图片格式格式,这里可以忽略
FileInputStream fis = new FileInputStream("D:/ftp/xxx.jpg");
OutputStream os = response.getOutputStream();
try {
int count = 0;
byte[] buffer = new byte[1024*1024];
while ( (count = fis.read(buffer)) != -1 )
os.write(buffer, 0, count);
} catch (IOException e){
e.printStackTrace();
}finally {
if(os!=null)
os.close();
if(fis != null)
fis.close();
}
}
在页面里直接引用<img src="servlet地址" />
访问jsp页面就可以显示图片了。
补充:Web开发 , Jsp ,