当前位置:网站首页>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
边栏推荐
- 怎样从微信返回的json字符串中截取某个key的值?
- /bin/ld: 找不到 -lssl
- matlab中wavedec2,说说wavedec2函数[通俗易懂]
- 使用FFmpeg命令行进行UDP、RTP推流(H264、TS),ffplay接收
- (4) Flink's table API and SQL table schema
- [leetcode] 200 number of islands
- 奥比中光 astra: Could not open “2bc5/[email protected]/6“: Failed to set USB interface
- 6096. Success logarithm of spells and potions
- 士官类学校名录
- Thoroughly understand browser strong cache and negotiation cache
猜你喜欢

2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)

【Experience Cloud】如何在VsCode中取得Experience Cloud的MetaData

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

微信支付宝账户体系和支付接口业务流程

已知两种遍历序列构造二叉树

【LeetCode】1905-统计子岛屿

(Video + graphic) machine learning introduction series - Chapter 5 machine learning practice
![[development environment] install Visual Studio Ultimate 2013 development environment (download software | install software | run software)](/img/26/3f19d36c048e669c736e27384e0fa7.jpg)
[development environment] install Visual Studio Ultimate 2013 development environment (download software | install software | run software)

Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium
![[leetcode] 1905 statistics sub Island](/img/82/d2f7b829f5beb7f9f1eabe8d101ecb.png)
[leetcode] 1905 statistics sub Island
随机推荐
[leetcode] 1020 number of enclaves
【idea】推荐一个idea翻译插件:Translation「建议收藏」
Astra: could not open "2bc5/ [email protected] /6“: Failed to set USB interface
【LeetCode】1162-地图分析
【LeetCode】695-岛屿的最大面积
Moveit obstacle avoidance path planning demo
Add an empty column to spark dataframe - add an empty column to spark dataframe
提前批院校名称
Redux - detailed explanation
/bin/ld: 找不到 -lxslt
彻底弄懂浏览器强缓存和协商缓存
List of sergeant schools
高考分数线爬取
高考录取分数线爬虫
2303. 计算应缴税款总额
fastjson List转JSONArray以及JSONArray转List「建议收藏」
6096. 咒语和药水的成功对数
【LeetCode】200-岛屿数量
MD5 encryption
使用FFmpeg命令行进行UDP、RTP推流(H264、TS),ffplay接收