Delete File from SFTP Example
This program demonstrates deleting a file from an SFTP server.
Problem Statement
Write a Java program that deletes a file from an SFTP server using JSch library.
Source Code
| 1 | import com.jcraft.jsch.*; |
| 2 | public class SFTPDeleteExample { |
| 3 | public static void main(String[] args) { |
| 4 | System.out.println("Eduinq SFTP Delete Example"); |
| 5 | JSch jsch=new JSch(); |
| 6 | try { |
| 7 | Session session=jsch.getSession("user","sftp.eduinq.com",22); |
| 8 | session.setPassword("password"); |
| 9 | session.setConfig("StrictHostKeyChecking","no"); |
| 10 | session.connect(); |
| 11 | Channel channel=session.openChannel("sftp"); |
| 12 | channel.connect(); |
| 13 | ChannelSftp sftp=(ChannelSftp)channel; |
| 14 | sftp.rm("/server/sample.txt"); |
| 15 | System.out.println("Eduinq File Deleted Successfully"); |
| 16 | sftp.disconnect(); |
| 17 | session.disconnect(); |
| 18 | } catch(Exception e) { |
| 19 | System.out.println("Eduinq Exception: "+e.getMessage()); |
| 20 | } |
| 21 | } |
| 22 | } |
Program Output
Eduinq SFTP Delete Example Eduinq File Deleted Successfully
Explanation
Uses rm() to remove file from server. - Requires valid path and permissions. - Exception handling ensures safe operations. - In enterprise systems, deletion manages storage securely. Conclusion: Shows deleting files from SFTP server.