我用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);
的时候就内存溢出了,请问有什么解决办法吗?

解决方案 »

  1.   

    将一个文件按照指定大小分割成诺干个小文件
    换成txt格式就行了
      

  2.   

    你这个是处理本地文件吧?如果我直接把文件放到本地,不做切割也是可以读出来的,而且很快,解析加插入数据表也才不到3分钟。但是一旦从sftp服务器读就不行了。
      

  3.   


    能不能在服务器段把文件压缩下呀!txt文件压缩比还是很大的!
      

  4.   

    多线程进行分段读取同一个文件,比如线程1读取0-1000长度的数据,线程2读取1001-2000长度的数据,等等,这一不仅可以充分的利用本机的cpu,也可以加大本机对服务器上的带宽的占用,下载会快一些