---
title: "FileUtils操作ftp以及本地上传、下载"
author: "weinan"
date: "2015-12-09T16:09:14Z"
tags: []
categories: ["移动开发"]
slug: "fileutils-ftp"
draft: false
lastmod: "2015-12-09T16:09:14Z"
id: 2262933
url: "https://weinan.iteye.com/blog/2262933"
---

\[quote] audit\_image\_src=/var/audit/upload audit\_image\_zipdesc=/var/audit/desc audit\_image\_zipdir=file:///D:/var/audit/desc audit\_ftp\_path=ftp://root:root1234@195.201.5.73:21/preaudit/RECV \[/quote] \[code="java"] /\** * ftp复制文件 * * @param srcPath * @param descPath * @return \*/ public boolean copy(String srcPath, String descPath) { FileSystemManager sfm = null; FileObject src = null; FileObject desc = null; boolean downloadResult=false; try { sfm = VFS.getManager(); src = sfm.resolveFile(srcPath); desc = sfm.resolveFile(descPath); if (src.exists()) { desc.copyFrom(src, Selectors.SELECT\_FILES); close(src, sfm); close(desc, sfm); downloadResult = true; }else{ downloadResult = false; } } catch (FileSystemException e) { downloadResult = false; } finally { close(desc, sfm); close(src, sfm); } return downloadResult; } public void close(FileObject file, FileSystemManager sfm) { if (null != file) { try { file.close(); sfm.closeFileSystem(file.getFileSystem()); } catch (FileSystemException e) { logger.error("文件\[" + file.toString() + "]关闭失败", e); } } } \[/code] \[code="java"] /\** * 根据批次号复制图片 * @param batchId \*/ @Override public void copyImgData(String batchId){ List preAttrList = preCmAttrExtService.getByBatchId(batchId); for (PreCmAttrExt preCmAttrExt : preAttrList) { String fileUrl= preCmAttrExt.getUrl(); File sourceFile = new File(imageSrc+fileUrl); File tempFile = new File(imageTmp+"/"+preCmAttrExt.getBatchId().trim()+"/"+batchId+"/"+preCmAttrExt.getsId().trim()+".jpg"); try { FileUtils.copyFile(sourceFile, tempFile); logger.info("图片文件复制成功，源文件地址："+sourceFile.getPath()+"目标文件地址："+tempFile.getPath()); } catch (IOException e) { logger.error("图片文件复制失败，源文件地址："+sourceFile.getPath()+"目标文件地址："+tempFile.getPath(), e); e.printStackTrace(); } } } \[/code]