Java: Encode mailto links using themes and plain text multi-line bodies … here is a solution to the problem.
Java: Encode mailto links using themes and plain text multi-line bodies
I googled a lot but couldn’t find a solution to the problem. I need to encode the mailto: link with subject and body as UTF-8 in java.
The body consists of plain text
There is no way to encode:
- The
- entire string in UTF-8, for example Ä to %C3%84S
- Blank to %20 instead of +
- r\n Enter %0D%0A
- / to %2f
\
Thanks for your help!
Solution
You want URLEncode.encode(String s, String enc) method. The second argument should be the string UTF-8
.
It does encode spaces as +
instead of %20
, which is valid for query parameters; You can always make it a special case and replace all the former with the latter. Example:
import java.net.URLEncoder;
import java.util.regex.Pattern;
public class Foobar {
private static Pattern space = Pattern.compile("\\+");
public static void main(String[] args) throws Exception {
String first = URLEncoder.encode("Ä+ \r\n/", "UTF-8");
String second = space.matcher(first).replaceAll("%20");
System.out.println(second);
}
}
This output:
%C3%84%2B%20%0D%0A%2F