简体   繁体   English

使用Java下载ftp csv文件

[英]ftp csv file download with java

I successfully download a csv file stored in ftp server using the code below. 我使用以下代码成功下载了保存在ftp服务器中的csv文件。

URL url = new URL(ftpUrl); 
URLConnection conn = url.openConnection();

InputStream inputStream = conn.getInputStream();
long filesize = conn.getContentLength();
byte[] buffer = new byte[4096];
int bytesRead = -1;

while ((bytesRead = inputStream.read(buffer)) != -1) {
    String str = new String(buffer, "UTF-8");
    out.println(str);
} 

The csv file has 1276KB filesize and about 20.000 rows. csv文件的文件大小为1276KB,约20.000行。 The problem is that the generated csv file has some lines either blank or with missing information. 问题在于,生成的csv文件的某些行为空或缺少信息。 The damaged rows occur about every 100 records. 损坏的行大约每100条记录发生一次。 I tried to fix by increasing the buffer size but damaged rows still exist. 我试图通过增加缓冲区大小来解决问题,但损坏的行仍然存在。 Any help is going to be appreciated :) 任何帮助将不胜感激:)

Sure! 当然! you have to tell how long is the string: except when the file size is a multiple of 4096, you will always get old items in buffer 您必须知道字符串的长度:除非文件大小是4096的倍数,否则总是会在buffer获取旧项

You must use this syntax instead 您必须改用此语法

String str = new String(buffer, 0, bytesRead, "utf-8");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM