上传和下载文件时使用内存泄漏(可能是因为使用 ChannelSftp.get )?

您能透露一下内部工作原理吗ChannelSftp.get( "file_name")?如果频繁使用此函数,使用此函数从文件列表(10 kb 的 600 个文件)中获取单个文件会导致 Tomcat8 内存泄漏问题吗?


这是用于从远程服务器上传和获取文件的代码。


public String connectRemote(String host, String user, String password, String 

    remotePath, String sFileName) throws IOException, JSchException, SftpException 

{

    try {

        String sFileNames ="";

        Session session = connectSFTP(host, user, password);


        Channel channel = session.openChannel("sftp");

        channel.connect();

        ChannelSftp chan = (ChannelSftp) channel;


        try {


            String ftpRemoteDirectory = "/Irismedical/Outbox_Eligibility";


            chan.cd(ftpRemoteDirectory);


            File ftest = new File(sFileName);


            chan.put(new FileInputStream(ftest), ftest.getName());


            sFileNames =  ftest.getName();


        }

        catch (Exception e) {

            chan.disconnect();

            session.disconnect();

            return null;

        }


        chan.cd("/Irismedical/Inbox/Eligibility");


        String sRes = null;

        for (int i = 0;i<6 ; i++) {


            try {

                  sRes = convertInputStreamToString(

                             chan.get(sFileNames+"_Response_271_1.edi"));


                 if (sRes  != null ) {


                     break;


            } catch (Exception e ) {

                if (i<3) {

                    Thread.sleep(3000);

                } else {

                    Thread.sleep(5000);

                }

            }

        }

                    session.disconnect();


        return sRes;

    }

    catch (Exception e) {

        }

        return null;

    }

}


private static String convertInputStreamToString(InputStream inputStream) 

    throws IOException {


    ByteArrayOutputStream result = new ByteArrayOutputStream();

    byte[] buffer = new byte[1024];

    int length;

    while ((length = inputStream.read(buffer)) != -1) {

        result.write(buffer, 0, length);

    }


    return result.toString(StandardCharsets.UTF_8.name());

}


守着一只汪
浏览 199回答 1
1回答

慕码人2483693

您必须处理流。FileInputStream上传代码中的 和下载代码中InputStream返回的。ChannelSftp.get
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java