FTP 서버 파일 업로드

4013 단어 ftp파일 업로드
@Value('${ftp.hostname}')
String hostname
@Value('${ftp.port}')
int port
@Value('${ftp.httpPort}')
int httpPort
@Value('${ftp.username}')
String username
@Value('${ftp.password}')
String password
@Value('${ftp.pathname}')
String pathname   // 

 
/**
 *  ftp 
 * */
def connect(){
    FTPClient ftp = new FTPClient();
    ftp.setControlEncoding("UTF-8");
    int reply;
    ftp.connect(hostname,port);
    // 
    ftp.login(username, password);
    reply = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();
    }
    // 
    ftp.changeWorkingDirectory(pathname)
    return ftp;
}
def uploadAvatar(Attachment attachment)
{
    def file = request.getFile("file")
    if (file.empty)
    {
        flash.message = message(code: ' ')
        respond attachment, model: [targetUri: params['targetUri']], view: 'create'
        return
    }
    def imageExtensionsList = ['jpg', 'png', 'jpeg', 'JPG', 'PNG', 'JPEG']
    def fileExtensionsList = ['pdf', 'doc', 'docx', 'xlsx', 'xls']
    def videoExtensionsList = ['mov', 'mp4', 'avi', 'rm', '3gp', 'mkv', 'wmv', 'ogg', 'rmvb']
    def audioExtensionsList = ['mp3', 'wav', 'amr', 'mid','m4a']
// 
def fileOrgName = file?.getOriginalFilename()
def fileType = fileOrgName?.split('\\.')[-1]
fileType = fileType?.toLowerCase()
if (!(fileType in imageExtensionsList) && !(fileType in fileExtensionsList) && !(fileType in videoExtensionsList) && !(fileType in audioExtensionsList))
{
    flash.message = message(code: ' ')
    respond attachment, model: [targetUri: params['targetUri']], view: 'create'
    return
}
// , ftp  ( , , ftp ,   )
def code = UUID.randomUUID().toString()
File fileImage = new File(servletContext.getRealPath("/"), "images/${code}.${fileType}")
file.transferTo(fileImage)
  if (fileImage)
            {
                //   
                attachment.fileName = fileOrgName
                attachment.fileUrl = "http://"+hostname+":"+httpPort+"/${code}.${fileType}"
                boolean result = false
                InputStream  fis = null
                FTPClient ftp = connect()
//                FTPClientConfig conf=new FTPClientConfig(FTPClientConfig.SYST_NT);
//                conf.setServerLanguageCode("zh");
//                ftp.configure(conf);
                try {
                    ftp.setFileType(FTP.BINARY_FILE_TYPE)  // 
                    fis = new FileInputStream(fileImage)
                    ftp.setControlEncoding("UTF-8")
                    ftp.enterLocalPassiveMode()
//                  ftp.enterLocalActiveMode()
                    ftp.setFileTransferMode(FTP.BINARY_FILE_TYPE)
                    result =  ftp.storeFile("${code}.${fileType}", fis);
                    ftp.logout();
                }catch(Exception e){
                    e.printStackTrace()
                }finally{

                    fis.close()
                    if (ftp.isConnected()) {
                        try {
                            ftp.disconnect();
                        } catch (IOException ioe) {
                            ioe.printStackTrace()
                        }
                    }
                    //  
                    if (fileImage.isFile() && fileImage.exists())
                    {
                        def flag = fileImage.delete()
                        System.out.print(flag)
                    }
                }
            }
}

 
}

좋은 웹페이지 즐겨찾기