当前位置:网站首页>解决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
边栏推荐
猜你喜欢

Redux - detailed explanation

Yolov5 code reproduction and server operation

SQL stored procedure

Redux——详解

Thoroughly understand browser strong cache and negotiation cache

Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium

03. Preliminary use of golang

03.golang初步使用

Engineer evaluation | rk3568 development board hands-on test

Bing.com網站
随机推荐
动态规划入门二(5.647.62)
Finally, I understand the event loop, synchronous / asynchronous, micro task / macro task, and operation mechanism in JS (with test questions attached)
List of sergeant schools
[development environment] install the Chinese language pack for the 2013 version of visual studio community (install test agents 2013 | install visual studio 2013 simplified Chinese)
【LeetCode】1140-石子游戏II
04.进入云原生后的企业级应用构建的一些思考
LeetCode刷题——两整数之和#371#Medium
【网络安全】网络资产收集
6095. 强密码检验器 II
folium地图无法显示的问题,临时性解决方案如下
Bing. Com website
[leetcode] 19 delete the penultimate node of the linked list
6.12 企业内部upp平台(Unified Process Platform)的关键一刻
Force deduction solution summarizes the lucky numbers in 1380 matrix
LeetCode刷题——去除重复字母#316#Medium
【idea】推荐一个idea翻译插件:Translation「建议收藏」
How to avoid 7 common problems in mobile and network availability testing
2279. Maximum number of backpacks filled with stones
MySQL calculate n-day retention rate
6095. Strong password checker II