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)
}
}
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
ruby ftp 봉인 실례 상세 설명ruby ftp 봉인 실례 상세 설명 최근에 루비로 Net::FTP의 도구 클래스를 봉인했습니다. # 지정된 형식의 파일 이름 목록 가져오기 # 예: source = "test/*.txt" # 반환: [source/...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.