/**
*下载文件
*
* @param fileName
* @param date
* @param plainFilePath 明文文件路径路径
* @param filepath
* @return
* @throws Exception
*/
public static String encodeAESFileDownloadByFtp(String plainFilePath, String fileName, String date,String filepath) throws Exception {
FileInputStream fis = null
ByteArrayOutputStream bos = null
FileOutputStream fos = null
FTPClient ftpClient = new FTPClient()
String bl = "false"
try {
//Log.info("下载并解密文件开始")
Log.info("连接远程下载服务器"+CMBCUtil.CMBCHOSTNAME+":"+2021)
ftpClient.connect(CMBCUtil.CMBCHOSTNAME, 2021)
ftpClient.login(CMBCUtil.CMBCLOGINNAME, CMBCUtil.CMBCLOGINPASSWORD)
FTPFile[] fs
fs = ftpClient.listFiles()
for (FTPFile ff : fs) {
if (ff.getName().equals(filepath)) {
ftpClient.changeWorkingDirectory("/"+filepath+"/")
}
}
fs = ftpClient.listFiles()
for (FTPFile ff : fs) {
if (ff.getName().equals(date)) {
bl = "true"
ftpClient.changeWorkingDirectory("/"+filepath+"/" + date)
}
}
Log.info("检查文件路径是否存在:/"+filepath+"/"+date + " "+ bl)
if("false".equals(bl)){
ViewUtil.dataSEErrorPerformedCommon( "查询文件路径不存在:"+"/"+filepath+"/" + date)
return bl
}
bl = "false"
fs = ftpClient.listFiles()
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
bl = "true"
Log.info("下载并解密文件开始。")
fos = new FileOutputStream(plainFilePath)
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()
byte[] bytes = decodeAES(key, bos.toByteArray())
fos = new FileOutputStream(plainFilePath)
fos.write(bytes)
Log.info("下载并解密文件结束:"+plainFilePath)
}
}
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)
}
}
}
}
文件字节输出流关联指定文件路径的文件,数据通过文件字节输出流以字节为单位输出并保存到文件中。文件字节输出流继承自OutputStream类,并实现OutputStream类的各种方法。
文件字节输出流的构造方法语法如下:
语法1:以File对象为参数创建FileOutputStream实例
new FileOutputStream(File file)1语法2:以文件对象的绝对路径为参数创建FIleOutputStream实例
new FileOutputStream(String filepath)欢迎分享,转载请注明来源:内存溢出
评论列表(0条)