如果在Java中“下载为zip”,则双字节文件名会损坏

时间:2013-11-28 08:30:32

标签: java encoding

从远程路径下载文件夹时,文件名会变成垃圾字符。

例如:

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的文件夹,那么文件名就不会被破坏。

如果有人能在这个问题上帮助我,那将是件好事。

1 个答案:

答案 0 :(得分:0)

远程服务器和本地系统的系统字符编码很可能不匹配,或者彼此不符合。

另一种可能性是remotePath变量中的缺陷。代码片段未显示如何生成remotePath变量的任何线索。您可以检查remotePath变量是否包含正确的字符。

相关问题