English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Dopo aver utilizzato recentemente l'SFTP per caricare file e aver cercato alcune informazioni, ho fatto una breve sintesi per facilitare le future ricerche. Ecco il codice specifico:
/* @return (ChannelSftp) channel; * Carica il file sul server /* Disconnetti la connessione SFTP * @param filePath * percorso del file * @param channelSftp * oggetto channelSftp * @return */ public static boolean uploadFile(String filePath, ChannelSftp channelSftp) { OutputStream outstream = null; InputStream instream = null; boolean successFlag = false; try { File isfile = new File(filePath); if (isfile.isFile()) { outstream = channelSftp.put(isfile.getName()); File file = new File(filePath); if (file.exists()) { instream = new FileInputStream(file); byte b[] = new byte[1024]; int n; while ((n = instream.read(b)) != -1) { outstream.write(b, 0, n); } outstream.flush(); } successFlag = true; } } catch (Exception e) { e.printStackTrace(); } try { if (instream != null) { instream.close(); } if (outstream != null) { outstream.close(); } } catch (IOException e) { e.printStackTrace(); } } return successFlag; } private static Session initJschSession() throws JSchException { int ftpPort = 0; String ftpHost = ""; String port = "00"; // numero di porta sftp String ftpUserName = ""; // nome utente String ftpPassword = ""; // password del collegamento String privateKey = ""; // String passphrase = ""; if (port != null && !port.equals("")) { ftpPort = Integer.valueOf(port); } JSch jsch = new JSch(); // Crea l'oggetto JSch if (StringUtils.isNotBlank(privateKey) && StringUtils.isNotBlank(passphrase)) { jsch.addIdentity(privateKey, passphrase); } if (StringUtils.isNotBlank(privateKey) && StringUtils.isBlank(passphrase)) { jsch.addIdentity(privateKey); } jsch.getSession(ftpUserName, ftpHost, ftpPort); Session session = jsch.getSession(ftpUserName, ftpHost, ftpPort); // Ottieni un oggetto Session in base al nome utente, IP del host e porta if (StringUtils.isNotBlank(ftpPassword)) { session.setPassword(ftpPassword); // Imposta la password } return session; } /* @return (ChannelSftp) channel; * Ottieni la connessione ChannelSftp /* Disconnetti la connessione SFTP * @param timeout * Tempo di scadenza * @return Restituisce l'oggetto ChannelSftp * @throws JSchException */ public static ChannelSftp getChannelSftp(Session session, int timeout) throws JSchException { Channel channel = null; Properties config = new Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); // Imposta le proprietà dell'oggetto Session session.setTimeout(timeout); // Imposta il tempo di timeout session.connect(); // Estabilisci la connessione tramite Session channel = session.openChannel("sftp"); // Apri il canale SFTP channel.connect(); // Stabilisci la connessione del canale SFTP /* channel.connect(); // Stabilisci la connessione del canale SFTP } /* @return (ChannelSftp) channel; /* */ /* Disconnetti la connessione SFTP /* * /* @param session /* @param channel /* Canale */ public static void closeConnection(Channel channel, Session session) { try { if (session != null) { session.disconnect(); // Chiudi la connessione sessione } if (channel != null) { channel.disconnect(); // Disconnetti la connessione } } catch (Exception e) { e.printStackTrace(); } }
I nomi utente e le password qui sono impostati autonomamente, il metodo qui descritto è stato semplicemente encapsulato per facilitare l'uso.
L'uso semplice di Java per caricare file su un server tramite SFTP che ho introdotto all'editori spero sia utile a tutti. Se avete qualsiasi domanda, lasciate un commento e l'editori risponderà tempestivamente. In questo senso, ringrazio anche tutti i sostenitori del sito tutorial di urla!
Dichiarazione: il contenuto di questo articolo è stato tratto da Internet, il copyright è di proprietà del rispettivo autore, il contenuto è stato contribuito e caricato autonomamente dagli utenti di Internet, questo sito non detiene i diritti di proprietà, non è stato editato manualmente e non assume alcuna responsabilità legale correlata. Se trovi contenuti sospetti di violazione del copyright, ti preghiamo di inviare una e-mail a notice#oldtoolbag.com (al momento dell'invio dell'e-mail, sostituisci # con @) per segnalare il problema e fornire prove pertinenti. Una volta verificata, questo sito rimuoverà immediatamente il contenuto sospetto di violazione del copyright.