/** * Send a file to server path via SFTP * @param src * @param path * @throws SftpException */ public void put(final File src, String path) throws SftpException { if (!path.endsWith("/")) path = path.concat("/"); if (path.startsWith("/")) path = path.substring(1); ChannelSftp sftp = (ChannelSftp) channel; SftpProgressMonitor progress = new SftpProgressMonitor() { @Override public void init(int arg0, String arg1, String arg2, long arg3) { System.out.println("File transfer begin.."); } @Override public void end() { Out.print(LOG_LEVEL.INFO, "Upload of file "+ src.getName() +" succeeded."); ready = true; } @Override public boolean count(long i) { return false; } }; sftp.put(src.getAbsolutePath(), initRemDir+path+src.getName(), progress); }
public void uploadFile(InputStream toBePrinted, String directory, String filename, SftpProgressMonitor progressMonitor) throws SftpException, JSchException, IOException { if(session== null){ throw new JSchException(context.getString(R.string.misc_connection_not_set_up)); } Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp channelSftp = (ChannelSftp)channel; try{ channelSftp.mkdir(directory); } catch (SftpException e){ //If cannot make directory, means directory already created } channelSftp.cd(directory); channelSftp.put(toBePrinted, filename, progressMonitor, ChannelSftp.OVERWRITE); channelSftp.disconnect(); }
@Override public void init( int op, String src, String dest, long max ) { String operation = (op == SftpProgressMonitor.PUT) ? "UPLOAD" : "DOWNLOAD"; log.debug("Begin " + operation + " from " + this.source + " to " + this.destination); }
/** * Get the progress monitor. * @return the progress monitor. */ protected SftpProgressMonitor getProgressMonitor() { if (monitor == null) { monitor = new ProgressMonitor(); } return monitor; }
private void getFile(final ChannelSftp channel, final ChannelSftp.LsEntry le, File localFile) throws IOException, SftpException { final String remoteFile = le.getFilename(); if (!localFile.exists()) { final String path = localFile.getAbsolutePath(); final int i = path.lastIndexOf(File.pathSeparator); if (i != -1) { if (path.length() > File.pathSeparator.length()) { new File(path.substring(0, i)).mkdirs(); } } } if (localFile.isDirectory()) { localFile = new File(localFile, remoteFile); } final long startTime = System.currentTimeMillis(); final long totalLength = le.getAttrs().getSize(); SftpProgressMonitor monitor = null; final boolean trackProgress = getVerbose() && totalLength > HUNDRED_KILOBYTES; if (trackProgress) { monitor = getProgressMonitor(); } try { log("Receiving: " + remoteFile + " : " + le.getAttrs().getSize()); channel.get(remoteFile, localFile.getAbsolutePath(), monitor); } finally { final long endTime = System.currentTimeMillis(); logStats(startTime, endTime, (int) totalLength); } if (getPreserveLastModified()) { FileUtils.getFileUtils().setFileLastModified(localFile, ((long) le.getAttrs() .getMTime()) * 1000); } }
public void init(int op, String src, String dest, long max) { this.max = max; monitor = new ProgressMonitor(null, ((op == SftpProgressMonitor.PUT) ? "put" : "get") + ": " + src, "", 0, (int) max); count = 0; percent = -1; monitor.setProgress((int) this.count); monitor.setMillisToDecideToPopup(1000); }
@Override public void init(int op, String src, String dest, long max) { this.max = max; String info = ((op == SftpProgressMonitor.PUT) ? "PUT" : "GET") + ": " + src + " (" + Utils.inByteFormat(max) + ")"; SFTPPD.setTitle(info); count = 0; percent = -1; pb_.setMaximum((int) max); pb_.setValue((int) this.count); }
private void sendFileToRemote(final ChannelSftp channel, final File localFile, String remotePath) throws IOException, SftpException { final long filesize = localFile.length(); if (remotePath == null) { remotePath = localFile.getName(); } final long startTime = System.currentTimeMillis(); final long totalLength = filesize; // only track progress for files larger than 100kb in verbose mode final boolean trackProgress = getVerbose() && filesize > HUNDRED_KILOBYTES; SftpProgressMonitor monitor = null; if (trackProgress) { monitor = getProgressMonitor(); } try { if (this.getVerbose()) { log("Sending: " + localFile.getName() + " : " + filesize); } channel.put(localFile.getAbsolutePath(), remotePath, monitor); // set the fileMode on the transferred file. The "remotePath" can potentially be a directory // into which the file got transferred, so we can't/shouldn't go ahead and try to change that directory's // permissions. Instead we determine the path of the transferred file on remote. final String transferredFileRemotePath; if (channel.stat(remotePath).isDir()) { // Note: It's correct to use "/" as the file separator without worrying about what the remote // server's file separator is, since the SFTP spec expects "/" to be considered as file path // separator. See section 6.2 "File Names" of the spec, which states: // "This protocol represents file names as strings. File names are // assumed to use the slash ('/') character as a directory separator." transferredFileRemotePath = remotePath + "/" + localFile.getName(); } else { transferredFileRemotePath = remotePath; } if (this.getVerbose()) { log("Setting file mode '" + Integer.toOctalString(getFileMode()) + "' on remote path " + transferredFileRemotePath); } channel.chmod(getFileMode(), transferredFileRemotePath); if (getPreserveLastModified()) { // set the last modified time (seconds since epoch) on the transferred file final int lastModifiedTime = (int) (localFile.lastModified() / 1000L); if (this.getVerbose()) { log("Setting last modified time on remote path " + transferredFileRemotePath + " to " + lastModifiedTime); } channel.setMtime(transferredFileRemotePath, lastModifiedTime); } } finally { if (this.getVerbose()) { final long endTime = System.currentTimeMillis(); logStats(startTime, endTime, (int) totalLength); } } }
public SftpUploadTask(SftpProgressMonitor monitor, SftpConnection connection, IUploadNotifier callback) { sftpMonitor = monitor; conn = connection; handler = callback; }