HttpServlet 下载图片中文名乱码问题

Posted hongbinglee

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HttpServlet 下载图片中文名乱码问题相关的知识,希望对你有一定的参考价值。

项目中用到下载图片,发现带中文图片名称有乱码问题,须将文件名进行URLEncode转码后,方可解决,特此记录

1.图片位置,例子名字命名为中文图片名.jpg.

技术图片

 

 2.web.xml 事例

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0"
         metadata-complete="true">
    <servlet>
        <servlet-name>download</servlet-name>
        <servlet-class>com.study.servlet.FileServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>download</servlet-name>
        <url-pattern>/down</url-pattern>
    </servlet-mapping>
</web-app>

3.后台Servlet代码,FileServlet.java

 1 package com.study.servlet;
 2 
 3 import javax.servlet.ServletContext;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.ServletOutputStream;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 import java.io.IOException;
10 import java.io.InputStream;
11 import java.net.URLEncoder;
12 
13 public class FileServlet extends HttpServlet {
14     @Override
15     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
16         doPost(req, resp);
17     }
18 
19     @Override
20     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
21         ServletContext context = this.getServletContext();
22         //InputStream in = context.getResourceAsStream("/static/images/11258.jpg");
23         InputStream in = context.getResourceAsStream("/static/images/中文图片名.jpg");
24         ServletOutputStream out = resp.getOutputStream();
25         //通过URLEncoder将中文名进行转码
26         resp.setHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode("中文图片名.jpg","UTF-8"));
27         byte [] buffer = new byte[1024];
28         int len = 0;
29         while((len = in.read(buffer))!= -1){
30             out.write(buffer,0,len);
31         }
32         in.close();
33         out.close();
34 
35     }
36 }

 

4.下载地址:http://localhost:8080/servlet_04_war/down,下载后的名称显示正常

技术图片

 

5.pom.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>JavaWebStudy</artifactId>
 7         <groupId>com.study</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <modelVersion>4.0.0</modelVersion>
11 
12     <artifactId>servlet-04</artifactId>
13     <packaging>war</packaging>
14 
15     <name>servlet-04 Maven Webapp</name>
16     <!-- FIXME change it to the project‘s website -->
17     <url>http://www.example.com</url>
18 
19     <properties>
20         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21         <maven.compiler.source>1.8</maven.compiler.source>
22         <maven.compiler.target>1.8</maven.compiler.target>
23     </properties>
24 
25     <dependencies>
26         <dependency>
27             <groupId>javax.servlet</groupId>
28             <artifactId>javax.servlet-api</artifactId>
29         </dependency>
30         <dependency>
31             <groupId>javax.servlet.jsp</groupId>
32             <artifactId>jsp-api</artifactId>
33         </dependency>
34     </dependencies>
35 
36     <build>
37         <finalName>servlet-04</finalName>
38 
39     </build>
40 </project>

 

以上是关于HttpServlet 下载图片中文名乱码问题的主要内容,如果未能解决你的问题,请参考以下文章

关于win10系统文字乱码?

java 后台如何获取前台上传的几张图片

poi导出excel 并处理插入网络图片 范例 处理文件下载中文乱码

当下载文件时,会出现文件名乱码(文件名中有中文)

请求中文乱码问题

C#HTML转图片乱码问题解决