(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頁面就可以顯示圖片了。