您能透露一下內(nèi)部工作原理嗎ChannelSftp.get( "file_name")?如果頻繁使用此函數(shù),使用此函數(shù)從文件列表(10 kb 的 600 個(gè)文件)中獲取單個(gè)文件會(huì)導(dǎo)致 Tomcat8 內(nèi)存泄漏問題嗎?這是用于從遠(yuǎn)程服務(wù)器上傳和獲取文件的代碼。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());}
1 回答

慕碼人2483693
TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超9個(gè)贊
您必須處理流。
FileInputStream
上傳代碼中的 和下載代碼中InputStream
返回的。ChannelSftp.get
添加回答
舉報(bào)
0/150
提交
取消