当前位置:网站首页>Solve the problem of base64encoder error
Solve the problem of base64encoder error
2022-07-02 15:45:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
I'm using it today servlet When writing a case of file download , I want to solve the problem of displaying documents in Chinese . Then guide the package import sun.misc.BASE64Encoder when , Find out IDEA Wrong report , This class is not recognized . Finally found in JDK9 after , The official has not supported import sun.misc.BASE64Encoder 了 .
There are a lot of words on the Internet , The first is to put JDK The version of is reduced to 1.8 within . But I don't think this is the best solution . I think we should modify the original code .
So I adopted the official new jar package import java.util.Base64.
The following java The original intention of class is to , Give the file name in different browsers , Modify the corresponding coding format , Make it display Chinese correctly .
This is the use of sun.misc.BASE64Encoder;
import sun.misc.BASE64Encoder;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
public class DownLoadUtils {
public static String getFileName(String agent, String filename) throws UnsupportedEncodingException {
if (agent.contains("MSIE")) {
// IE browser
filename = URLEncoder.encode(filename, "utf-8");
filename = filename.replace("+", " ");
} else if (agent.contains("Firefox")) {
// Firefox
BASE64Encoder base64Encoder = new BASE64Encoder();
filename = "=?utf-8?B?" + base64Encoder.encode(filename.getBytes("utf-8")) + "?=";
} else {
// Other browsers
filename = URLEncoder.encode(filename, "utf-8");
}
return filename;
}
}hold BASE64Encoder base64Encoder = new BASE64Encoder(); It is amended as follows Base64.Encoder encoder = Base64.getEncoder();
And put encode() Methods to encodeToString(). Nothing else needs to be modified .
import java.util.Base64;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
public class DownLoadUtils {
public static String getFileName(String agent, String filename) throws UnsupportedEncodingException {
if (agent.contains("MSIE")) {
// IE browser ( The version is too low to open )
filename = URLEncoder.encode(filename, "utf-8");
filename = filename.replace("+", " ");
} else if (agent.contains("Firefox")) {
// Firefox
Base64.Encoder encoder = Base64.getEncoder();
filename = "=?utf-8?B?" + encoder.encodeToString(filename.getBytes("utf-8")) + "?=";
} else {
// Other browsers
filename = URLEncoder.encode(filename, "utf-8");
}
return filename;
}
}such IDEA It won't be wrong .
Open it with Firefox , The Chinese name can be displayed
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/147729.html Link to the original text :https://javaforall.cn
边栏推荐
- Some problems about pytorch extension
- 奥比中光 astra: Could not open “2bc5/[email protected]/6“: Failed to set USB interface
- Leetcode skimming -- count the number of numbers with different numbers 357 medium
- (5) Flink's table API and SQL update mode and Kafka connector case
- For the problem that Folium map cannot be displayed, the temporary solution is as follows
- Cultural scores of summer college entrance examination
- 【LeetCode】977-有序數組的平方
- 【LeetCode】1162-地图分析
- Pytoch saves tensor to Mat file
- [leetcode] 1020 number of enclaves
猜你喜欢

Bing. Com website

Redux——详解

PTA 天梯赛习题集 L2-001 城市间紧急救援

Thoroughly understand browser strong cache and negotiation cache

Two traversal sequences are known to construct binary trees

Comparison between rstan Bayesian regression model and standard linear regression model of R language MCMC

Leetcode question brushing - parity linked list 328 medium

LeetCode刷题——奇偶链表#328#Medium

【Salesforce】如何确认你的Salesforce版本?

使用 percona 工具给 MySQL 表加字段中断后该如何操作
随机推荐
【LeetCode】1162-地图分析
使用 percona 工具给 MySQL 表加字段中断后该如何操作
Redux - detailed explanation
【LeetCode】876-链表的中间结点
[leetcode] 167 - sum of two numbers II - enter an ordered array
【LeetCode】877-石子游戏
自定义异常
folium地图无法显示的问题,临时性解决方案如下
/bin/ld: 找不到 -lcrypto
Aiko ai Frontier promotion (7.2)
《大学“电路分析基础”课程实验合集.实验五》丨线性有源二端网络等效电路的研究
[2. Basics of Delphi grammar] 3 Object Pascal constants and variables
/bin/ld: 找不到 -lgssapi_krb5
6096. 咒语和药水的成功对数
Leetcode skimming -- count the number of numbers with different numbers 357 medium
制作p12证书[通俗易懂]
SQL FOREIGN KEY
Basic knowledge of cryptography
密码学基础知识
XPT2046 四线电阻式触摸屏