Cra*_*ath 2 java ftp inputstream
我有一个本地 FTP 服务器(使用 FileZilla 服务器创建),并且很少有 XLS 文件。我需要将文件输入流返回到我的 Java 应用程序,以便我可以解析数据等。
为此,我尝试使用 FTPClient,但出于某种原因,在尝试从文件(大于 40KB 或)返回输入流时,FTPClient.retrieveFileStream() 挂起。
我知道有与此类似的问题,我已经阅读了所有内容,至少我能找到的所有内容都已阅读,但没有任何帮助。这真的很烦人,如果可能的话,我想要一些帮助。
相关代码部分:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
public InputStream returnFileStream(String FileName){
InputStream inputstream = null;
try {
String remoteFile = this.filesPath+FileName;
inputstream = client.retrieveFileStream(remoteFile);
client.completePendingCommand();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (client.isConnected()) {
client.logout();
client.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return inputstream;
}
Run Code Online (Sandbox Code Playgroud)
public static void main(String[] args){
FTPwrapper ftp = new FTPwrapper();
InputStream inputstream = ftp.returnFileStream("2012 m muzieju statistika.xls");
}
Run Code Online (Sandbox Code Playgroud)
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...
Run Code Online (Sandbox Code Playgroud)
在 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)
Run Code Online (Sandbox Code Playgroud)
我认为这个异常是因为只返回了我的 XLS 文件输入流的一部分。所以就是这样,我的问题。任何想法如何解决这个问题?
这不是我想要的答案,而是一种解决方法,它必须足够......我没有尝试直接检索流,而是使用retrieveFileStream,我曾经retrieveFile将文件下载为临时系统文件,并将我从中获得的 OutputStream 转换retrieveFile为 InputStream . 之后我只是删除了下载的临时文件。
尝试使用it.sauronsoftware.ftp4j,但它没有类似的方法/功能retrieveFileStream,只能下载文件。
编辑:
我找到了一种无需使用retrieveFileStream或下载文件即可返回文件输入流的方法。由于retrieveFile返回 OutputStream,我可以将 OutputStream 转换为 InputStream(为什么我之前没有想到这一点?),例如:
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
client.retrieveFile(remoteFile, outputStream);
InputStream inputstream = new ByteArrayInputStream(outputStream.toByteArray());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7660 次 |
| 最近记录: |