UTF-8编码与Base-64编码 [英] UTF-8 encoding vs Base-64 Encoding

查看:155
本文介绍了UTF-8编码与Base-64编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对文件进行编码,可能是图像或任何pdf,然后将其发送到服务器。我必须遵循哪种类型的编码和解码。 (服务器和客户端都在我们公司。我们可以在两个地方编写逻辑)。 java中默认支持 UTF-8编码。并使用 Base-64 编码我必须导入外部jar。对于简单的文本,两种方式都很好。我正在使用tcp套接字编程。

I want to encode a file it may be image or any pdf and send it to server. Which type of Encoding and decoding I have to follow. (Both server and client is in our company. we can write logic in both place). UTF-8 Encoding is by default supported in java. and to use Base-64 encoding I have to import external jar. for simple texts both the ways are working fine. I am using tcp socket programming.

使用UTF-8编码

String str = "This is my Sample application";
        String urlEncodedData = URLEncoder.encode(str, "UTF-8"); // Encoding with UTF-8
        System.out.println("..after URL Encodingencoding..."+urlEncodedData );
        String retrievedData = URLDecoder.decode(urlEncodedData , "UTF-8");// Decoding with UTF-8
        System.out.println("..after decoding..."+retrievedData ); 

使用Base-64(使用commons.codec jar of apache

byte[] b =Base64.encodeBase64(str.getBytes()); //Encoding  base 64
        Base64.decodeBase64(b); // Decoding with Base 64


推荐答案

UTF -8是 text 编码 - 一种将文本编码为二进制数据的方法。

UTF-8 is a text encoding - a way of encoding text as binary data.

Base64在某种程度上是相反的 - 它是一种方式将任意二进制数据编码为ASCII文本。

Base64 is in some ways the opposite - it's a way of encoding arbitrary binary data as ASCII text.

如果需要将任意二进制数据编码为文本,Base64是要走的路 - 你一定不能尝试将任意二进制数据视为UTF-8编码的文本数据。

If you need to encode arbitrary binary data as text, Base64 is the way to go - you mustn't try to treat arbitrary binary data as if it's UTF-8 encoded text data.

然而,你很可能就像二进制数据一样将文件传输到服务器 - 这取决于你正在使用的传输方式。

However, you may well be able to transfer the file to the server just as binary data in the first place - it depends on what transport you're using.

这篇关于UTF-8编码与Base-64编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆