当前位置:网站首页>解决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
边栏推荐
- [experience cloud] how to get the metadata of experience cloud in vscode
- 动态规划入门一,队列的bfs(70.121.279.200)
- 【LeetCode】695-岛屿的最大面积
- Redux——详解
- 【LeetCode】200-岛屿数量
- Infra11199 database system
- 【idea】推荐一个idea翻译插件:Translation「建议收藏」
- . Solution to the problem of Chinese garbled code when net core reads files
- 03. Preliminary use of golang
- PTA 天梯赛习题集 L2-001 城市间紧急救援
猜你喜欢
[leetcode] 417 - Pacific Atlantic current problem
Build your own semantic segmentation platform deeplabv3+
Wechat Alipay account system and payment interface business process
4. Jctree related knowledge learning
Thoroughly understand browser strong cache and negotiation cache
Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium
How to avoid 7 common problems in mobile and network availability testing
损失函数与正负样本分配:YOLO系列
[salesforce] how to confirm your salesforce version?
百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香
随机推荐
Engineer evaluation | rk3568 development board hands-on test
Yolov5 code reproduction and server operation
Summary of the first three passes of sqli Labs
How to avoid 7 common problems in mobile and network availability testing
03. Preliminary use of golang
PostgresSQL 流复制 主备切换 主库无读写宕机场景
百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香
密码学基础知识
Bing. Site Internet
睿智的目标检测23——Pytorch搭建SSD目标检测平台
/bin/ld: 找不到 -lxslt
【LeetCode】977-有序數組的平方
Deux séquences ergodiques connues pour construire des arbres binaires
【LeetCode】1020-飞地的数量
6096. Success logarithm of spells and potions
LeetCode刷题——验证二叉树的前序序列化#331#Medium
[leetcode] 167 - sum of two numbers II - enter an ordered array
MD5 encryption
树-二叉搜索树
[experience cloud] how to get the metadata of experience cloud in vscode