HttpServlet 下载图片中文名乱码问题
2021-04-02 23:28
标签:read pre ati https file mamicode cti download site 项目中用到下载图片,发现带中文图片名称有乱码问题,须将文件名进行URLEncode转码后,方可解决,特此记录 1.图片位置,例子名字命名为中文图片名.jpg. 2.web.xml 事例 3.后台Servlet代码,FileServlet.java 4.下载地址:http://localhost:8080/servlet_04_war/down,下载后的名称显示正常 5.pom.xml HttpServlet 下载图片中文名乱码问题 标签:read pre ati https file mamicode cti download site 原文地址:https://www.cnblogs.com/HongBingLee/p/12546908.htmlDOCTYPE 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>downloadservlet-name>
servlet-class>com.study.servlet.FileServletservlet-class>
servlet>
servlet-mapping>
servlet-name>downloadservlet-name>
url-pattern>/downurl-pattern>
servlet-mapping>
web-app>
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 }
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>JavaWebStudyartifactId>
7 groupId>com.studygroupId>
8 version>1.0-SNAPSHOTversion>
9 parent>
10 modelVersion>4.0.0modelVersion>
11
12 artifactId>servlet-04artifactId>
13 packaging>warpackaging>
14
15 name>servlet-04 Maven Webappname>
16
17 url>http://www.example.comurl>
18
19 properties>
20 project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
21 maven.compiler.source>1.8maven.compiler.source>
22 maven.compiler.target>1.8maven.compiler.target>
23 properties>
24
25 dependencies>
26 dependency>
27 groupId>javax.servletgroupId>
28 artifactId>javax.servlet-apiartifactId>
29 dependency>
30 dependency>
31 groupId>javax.servlet.jspgroupId>
32 artifactId>jsp-apiartifactId>
33 dependency>
34 dependencies>
35
36 build>
37 finalName>servlet-04finalName>
38
39 build>
40 project>
上一篇:jsp快速入门
下一篇:select2 api参数的文档
文章标题:HttpServlet 下载图片中文名乱码问题
文章链接:http://soscw.com/index.php/essay/71598.html