The project encountered a problem where the file name to be downloaded is Chinese and there are spaces. If the connection is not processed for download, an error will be reported. To solve this problem, you only need to encode your url and then replace the space with the encoding.
Copy codeThe Code is as follows: // encode the path and replace all spaces in the path. After encoding, the space becomes "+", and the space encoding is "% 20". Therefore, replace all "+" with "% ". 20.
Copy codeThe Code is as follows: url = URLEncoder. encode (urlStr, "UTF-8"). replaceAll ("\\+", "% 20 ");
Copy codeThe Code is as follows: // The "/" in the encoded path also turns into something encoded, and all of them are replaced back. This is the complete path.
Copy codeThe Code is as follows: url = url. replaceAll ("% 3A", ":"). replaceAll ("% 2F ","/");
The above method can solve the problem of Chinese characters and spaces when handling link downloads. If there are other special characters, you can use this method to solve the problem.