当前位置:网站首页>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
边栏推荐
猜你喜欢
Bing. Com website
怎样从微信返回的json字符串中截取某个key的值?
LeetCode刷题——奇偶链表#328#Medium
《大学“电路分析基础”课程实验合集.实验五》丨线性有源二端网络等效电路的研究
《大学“电路分析基础”课程实验合集.实验七》丨正弦稳态电路的研究
Basic knowledge of cryptography
Leetcode skimming - remove duplicate letters 316 medium
Leetcode skimming -- incremental ternary subsequence 334 medium
[leetcode] 1162 map analysis
PTA 天梯赛习题集 L2-001 城市间紧急救援
随机推荐
【LeetCode】876-链表的中间结点
《大学“电路分析基础”课程实验合集.实验五》丨线性有源二端网络等效电路的研究
[leetcode] 486 predict winners
已知兩種遍曆序列構造二叉樹
爱可可AI前沿推介(7.2)
lseek 出错
MD5加密
终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)
For the problem that Folium map cannot be displayed, the temporary solution is as follows
[leetcode] 1254 - count the number of closed Islands
制作p12证书[通俗易懂]
【LeetCode】977-有序數組的平方
PostgresSQL 流复制 主备切换 主库无读写宕机场景
Pyinstaller打包exe附带图片的方法
6.12 critical moment of Unified Process Platform
[leetcode] 977 - carré du tableau ordonné
(5) Flink's table API and SQL update mode and Kafka connector case
Folium, diagnosis and close contact trajectory above
[leetcode] 189 rotation array
LeetCode刷题——奇偶链表#328#Medium