当前位置:网站首页>解决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
边栏推荐
- PTA 天梯赛习题集 L2-001 城市间紧急救援
- Markdown tutorial
- Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!
- 【LeetCode】695-岛屿的最大面积
- Build your own semantic segmentation platform deeplabv3+
- College entrance examination admission score line crawler
- Beijing rental data analysis
- SQL transaction
- 【LeetCode】876-链表的中间结点
- [leetcode] 1905 statistics sub Island
猜你喜欢

已知兩種遍曆序列構造二叉樹

LeetCode刷题——统计各位数字都不同的数字个数#357#Medium

How to choose a third-party software testing organization for automated acceptance testing of mobile applications

Pytoch saves tensor to Mat file

Yolov5 code reproduction and server operation

PTA ladder game exercise set l2-001 inter city emergency rescue

MySQL -- Index Optimization -- order by

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

怎样从微信返回的json字符串中截取某个key的值?

Build your own semantic segmentation platform deeplabv3+
随机推荐
[leetcode] 189 rotation array
Engineer evaluation | rk3568 development board hands-on test
Redux——详解
【Experience Cloud】如何在VsCode中取得Experience Cloud的MetaData
【LeetCode】283-移动零
6096. Success logarithm of spells and potions
02.面向容器化后,必须面对golang
College entrance examination score line climbing
LeetCode刷题——递增的三元子序列#334#Medium
locate: 无法执行 stat () `/var/lib/mlocate/mlocate.db‘: 没有那个文件或目录
【LeetCode】19-删除链表的倒数第N个结点
04. Some thoughts on enterprise application construction after entering cloud native
[leetcode] 200 number of islands
终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)
[leetcode] 577 reverse word III in string
How to choose a third-party software testing organization for automated acceptance testing of mobile applications
. Net again! Happy 20th birthday
ssh/scp 使不提示 All activities are monitored and reported.
睿智的目标检测23——Pytorch搭建SSD目标检测平台
Download blender on Alibaba cloud image station