【发布时间】:2016-09-03 04:09:40
【问题描述】:
我急需帮助。 在我的 android 应用程序中,我无法从名称中包含 UTF-8 字符的服务器下载文件。它适用于常规字符(英文字母)。服务器接收 UTF-8 字符为“?”。
例子:
名为“Preview-icon.png”的文件有效,但名为“Preview-iconČ.png”的文件不起作用,并且在搜索“Preview-icon?”时出现“找不到文件错误”。 png”而不是“Preview-iconČ”。
我已经找了一个星期的解决方案,但仍然没有找到。
下面是我下载文件的代码:
public void downloadFile(String url, String dest_file_path) {
try {
File dest_file = new File(dest_file_path);
URL u = new URL(url);
URLConnection conn = u.openConnection();
int contentLength = conn.getContentLength();
DataInputStream stream = new DataInputStream(u.openStream());
byte[] buffer = new byte[contentLength];
stream.readFully(buffer);
stream.close();
DataOutputStream fos = new DataOutputStream(new FileOutputStream(dest_file));
fos.write(buffer);
fos.flush();
fos.close();
} catch(FileNotFoundException e) {
Log.e("file","not found!");
return;
} catch (IOException e) {
Log.e("IO","error");
return;
}
参数url是一个包含url的常规字符串,例如。 “http://127.0.0.1:10001/downloadfile?filepath=/home/nikola/Desktop/Preview-icon.png&filename=Preview-icon.png”。并且 dest_file_path 是内部存储中的位置目标,例如。 “/mnt/sdcard/Preview-icon.png”。
【问题讨论】:
-
你用的是哪个浏览器?
-
@Mrunal None,这是用java编写的纯代码。
-
如何在 URL 中编码非 ASCII 字符? 提示:这是必须的。 我相信
Preview-iconČ.png应该编码为Preview-icon%C4%8C.png。 -
@Andreas 我使用过 URLEncoder,但似乎我在试错时删除了它。即使使用 URLEncoder,它也不适用于 UTF-8 字符。
-
可能服务器不支持 URL 中的 UTF-8,如果它是旧服务器。
标签: java android android-studio utf-8 cloud