jsp shows Chinese file name and absolute path under the image solution

  • 2020-06-07 05:09:27
  • OfStack

(1) jsp displays pictures of Chinese file names
Method 1. Change the server. xml file in Tomcat to:
 
<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:
 
<%@ page import="java.net.URLEncoder" %> 
<img src="...../.../URLEncoder.encode(" Pictures of .jpg","GBK")"/> 

Method 2. server. xml in tomcat
Add 1 attribute: URIEncoding=" UTF-8"
After modification, it is as follows:
 
<Connector port="8080" protocol="HTTP/1.1" 
maxThreads="150" connectionTimeout="20000" 
redirectPort="8443" URIEncoding="UTF-8"/> 

(2) Display the picture under the absolute path
Idea: Read the file stream from the local hard disk and use servlet to read the image and display it on the jsp page
servlet code:
 
public void doGet(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException { 
response.setContentType("text/html; charset=UTF-8"); 
response.setContentType("image/jpeg"); // Set the image format, which can be ignored here  
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(); 
} 
} 

Reference directly in the page < img src="servlet address "/ >
Visit the jsp page to display the images.
I think method 1 is the easiest, hehe! I have tried method 1, which can solve the problem that my Chinese filename is not displayed. Thank you xiaoxiaoxuewen very much.

Related articles: