Java 如果';下载为zip';在爪哇

Java 如果';下载为zip';在爪哇,java,encoding,Java,Encoding,从远程路径下载文件夹时,文件名会获取垃圾字符 例如: 但在以Zip格式下载文件夹后,文件名包含垃圾字符,如½T½ñ.txt 代码段: if(isZip) { int lastIndex = remotePath.lastIndexOf('/'); filename = remotePath.substring(remotePath.lastIndexOf('/', lastIndex - 1) + 1, lastIndex); filename = filename.c

从远程路径下载文件夹时,文件名会获取垃圾字符

例如:

但在以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();
if(isZip){
int lastIndex=remotePath.lastIndexOf('/');
filename=remotePath.substring(remotePath.lastIndexOf('/',lastIndex-1)+1,lastIndex);
filename=filename.concat(“.zip”);
}
File File=新文件(localPath+“/”+文件名);
if(file.exists())
返回已存在的资源文件;
如果(!file.createNewFile()),则为else
返回-1;
InputStream reader=urlCon.getInputStream();
FileOutputStream编写器=新的FileOutputStream(文件);
//过程体
整数计数;
long-bytesSent=0;
字节[]inarray=新字节[65534];
长文件大小=((HttpsURLConnection)urlCon.getContentLength();
/*初始化数据传输统计信息*/
long timeStart=System.currentTimeMillis();
while(bytesent0)和运行){
writer.write(inarray,0,count);
字节数+=计数;
}
reader.close();
writer.flush();
writer.close();
如果我下载的文件夹没有压缩,那么文件名就不会损坏


如果有人能在这个问题上帮助我就好了。

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


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

这可能与编码有关。@Artur:除了看起来更像日语而不是汉语之外,这个问题不是关于语言的,而是关于解决编码问题的。我举了一个例子,我们使用的本地化语言也很少。你说“如果我以ZIP格式下载文件夹”。您使用哪种产品创建ZIP文件?也许这是产品的问题,而不是您的代码的问题。好吧,它尝试了好几次,但让我们再次尝试解释:您正在向我们展示一段代码,它只是按原样复制数据。这段代码似乎很好。问题在于,提供源数据的服务器和用于随后打开下载文件的工具必须就zip条目名称的字符编码达成一致。你关注的是这个链条中唯一一个对这个没有影响的部分。如果用浏览器下载文件,很可能会得到相同的结果。
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();