| | |
| | | |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.net.HttpURLConnection; |
| | | import java.net.URL; |
| | | import java.net.URLEncoder; |
| | | |
| | | public class UrlDownloader { |
| | | |
| | |
| | | return connection.getInputStream(); |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | String urlString = "https://example.com/file.txt"; // 替换为你要下载的URL |
| | | |
| | | public static void main(String[] args) throws UnsupportedEncodingException { |
| | | String urlString = "http://192.168.110.188:9300/statics/2025/01/21/身份证人像面_20250121171827A014.png"; // 替换为你要下载的URL |
| | | //将路径中的中文进行编码 |
| | | //String urlString = "http://192.168.110.188:9300/statics/2025/01/21/%E8%BA%AB%E4%BB%BD%E8%AF%81%E4%BA%BA%E5%83%8F%E9%9D%A2_20250121171827A014.png"; |
| | | // 找到最后一个斜杠的位置 |
| | | int lastSlashIndex = urlString.lastIndexOf("/"); |
| | | // 从斜杠后提取文件名 |
| | | String baseUrl = urlString.substring(0, lastSlashIndex); |
| | | String fileName = urlString.substring(lastSlashIndex + 1); |
| | | String encode = URLEncoder.encode(fileName, "UTF-8"); |
| | | urlString = baseUrl + "/" + encode; |
| | | try (InputStream inputStream = downloadAsStream(urlString)) { |
| | | // 这里你可以处理输入流,例如将其写入文件或进行其他操作 |
| | | byte[] buffer = new byte[1024]; |