从远程路径下载文件夹时,文件名出现垃圾字符。
例如:
remote path: "//10.7.456.78/abc/私はライオン"
local path: "C:\Users\test32\Desktop"
'私はライオン' folder contains 2 txt files
1. はライ.txt
2. ライ.txt但是在以Zip格式下载文件夹后,文件名中包含了像½T½ñ.txt这样的垃圾字符。
代码片段:
if(isZip) {
int lastIndex = remotePath.lastIndexOf('/');
filename = remotePath.substring(remotePath.lastIndexOf('/', lastIndex - 1) + 1, lastIndex);
filename = filename.concat(".zip");
}
File file = new File(localPath + "/" + filename);
if (file.exists())
return RES_FILE_EXISTS;
else if (!file.createNewFile())
return -1;
InputStream reader = urlCon.getInputStream();
FileOutputStream writer = new FileOutputStream(file);
// Process body
int count;
long bytesSent = 0;
byte[] inarray = new byte[65534];
long fileSize = ((HttpsURLConnection) urlCon).getContentLength();
/* Initialize data transfer statistics */
long timeStart = System.currentTimeMillis();
while (bytesSent < fileSize && ((count = reader.read(inarray)) > 0) && run) {
writer.write(inarray, 0, count);
bytesSent += count;
}
reader.close();
writer.flush();
writer.close();如果我下载的文件夹没有zip,那么文件名就不会损坏。
如果有人能在这个问题上帮我,那就太好了。
发布于 2014-01-06 06:07:30
很可能远程服务器和本地系统的系统字符编码不匹配,或者彼此不一致。
另一种可能是remotePath变量中的缺陷。代码片段没有显示任何关于remotePath变量是如何生成的线索。您可以检查remotePath变量是否包含正确的字符。
https://stackoverflow.com/questions/20261102
复制相似问题