I have created an URL to get the file on the server.
However, the file name may contains special characters like: ~!@#$%^&*()
or some Asian characters like Japanese, Chinese and Korean.
I have read this article and adopt the solution:
String dwnStr= new String("http://") + `HOST_IP_ADDRESS` + new String('/')
+ "picture/dog.jpg";
URL url= new URL(dwnStr);
URI uri= new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); // According to the official doc, it's in all cases.
url= uri.toURL();
(HttpURLConnection) url.openConnection();
And it gets well in the condition that if the dwnStr
doesn't contain any special character.By contrast,if the dwnStr
contains #
hash mark, the URL
constructor would drop the words after #
, taking it as fragment identifier.
Thus, I tried another solution (still in the above link. This time, I avoid to use URL
:
uri = new URI("http", null, HOST_IP_ADDRESS, 80, null,URLEncoder.encode("picture/dog.jpg", "UTF-8"), null);
It even can't work for the file name which doesn't contain any special characters.
What's the mistake I have made?
What strategy I can still adopt with?
ps: 1. I have tried the chosen solution in this article too. Of course, it doesn't work.
You need to call the toASCIIString()
method. It returns a String
that you can send to the remote server.
Another option is to use the IDN
class's static method toASCII
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.