본문 바로가기
개발(라이브러리,프레임워크)/Spring boot

SFTP에 파일 올리기

by zieunee 2022. 10. 21.
반응형

내부망 서버SFTP에 이미지 파일 등을 매일올려야하는데 

접속하기 너무 귀찮아서 소스 만들었음 

여러개 multi로 올리는 케이스, 단일파일 올리는 케이스 2개 있음 


XXController 에 아래 내용 추가  

	/**
	 * 단일 파일 업로드
	 * @param request
	 * @param file
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/uploadFile")
	@ResponseBody
	public String uploadFile(HttpServletRequest request, @RequestPart MultipartFile file
			,@RequestPart HashMap<String, String> params) throws Exception {
		GboxFileDomain fileDomain = new GboxFileDomain();
		String result = null; 
		logger.info("#### LoadFile 호출 ####");
		fileDomain.setName(params.get("name"));
		fileDomain.setFilePath(params.get("filePath"));
		fileDomain.setDirName(params.get("dirName"));
		fileDomain.setType(params.get("type"));
		fileDomain.setFile(file);
		gboxCommonService.fileUpload(fileDomain);

		return "uploadFile";
	}
	 // 여러 개의 파일 업로드 
	@RequestMapping(value = "/uploadMultiFile")
	@ResponseBody
	public String multiFileUpload(
			@RequestPart MultipartFile[] file,
			@RequestPart HashMap<String, String> params){
		GboxFileDomain fileDomain = new GboxFileDomain();
    	// 결과값 리턴 
//	    List<GboxFileDomain> result = null;
		fileDomain.setName(params.get("name"));
		fileDomain.setFilePath(params.get("filePath"));
		fileDomain.setDirName(params.get("dirName"));
		fileDomain.setType(params.get("type"));
		fileDomain.setMultiFile(file);
		// 파일 있을 경우만 
		if (file != null) {
			try {
				gboxCommonService.fileUpload(fileDomain);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return "loadFile";
	}
	
	@RequestMapping(value = "/downloadFile")
	@ResponseBody
	public String downloadFile(@RequestPart HashMap<String, String> params){
		GboxFileDomain fileDomain = new GboxFileDomain();
		fileDomain.setName(params.get("name"));
		fileDomain.setFilePath(params.get("filePath"));
		fileDomain.setDirName(params.get("dirName"));
		fileDomain.setType(params.get("type"));
		try {
			gboxCommonService.fileDownload(fileDomain);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "downloadFile";
	}

xxService 에 아래 내용 추가

/*
	 * 특정 서버에 파일 업로드
	 */
	public void fileUpload(GboxFileDomain fileDomain) throws Exception {
		FTPUtils ftpUtils = new FTPUtils();
		try {
			// 단일 파일
			if (fileDomain.getFile() != null) {
				MultipartFile mfile = fileDomain.getFile();
				// 전송
				if (fileDomain != null) {
					ftpUtils.init(wurl, username, pwd);
					ftpUtils.upload(mfile, fileDomain.getFilePath(), fileDomain.getDirName());
					ftpUtils.disconnect();
				}
				// 파일 리스트
			} else if (fileDomain.getMultiFile() != null) {
				MultipartFile[] mfiles = fileDomain.getMultiFile();
				// 전송
				if (fileDomain != null) {
					ftpUtils.init(wurl, username, pwd);
					ftpUtils.uploadMultiFile(mfiles, fileDomain.getFilePath(), fileDomain.getDirName());
					ftpUtils.disconnect();
				}
			}
		} catch (Exception e) {
			logger.error("==== [PersonalHstSenderJob] personalHst file transfer error", e);
			// throw new BizException(0, "personalHst file transfer error :: " + e);
		}
	}

	public void fileDownload(GboxFileDomain fileDomain) throws Exception {
		FTPUtils ftpUtils = new FTPUtils();
		String url = wurl;
		String password = pwd;
		if(fileDomain.getType() == "was") {
			 url = was;
			 password = waspwd;
		}else if(fileDomain.getType() == "db") {
			 url = db;
//			 password = waspwd;
		}
		logger.info(url);
		try {
			// 단일 파일
			if (fileDomain.getName() != null) {
				ftpUtils.init(url, username, password);
				ftpUtils.download(fileDomain.getFilePath(), fileDomain.getName(), fileDomain.getDirName());
				ftpUtils.disconnect();
			}
		} catch (Exception e) {
			logger.error("==== [PersonalHstSenderJob] personalHst file transfer error", e);
			// throw new BizException(0, "personalHst file transfer error :: " + e);
		}
	}

 

XXDomain

import org.springframework.web.multipart.MultipartFile;

public class GboxFileDomain {
	private String name;
	private String filePath;
	private String dirName;
	private MultipartFile file;
	private MultipartFile[] multiFile;
	private String type;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public String getDirName() {
		return dirName;
	}

	public void setDirName(String dirName) {
		this.dirName = dirName;
	}

	public MultipartFile getFile() {
		return file;
	}

	public void setFile(MultipartFile file) {
		this.file = file;
	}

	public MultipartFile[] getMultiFile() {
		return multiFile;
	}

	public void setMultiFile(MultipartFile[] multiFile) {
		this.multiFile = multiFile;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	

}
반응형