Base64:java.lang.IllegalArgumentException:非法字符 [英] Base64: java.lang.IllegalArgumentException: Illegal character

查看:756
本文介绍了Base64:java.lang.IllegalArgumentException:非法字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在用户注册后发送确认电子邮件。我正在使用JavaMail库和Java 8 Base64 util类。

I'm trying to send a confirmation email after user registration. I'm using the JavaMail library for this purpose and the Java 8 Base64 util class.

我正在按以下方式编写用户电子邮件:

I'm encoding user emails in the following way:

byte[] encodedEmail = Base64.getUrlEncoder().encode(user.getEmail().getBytes(StandardCharsets.UTF_8));
Multipart multipart = new MimeMultipart();
InternetHeaders headers = new InternetHeaders();
headers.addHeader("Content-type", "text/html; charset=UTF-8");
String confirmLink = "Complete your registration by clicking on following"+ "\n<a href='" + confirmationURL + encodedEmail + "'>link</a>";
MimeBodyPart link = new MimeBodyPart(headers,
confirmLink.getBytes("UTF-8"));
multipart.addBodyPart(link);

其中 confirmationURL 是:

private final static String confirmationURL = "http://localhost:8080/project/controller?command=confirmRegistration&ID=";

然后以这种方式在ConfirmRegistrationCommand中对此进行解码:

And then decoding this in ConfirmRegistrationCommand in such way:

    String encryptedEmail = request.getParameter("ID");

    String decodedEmail = new String(Base64.getUrlDecoder().decode(encryptedEmail), StandardCharsets.UTF_8);

    RepositoryFactory repositoryFactory = RepositoryFactory
            .getFactoryByName(FactoryType.MYSQL_REPOSITORY_FACTORY);
    UserRepository userRepository = repositoryFactory.getUserRepository();
    User user = userRepository.find(decodedEmail);

    if (user.getEmail().equals(decodedEmail)) {
        user.setActiveStatus(true);
        return Path.WELCOME_PAGE;
    } else {
        return Path.ERROR_PAGE;
    }

当我尝试解码时:

http://localhost:8080/project/controller?command=confirmRegistration&ID=[B@6499375d

我收到 java.lang.IllegalArgumentException:非法base64字符5b

我尝试使用基本的编码/解码器(不是网址)但没有成功。

I tried to use basic Encode/Decoder (not URL ones) with no success.

已解决:

问题是下一个 - 在线:

The problem was the next - in the line:

 String confirmLink = "Complete your registration by clicking on following"+ "\n<a href='" + confirmationURL + encodedEmail + "'>link</a>";

我在一个字节数组上调用toString,所以我应该执行以下操作:

I'm calling toString on an array of bytes, so I should do the following:

String encodedEmail = new String(Base64.getEncoder().encode(
                user.getEmail().getBytes(StandardCharsets.UTF_8)));

感谢 Jon Skeet ByteHamster

推荐答案

您的编码文本是 [B @ 6499375d 。这不是Base64,在编码时出现问题。解码代码看起来不错。

Your encoded text is [B@6499375d. That is not Base64, something went wrong while encoding. That decoding code looks good.

使用此代码将byte []转换为String,然后再将其添加到URL中:

Use this code to convert the byte[] to a String before adding it to the URL:

String encodedEmailString = new String(encodedEmail, "UTF-8");
// ...
String confirmLink = "Complete your registration by clicking on following"
    + "\n<a href='" + confirmationURL + encodedEmailString + "'>link</a>";

这篇关于Base64:java.lang.IllegalArgumentException:非法字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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