👀 ftp에 올린 실제파일 , database 삭제
- cdn_repo.java (interface)
public interface cdn_repo extends JpaRepository<cdn_dto, Integer>{
	@Query("select now()")
	String mysql_times();
	
        //insert때 쓴거
	List<cdn_dto> findByImgnameLikeOrderByIdxDesc(String imgname);
}
- Controller
@Autowired
cdn_repo cdn_repo;
@GetMapping("/cdn_del/{filenm}")
public String cdn_file_delete(@PathVariable String filenm) {
    FTPClient ftp = new FTPClient();
    ftp.setControlEncoding("utf-8");
    FTPClientConfig fc = new FTPClientConfig();
    try {
        String host = "172.30.1.16"; //CDN 서버 주소
        String user = "admin"; //FTP 접속 ID
        String pass = "123123"; //FTP 접속 password
        int port = 20021; //FTP 접속 port
        ftp.configure(fc); 
        ftp.connect(host,port); //FTP 접속 체크
        if(ftp.login(user, pass)) { //FTP 로그인
            //실제 file먼저 지우고 db 삭제
            String ftpurl = "/home/admin/CDN/images/cdn_upload_seeun/"; //FTP 절대 경로
            List<cdn_dto> origin = cdn_repo.findByImgnameLikeOrderByIdxDesc(filenm+"%");
            //deleteFile : FTP 에 있는 파일을 삭제할 때 사용하는 메소드
            boolean del_ok = ftp.deleteFile(ftpurl+origin.get(0).getImgname());
            if(del_ok == true) {
                //delete : 일반속성 / deleteById : primary key
                cdn_repo.deleteById(origin.get(0).getIdx()); //database 삭제
                System.out.println("file 삭제 완료");
            }else {
                System.out.println("file을 찾을 수 없습니다.");
            }
        }else {
            System.out.println("FTP 아이디 및 패스워드 오류발생");
        }
    }catch(Exception e) {
        System.out.println("FTP 접속 오류!");
    }
    return null;
}
http://localhost:8081/cdn_del/11 접속시 삭제됨
=> like / finenm + "%" 로 작성해서 해당 파일명만 적어도 삭제됨니당
deleteFile :
FTP 에 있는 파일을 삭제할 때 사용하는 메소드
db 삭제시 주의
delete : 일반속성 / deleteById : primary key
연결해제 필수
ftp.disconnect();
====> 우왕 둘다 삭제됨
'CLASS > SPRINGBOOT' 카테고리의 다른 글
| #1-1 / 🌸 Gradle Project start + Oracle (0) | 2024.08.30 | 
|---|---|
| #11-1 / websocket - socket 통신 (0) | 2024.08.29 | 
| #10-2 / Ajax로 이미지 전송 및 JPA (0) | 2024.08.28 | 
| #10-1 / CDN 이미지 (0) | 2024.08.28 | 
| #9-3 / CDN FTP 파일 업로드 사용법 (0) | 2024.08.27 |