当前位置:网站首页>解决BASE64Encoder报错的问题
解决BASE64Encoder报错的问题
2022-07-02 12:16:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
今天在用servlet写一个文件下载的案例时,想解决文件的中文显示问题。然后在导包import sun.misc.BASE64Encoder时,发现IDEA报错了,无法识别这个类。最后发现在JDK9后,官方就已经不支持import sun.misc.BASE64Encoder了。
网上说了很多,第一种是把JDK的版本降低到1.8以内。但是我觉得这不是解决的最好办法。我觉得应该去修改原有的代码。
所以我采用了官方提供了新的jar包import java.util.Base64。
下面这个java类的本意是为了,给文件名在不同的浏览器中,修改对应的编码格式,使其能正确显示中文。
这是用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浏览器
filename = URLEncoder.encode(filename, "utf-8");
filename = filename.replace("+", " ");
} else if (agent.contains("Firefox")) {
// 火狐浏览器
BASE64Encoder base64Encoder = new BASE64Encoder();
filename = "=?utf-8?B?" + base64Encoder.encode(filename.getBytes("utf-8")) + "?=";
} else {
// 其它浏览器
filename = URLEncoder.encode(filename, "utf-8");
}
return filename;
}
}
把BASE64Encoder base64Encoder = new BASE64Encoder();修改为Base64.Encoder encoder = Base64.getEncoder();
并把encode()方法改为encodeToString()。其他都无需修改。
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浏览器(版本太低的也无法打开)
filename = URLEncoder.encode(filename, "utf-8");
filename = filename.replace("+", " ");
} else if (agent.contains("Firefox")) {
// 火狐浏览器
Base64.Encoder encoder = Base64.getEncoder();
filename = "=?utf-8?B?" + encoder.encodeToString(filename.getBytes("utf-8")) + "?=";
} else {
// 其它浏览器
filename = URLEncoder.encode(filename, "utf-8");
}
return filename;
}
}
这样IDEA就不会报错了。
使用火狐浏览器打开后,中文名就可以显示了
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/147729.html原文链接:https://javaforall.cn
边栏推荐
- /bin/ld: 找不到 -lssl
- 二叉树前,中,后序遍历
- Libcurl Lesson 13 static library introduces OpenSSL compilation dependency
- 02.面向容器化后,必须面对golang
- 5. Practice: jctree implements the annotation processor at compile time
- 【LeetCode】695-岛屿的最大面积
- College entrance examination admission score line crawler
- /bin/ld: 找不到 -lxml2
- Bing. Site Internet
- 微信支付宝账户体系和支付接口业务流程
猜你喜欢
【Salesforce】如何确认你的Salesforce版本?
动态规划入门二(5.647.62)
Markdown tutorial
Evaluation of embedded rz/g2l processor core board and development board of Feiling
使用 percona 工具给 MySQL 表加字段中断后该如何操作
自定义异常
. Net again! Happy 20th birthday
[network security] network asset collection
Redux - detailed explanation
[leetcode] 1254 - count the number of closed Islands
随机推荐
6091. Divide the array so that the maximum difference is K
Name of institution approved in advance
[leetcode] 1254 - count the number of closed Islands
[network security] network asset collection
SQL stored procedure
beforeEach
ssh/scp 使不提示 All activities are monitored and reported.
【LeetCode】977-有序數組的平方
高考分数线爬取
二叉树前,中,后序遍历
【Salesforce】如何确认你的Salesforce版本?
[leetcode] 344 reverse string
04. Some thoughts on enterprise application construction after entering cloud native
【LeetCode】1140-石子游戏II
【LeetCode】1020-飞地的数量
已知兩種遍曆序列構造二叉樹
Redux - detailed explanation
Leetcode skimming -- count the number of numbers with different numbers 357 medium
Custom exception
Leetcode question brushing - parity linked list 328 medium