Java JDK7中Base64类的使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34469472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:40:54  来源:igfitidea点击:

Use of Base64 class in JDK7

java

提问by Shiladittya Chakraborty

I am trying to use Base64class in jdk7 but I am getting Base64 cannot be resolvederror. Why eclipse throw this error?

我正在尝试Base64在 jdk7 中使用类,但Base64 cannot be resolved出现错误。为什么eclipse会抛出这个错误?

I am using below code

我正在使用下面的代码

byte[] imageData = Base64.getDecoder().decode(readFile(imagePart.getInputStream()));

even import statement also shows the same error : import java.util.Base64;

即使导入语句也显示相同的错误: import java.util.Base64;

Is this class not available in jdk7?

这个类在jdk7中不可用吗?

采纳答案by Henry

From the documentation:

文档

Since: 1.8

自:1.8

So no, it is not available in jdk 7.

所以不,它在 jdk 7 中不可用。

回答by Azat Nugusbayev

Base64.getDecoder().decode() is available from 1.8

Base64.getDecoder().decode() 可从 1.8

Try to use Google Guava.

尝试使用谷歌番石榴

POM

聚甲醛

<dependency>
   <artifactId>guava</artifactId>
   <groupId>com.google.guava</groupId>
   <type>jar</type>
   <version>14.0.1</version>
</dependency>

Code Snippet

代码片段

String inputContent = "Hello World";
String base64String = BaseEncoding.base64().encode(inputContent.getBytes("UTF-8"));
//decode
System.out.println("Base64:" + base64String);
byte[] contentInBytes = BaseEncoding.base64().decode(base64String);
System.out.println("Source content: " + new String(contentInBytes, "UTF-8"));//Hello World

回答by awsome

java.util.Base64 is available in Java 8 or better

java.util.Base64 在 Java 8 或更高版本中可用

In Java 7 you can use Apache Commons Codec

在 Java 7 中,您可以使用Apache Commons Codec

See here for examples http://www.rgagnon.com/javadetails/java-0598.html

有关示例,请参见此处http://www.rgagnon.com/javadetails/java-0598.html

回答by predrags

If it is needed to specifically use JDK7 for your project, and you still need to use java.util.Base64class, you can include in your project the code for that class from OpenJDK.

如果您的项目需要专门使用 JDK7,并且您仍然需要使用java.util.Base64类,您可以在您的项目中包含 OpenJDK 中该类的代码。

Source for this class is available at:
http://www.grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/util/Base64.java?av=f

此类源可从以下网址获得:http:
//www.grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/util/Base64.java?av=f

Base64.javafile can be downloaded at:
http://www.grepcode.com/file_/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/util/Base64.java/?v=source&disposition=attachment

Base64.java文件可以在以下位置下载:http:
//www.grepcode.com/file_/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/util/Base64.java/?v=来源&处置=附件

回答by Zac

As stated java.util.Base64 is not available until 8.

如前所述,java.util.Base64 直到 8 才可用。

However I find it worth noting that Android developers have access to android.util.Base64 at language level 7 (API 24) and the codeis entirely self contained so you can copy and drop it into your project if you need Base64 and don't want to use a 3rd party like Apache. Just watch rights and usage and all that.

但是我发现值得注意的是,Android 开发人员可以在语言级别 7(API 24)上访问 android.util.Base64 并且代码是完全自包含的,因此如果您需要 Base64 并且不需要 Base64,您可以将其复制并放入您的项目中想使用像Apache这样的 3rd 方。只需注意权利和使用情况以及所有这些。