/**
*下载并解压文件
*
* @param localFilePath
* @param fileName
* @param routeFilepath
* @return
* @throws Exception
*/
public static String fileDownloadByFtp(String localFilePath, String fileName,String routeFilepath) throws Exception {
FileInputStream fis = null
ByteArrayOutputStream bos = null
FileOutputStream fos = null
FTPClient ftpClient = new FTPClient()
String SFP = System.getProperty("file.separator")
String bl = "false"
try {
Log.info("下载并解密文件开始")
Log.info("连接远程下载服务器"+CCFCCBUtil.CCFCCBHOSTNAME+":"+22)
ftpClient.connect(CCFCCBUtil.CCFCCBHOSTNAME, 22)
ftpClient.login(CCFCCBUtil.CCFCCBLOGINNAME, CCFCCBUtil.CCFCCBLOGINPASSWORD)
FTPFile[] fs
ftpClient.makeDirectory(routeFilepath)
ftpClient.changeWorkingDirectory(routeFilepath)
bl = "false"
fs = ftpClient.listFiles()
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
bl = "true"
Log.info("下载文件开始。")
ftpClient.setBufferSize(1024)
// 设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE)
InputStream is = ftpClient.retrieveFileStream(fileName)
bos = new ByteArrayOutputStream(is.available())
byte[] buffer = new byte[1024]
int count = 0
while ((count = is.read(buffer)) != -1) {
bos.write(buffer, 0, count)
}
bos.flush()
fos = new FileOutputStream(localFilePath+SFP+fileName)
fos.write(bos.toByteArray())
Log.info("下载文件结束:"+localFilePath)
}
}
Log.info("检查文件是否存:"+fileName+" "+bl)
if("false".equals(bl)){
ViewUtil.dataSEErrorPerformedCommon("查询无结果,请稍后再查询。")
return bl
}
return bl
} catch (Exception e) {
throw e
} finally {
if (fis != null) {
try {
fis.close()
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e)
}
}
if (bos != null) {
try {
bos.close()
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e)
}
}
if (fos != null) {
try {
fos.close()
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e)
}
}
}
}
备注:只需要修改 ftpClient.connect方法中的用户名和密码即可进行远程服务器连接下载,具体的根据实际情况修改即可。
在项目根路径中建一个文件夹比如Files专门存放文件,可以在webRoot中建此文件夹,然后你在前台写一个<a>标签,比如这样<a href=http://192.168.14.17:8080/DownLoad/Files/a.txt>下载</a>
这样就可以了
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)