Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.jcraft.jsch.JSch
- import com.jcraft.jsch.Session
- import com.jcraft.jsch.Channel
- import com.jcraft.jsch.ChannelSftp
- import com.axeda.drm.sdk.data.UploadedFile
- import com.jcraft.jsch.JSchException
- final String SFTP_ADDRESS = "bogusIP";
- final int SFTP_PORT = 22;
- final String userName1 = "axeda1";
- final String password1 = "aX3D@1";
- //final String userName2 = "axeda2"
- //final String password2 = "aX3D@2"
- final String modelName = parameters.model;
- final String deviceName = parameters.serial;
- UploadedFile[] files = compressedFile.getFiles();
- File file = files[0]?.extractFile();
- boolean result = sendFile(SFTP_ADDRESS, SFTP_PORT, userName1, password1, modelName, deviceName, file);
- if (!result){
- // store what is needed for retry
- }
- public boolean sendFile(String address,
- int port,
- String userName,
- String password,
- String modelName,
- String deviceName,
- File file) {
- JSch jsch = null;
- Session session = null;
- Channel channel = null;
- ChannelSftp sftpChannel = null;
- InputStream is = null;
- boolean error = false;
- try {
- jsch = new JSch()
- session = jsch.getSession(userName, address, port);
- session.setPassword(password)
- session.setConfig("StrictHostKeyChecking", "no");
- session.connect();
- channel = session.openChannel("sftp");
- channel.connect();
- sftpChannel = (ChannelSftp)channel;
- String fileName = file.getName();
- String shortFileName = fileName.substring(0,fileName.indexOf("["));
- String fileExt = fileName.substring(fileName.indexOf("]")+1);
- String fileNameToStore = shortFileName + fileExt;
- InputStream inputStream = new FileInputStream(file);
- try{
- sftpChannel.cd(modelName);
- }
- catch (Exception){
- logger.debug "Asset model directory does not exist. Creating now."
- sftpChannel.mkdir(modelName);
- sftpChannel.cd(modelName);
- }
- try{
- sftpChannel.cd(deviceName);
- }
- catch (Exception){
- logger.debug "Asset device directory does not exist. Creating now."
- sftpChannel.mkdir(deviceName);
- sftpChannel.cd(deviceName);
- }
- sftpChannel.put(inputStream, fileNameToStore);
- }
- catch (Exception e){
- error = true;
- logger.info "Could not send file to SFTP"
- logger.error e.getMessage()
- }
- finally{
- is?.close();
- sftpChannel?.exit();
- channel?.disconnect();
- session?.disconnect();
- }
- logger.info "Return error: " + error
- return !error;
- }
Add Comment
Please, Sign In to add comment