티스토리 뷰

Java

FTP를 이용한 다운로드

LichKing 2014. 9. 30. 13:50

/* 자세히 알아보진 않았는데 FTPClient 객체를 제공하는 jar가있고 FtpClient 객체를 제공하는 jar가 있는것 같다. 서로 제공하는 내용이 약간 다르니 참고하는 분들은 FTP인지 Ftp인지 잘 확인하기바람.*/


//일반파일 경로

StringBuffer ftpFilePath = new StringBuffer();    //다운로드받을 파일이 있는경로
ftpFilePath.append(ConfigProperties.getProperty("download.path") + "/hub");
ftpFilePath.append("/FILE/TEXT/admbrd/" + request.getParameter("portal_cd"));

FtpClient client = new FtpClient();    //FtpClient 객체 생성client.openServer(ConfigProperties.getProperty("webtob.ftp.url"), 21); //호스트IP, 포트번호
client.login(ID, PassWord);    //로그인 가능한 ID, PW 설정


BufferedOutputStream outs = null;
TelnetInputStream fileIs = null;

try{
    client.cd(ftpFilePath.toString());    //해당경로로 이동
    client.binary();

     fileIs = client.get(파일명);    //이동한 경로에 존재하는 파일명

     outs = new BufferedOutputStream(response.getOutputStream());
     byte bytes[] = new byte[1024];
     int c;

     request.setCharacterEncoding("UTF-8");
     response.setContentType("application/octet-stream");
     response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(파일명, "utf-8") + ";");    //파일명 인코딩 설정

      while ((c = fileIs.read(bytes)) != -1){
                outs.write(bytes, 0, c);
      }catch(Exception e){


}finally{
            if(fileIs != null){
                fileIs.close();
            }
            if(imgIs != null){
                imgIs.close();
            }
            if(outs != null){
                outs.close();
            }
            client.closeServer();
        }

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함