直接貼代碼了public class SftpUtil {
private static Session session = null;
private static Channel channel = null;
private static final Logger log = Logger.getLogger(SftpUtil.class.getName());
/**
* 獲取ChannelSftp實(shí)例對(duì)象
*
* @param sftpDetails
* @param timeout
* @return ChannelSftp
* @throws JSchException
*/
private static ChannelSftp getChannel(int timeout) throws JSchException {
ResourceBundle bundle = ResourceBundle.getBundle("ZJW");
String ftpHost = bundle.getString(SftpConstants.SFTP_REQ_HOST);
String port = bundle.getString(SftpConstants.SFTP_REQ_PORT);
String ftpUsername = bundle.getString(SftpConstants.SFTP_REQ_USERNAME);
String ftpPassword = bundle.getString(SftpConstants.SFTP_REQ_PASSWORD);
int ftpPort = SftpConstants.SFTP_DEFAULT_PORT;
if (port != null && !port.equals("")) {
ftpPort = Integer.valueOf(port);
}
JSch jsch = new JSch(); // 創(chuàng)建JSch對(duì)象
session = jsch.getSession(ftpUsername, ftpHost, ftpPort); // 根據(jù)用戶名,主機(jī)ip,端口獲取一個(gè)Session對(duì)象
System.out.println(ftpUsername+"###"+ftpHost+"###"+ftpPort+"###"+ftpPassword);
log.info("Session created.");
if (ftpPassword != null) {
session.setPassword(ftpPassword); // 設(shè)置密碼
}
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
//config.put("PreferredAuthentications", "password");
session.setConfig(config); // 為Session對(duì)象設(shè)置properties
session.setTimeout(timeout); // 設(shè)置timeout時(shí)間
log.info("Session建立連接。。。");
session.connect(); // 通過(guò)Session建立鏈接
log.info("Session connected.");
log.info("Opening Channel.");
channel = session.openChannel("sftp"); // 打開(kāi)SFTP通道
channel.connect(); // 建立SFTP通道的連接
log.info("Connected successfully to ftpHost = " + ftpHost + ",as ftpUsername = " + ftpUsername
+ ", returning: " + channel);
return (ChannelSftp) channel;
}
/**
* zip文件上傳
* @param dst
* @param src
* @param timeout
* @description dst為目標(biāo)文件名(e.g:xxx/sftp/xxx.zip")
* src為本地文件名(e.g:c:\\xxx\\xxx.zip)
* timeout為連接超時(shí)時(shí)間,單位ms
*/
public static void uploadZip(String dst,String src,int timeout){
try {
ChannelSftp channel = getChannel(timeout); //獲取執(zhí)行對(duì)象
if(channel==null){
log.info("程序錯(cuò)誤,ChannelSftp獲取為null!");
}
log.info("zip上傳開(kāi)始...");
channel.put(src, dst, ChannelSftp.OVERWRITE); //執(zhí)行sftp上傳,模式為overwrite
log.info("zip上傳成功...");
} catch (Exception e) {
log.info("系統(tǒng)異常,文件上傳失??!");
e.printStackTrace();
}finally {
try {
closeChannel();
} catch (Exception e) {
log.info("系統(tǒng)異常,資源關(guān)閉失敗!");
e.printStackTrace();
}
}
}
}
在本機(jī)裝了一個(gè)msftpsrvr64模擬sftp,使用FlashFXP連接上傳都很正常用自己寫的測(cè)試就不行了
@Test
public void demo03(){
String dst = "\0000.zip";
String src = "D:\\0000.zip";
SftpUtil.uploadZip(dst, src, 6000);
}
錯(cuò)誤碼:
java.lang.StringIndexOutOfBoundsException: String index out of range: 16777219
at java.lang.String.checkBounds(Unknown Source)
at java.lang.String.<init>(Unknown Source)
at com.jcraft.jsch.Util.byte2str(Util.java:417)
at com.jcraft.jsch.Util.byte2str(Util.java:428)
at com.jcraft.jsch.DHG14.next(DHG14.java:203)
at com.jcraft.jsch.Session.connect(Session.java:326)
at com.jcraft.jsch.Session.connect(Session.java:183)
at com.waterelephant.zjw.utils.SftpUtil.getChannel(SftpUtil.java:67)
at com.waterelephant.zjw.utils.SftpUtil.uploadZip(SftpUtil.java:102)
at com.waterelephant.test.PropTest.demo03(PropTest.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
調(diào)試過(guò) 是在session.connect()那里出的異常 debug進(jìn)去看了一下 是連上了的 但是中間出了異常 沒(méi)看太懂...
關(guān)于使用jsch 上傳文件到sftp的問(wèn)題 java.lang.StringIndexOutOfBoundsException
拉風(fēng)的咖菲貓
2019-03-01 10:35:01