Java FtpClient retrieveFileStream[挂起]
我有一个本地FTP服务器(使用FileZilla服务器创建),并且几乎没有XLS文件。我需要将文件输入流返回到我的Java应用程序,以便解析数据等 为此,我尝试使用FTPClient,但出于某种原因,在尝试从文件(大于40KB)返回inputstream时,FTPClient.retrieveFileStream()挂起 我知道也有类似的问题,我都读过了,至少我能找到的都读过了,没有任何帮助。这真的很烦人,如果可能的话,我想得到一些帮助 相关代码部分:Java FtpClient retrieveFileStream[挂起],java,ftp,inputstream,Java,Ftp,Inputstream,我有一个本地FTP服务器(使用FileZilla服务器创建),并且几乎没有XLS文件。我需要将文件输入流返回到我的Java应用程序,以便解析数据等 为此,我尝试使用FTPClient,但出于某种原因,在尝试从文件(大于40KB)返回inputstream时,FTPClient.retrieveFileStream()挂起 我知道也有类似的问题,我都读过了,至少我能找到的都读过了,没有任何帮助。这真的很烦人,如果可能的话,我想得到一些帮助 相关代码部分: public FTPwrapper(){
public FTPwrapper(){
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Properties props = (Properties) context.getBean("ftp_props");
this.FTPhost = props.getProperty("host");
this.FTPuser = props.getProperty("user");
this.FTPpass = props.getProperty("pass");
this.FTPport = Integer.parseInt(props.getProperty("port"));
this.filesPath = props.getProperty("filesPath");
//start ftp connection
try {
client.connect(this.FTPhost, this.FTPport);
client.login(this.FTPuser, this.FTPpass);
client.setBufferSize(1024 * 1024);
client.enterLocalPassiveMode();
client.setFileType(FTP.BINARY_FILE_TYPE);
} catch (IOException e) {
e.printStackTrace();
}
}
FPT日志:
...login commands...
227 Entering Passive Mode
RETR /vtipsis_data/KT/2012 m muzieju statistika.xls
50 Opening data channel for file download from server of "/vtipsis_data/KT/2012 m muzieju statistika.xls" //Hangs...
ftp连接最终超时后,我得到java异常:
java.io.IOException: Your file contains 383 sectors, but the initial DIFAT array at index 4 referenced block # 505. This isn't allowed and your file is corrupt
at org.apache.poi.poifs.storage.BlockAllocationTableReader.<init>(BlockAllocationTableReader.java:103)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:151)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:322)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:303)
java.io.IOException:您的文件包含383个扇区,但索引4处的初始DIFAT数组引用了块#505。这是不允许的,并且您的文件已损坏
位于org.apache.poi.poifs.storage.BlockAllocationTableReader。(BlockAllocationTableReader.java:103)
位于org.apache.poi.poifs.filesystem.poifsffilesystem.(poifsffilesystem.java:151)
在org.apache.poi.hssf.usermodel.HSSFWorkbook上。(HSSFWorkbook.java:322)
在org.apache.poi.hssf.usermodel.HSSFWorkbook上(HSSFWorkbook.java:303)
我假设这个异常是因为只返回了XLS文件inputstream的一部分。这就是我的问题。有什么办法可以解决这个问题吗?这不是我想要的答案,而是一个解决方案,它必须满足。。。我没有使用
retrieveFileStream
直接检索流,而是使用retrieveFile
将文件作为临时系统文件下载,并将从retrieveFile
获得的OutputStream转换为InputStream。之后,我只是删除下载的临时文件
尝试使用it.sauronsoftware.ftp4j
,但它没有类似于retrieveFileStream
的方法/功能,只能下载文件
编辑:
我找到了一种不使用retrieveFileStream
或下载文件就返回文件inputstream的方法。由于retrieveFile
返回OutputStream,我可以将OutputStream转换为InputStream(为什么我以前没有想到这一点?)
只是说谢谢。。。为了表达我的感激之情:那些不想使用PowerMock并愿意重写代码以摆脱静态调用的人一定是个好人;-)
...login commands...
227 Entering Passive Mode
RETR /vtipsis_data/KT/2012 m muzieju statistika.xls
50 Opening data channel for file download from server of "/vtipsis_data/KT/2012 m muzieju statistika.xls" //Hangs...
java.io.IOException: Your file contains 383 sectors, but the initial DIFAT array at index 4 referenced block # 505. This isn't allowed and your file is corrupt
at org.apache.poi.poifs.storage.BlockAllocationTableReader.<init>(BlockAllocationTableReader.java:103)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:151)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:322)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:303)
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
client.retrieveFile(remoteFile, outputStream);
InputStream inputstream = new ByteArrayInputStream(outputStream.toByteArray());