使用Java将文件从FTP下载到本地会导致文件无法读取 - 编码问题 [英] Downloading files from FTP to local using Java makes the file unreadable - encoding issues
问题描述
我开发了一个代码,可以从FTP读取非常大的文件,并使用Java将其写入本地计算机。它的代码如下。这是来自下一个(Text key,Text value)
在的
RecordReader
> CustomInputFormat
if(!processed)
{
System.out .println(in processed);
in = fs.open(file);
processed = true;
while(bytesRead< = fileSize){
byte buf [] = new byte [1024];
尝试{
in.read(buf);
in.skip(1024);
bytesRead + = 1024;
long diff = fileSize-bytesRead;
if(diff <1024)
{
break;
}
value.set(buf,0,1024); //这是记录的值被设置的位置,并且它将映射到映射器。
}
catch(Exception e)
{
e.printStackTrace();
}
if(diff <1024)
{
int difference =(int)(fileSize-bytesRead);
字节buf [] =新字节[区别];
in.read(buf);
bytesRead + =差异;
}
System.out.println(closing stream);
in.close();
写入结束后,我看到传输已完成,文件大小目的地与源处相同。但是我无法打开文件,编辑器给出错误信息。
gedit一直未能检测到字符编码。
请检查您是否尝试打开二进制文件。
从菜单中选择一个字符编码并重试。
这个问题:与我相关的我相关,但是我无法读取文件,没有意义。 任何指针? 您的复制代码是完整的,完全是100%A级废话。在Java中复制流的规范方法如下: 摆脱所有其他的毛病。这只是浪费时间和空间,并明显损害您的数据传输。 I have a developed a code that reads very large files from FTP and writes it to local machine using Java. The code that does it is as follows . This is a part from the After the write is over , I see that the transfer is done and the size of the file at the destination is same as that at the source. But I am unable to open the file and the editor gives the error as This Question: Java upload jpg using JakartaFtpWrapper - makes the file unreadable is related to mine I believe , but I couldn't make sense of it. Any pointers ? Your copying code is complete and utter 100% A grade nonsense. The canonical way to copy a stream in Java is as follows: Get rid of all the other fluff. It is just wasting time and space and clearly damaging your data in transit. 这篇关于使用Java将文件从FTP下载到本地会导致文件无法读取 - 编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
int count;
byte [] buffer = new byte [8192]; ((count = in.read(buffer))> 0)
{
out.write(buffer,0,count); //如果你喜欢
,
}
next(Text key, Text value)
inside the RecordReader
of the CustomInputFormat
if(!processed)
{
System.out.println("in processed");
in = fs.open(file);
processed=true;
}
while(bytesRead <= fileSize) {
byte buf[] = new byte[1024];
try {
in.read(buf);
in.skip(1024);
bytesRead+=1024;
long diff = fileSize-bytesRead;
if(diff<1024)
{
break;
}
value.set(buf, 0, 1024); // This is where the value of the record is set and it goes to the mapper .
}
catch(Exception e)
{
e.printStackTrace();
}
}
if(diff<1024)
{
int difference= (int) (fileSize-bytesRead);
byte buf[] = new byte[difference];
in.read(buf);
bytesRead+=difference;
}
System.out.println("closing stream");
in.close();
gedit has not been able to detect the character coding.
Please check that you are not trying to open a binary file.
Select a character coding from the menu and try again.
int count;
byte[] buffer = new byte[8192]; // or more if you like
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}