Dev/Java

ftp upload

박공명 2010. 3. 24. 18:12


import sun.net.ftp.FtpClient;
import java.io.FileOutputStream;
import sun.net.TelnetOutputStream;
import java.io.IOException;
import sun.net.TelnetInputStream;
import java.io.FileInputStream;
import java.util.zip.*;
import java.util.*;
import java.io.*;

 public boolean doUpLoad(String mode,String s_server,String userid,String passwd) {
  FtpClient ftpClient = new FtpClient();
  TelnetOutputStream os =null ;
  FileInputStream is = null;
  try {
   
   System.out.println("doUpload in");
   ftpClient.openServer(s_server);
   System.out.println("server open");
   ftpClient.login(userid, passwd);
   System.out.println("server login");
   if (serverPath.length() != 0)
    ftpClient.cd(serverPath);
   //System.out.println("-4");
   if("asc".equals(mode)) 
    ftpClient.ascii();
   else
    ftpClient.binary();
   //System.out.println("-5");
   if("".equals(serverFile)) {
    serverFile =  localFile ;
   }
   System.out.println("write file path = " + serverPath+"/"+serverFile);
   os = ftpClient.put(serverPath +"/"+ serverFile );
   System.out.println("read file path = " + localPath+"/"+localFile);
   File file_in = new File(localPath+"/"+localFile);
   is = new FileInputStream(file_in);
   byte[] bytes = new byte[1024];
   int c;
   while ((c = is.read(bytes)) != -1) {
    os.write(bytes, 0, c);
   }
   System.out.println("doUpload complete");
   
   is.close();
   os.close();
   ftpClient.closeServer();
   System.out.println("doUpload close server");
   return true;
  }catch (IOException ie) {
   ie.printStackTrace();
   System.out.println("FTP Exeception -----" + ie.getMessage());
   return false;
  }catch(Exception e){
   e.printStackTrace();
   System.out.println("FTP Exeception -----" + e.getMessage());
   return false;
  }finally{
   try{
    is.close();
    os.close();
    ftpClient.closeServer();
   }catch(Exception e){} 
  }
 }


ㅇㅇㅇ

file.delete(); 만 추가하면 upload 가 아니구 move가 되겠지