java读取SFTP服务器上的大数据文件
我用java读取sftp上一个有300多万条数据的txt文件,文件大小500多M,代码如下
public FilePoJo read(String fileUid, String folderName) throws Exception {
ChannelSftp sftp = connect();
ByteArrayOutputStream writer = null;
FilePoJo pojo = null;
try {
sftp.cd(folderName);
writer = new ByteArrayOutputStream();
sftp.get(new String(fileUid.getBytes("UTF-8"), "ISO-8859-1"), writer);
byte[] content = writer.toByteArray();
pojo = new FilePoJo();
pojo.setName(getFileName(fileUid));
pojo.setUuid(fileUid);
pojo.setContent(content);
pojo.setExtendName(getExtendName(fileUid));
}
catch (SftpException e)
{
logger.error("execute read(fileUid, folderName) function failure, pls check the parameters of fileUid and folderName!");
throw e;
}
finally
{
if(writer != null)
{
writer.close();
}
disconnect(sftp);
}
return pojo;
}
每次执行到
sftp.get(new String(fileUid.getBytes("UTF-8"), "ISO-8859-1"), writer);
的时候就内存溢出了,请问有什么解决办法吗?