OutputStreamWriter pw = null//定义一个流
pw = new OutputStreamWriter(new FileOutputStream(“D:/test.txt”),"GBK")//确认流的输出文件和编码格式,此过程创建了“test.txt”实例
pw.write("我是要写入到记事本文件的数据")//将要写入文件的内容,可以多次write
pw.close()//关闭流
备注:文件流用完之后必须及时通过close方法关闭,否则会一直处于打开状态,直至程序停止,增加系统负担。
直接把大文件读取为IO流,之后进行上传下载即可,不用担心文件大,是可以分流下载上传的(setBufferSize(1024))。举例:
import hkrt.b2b.view.util.Log
import hkrt.b2b.view.util.ViewUtil
import java.io.ByteArrayOutputStream
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.InputStream
import org.apache.commons.net.ftp.FTPClient
import org.apache.commons.net.ftp.FTPFile
public class CCFCCBFTP {
/**
* 上传文件
*
* @param fileName
* @param plainFilePath 明文文件路径路径
* @param filepath
* @return
* @throws Exception
*/
public static String fileUploadByFtp(String plainFilePath, String fileName, String filepath) throws Exception {
FileInputStream fis = null
ByteArrayOutputStream bos = null
FTPClient ftpClient = new FTPClient()
String bl = "false"
try {
fis = new FileInputStream(plainFilePath)
bos = new ByteArrayOutputStream(fis.available())
byte[] buffer = new byte[1024]
int count = 0
while ((count = fis.read(buffer)) != -1) {
bos.write(buffer, 0, count)
}
bos.flush()
Log.info("加密上传文件开始")
Log.info("连接远程上传服务器"+CCFCCBUtil.CCFCCBHOSTNAME+":"+22)
ftpClient.connect(CCFCCBUtil.CCFCCBHOSTNAME, 22)
ftpClient.login(CCFCCBUtil.CCFCCBLOGINNAME, CCFCCBUtil.CCFCCBLOGINPASSWORD)
FTPFile[] fs
fs = ftpClient.listFiles()
for (FTPFile ff : fs) {
if (ff.getName().equals(filepath)) {
bl="true"
ftpClient.changeWorkingDirectory("/"+filepath+"")
}
}
Log.info("检查文件路径是否存在:/"+filepath)
if("false".equals(bl)){
ViewUtil.dataSEErrorPerformedCommon( "查询文件路径不存在:"+"/"+filepath)
return bl
}
ftpClient.setBufferSize(1024)
ftpClient.setControlEncoding("GBK")
// 设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE)
ftpClient.storeFile(fileName, fis)
Log.info("上传文件成功:"+fileName+"。文件保存路径:"+"/"+filepath+"/")
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)
}
}
}
}
/**
*下载文件
*
* @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)
// ftpClient.connect(CMBCUtil.CMBCHOSTNAME, 2021)
// ftpClient.login(CMBCUtil.CMBCLOGINNAME, CMBCUtil.CMBCLOGINPASSWORD)
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)
}
}
}
}}
备注:以上方法就实现了流的二进制上传下载转换,只需要将服务器连接部分调整为本地的实际ftp服务用户名和密码即可。
public static void downloadFileFtp(KmConfig kmConfig,String fileName, String clientFileName, OutputStream outputStream){try {
String ftpHost = kmConfig.getFtpHost()
int port = kmConfig.getFtpPort()
String userName = kmConfig.getFtpUser()
String passWord = kmConfig.getFtpPassword()
String path = kmConfig.getFtpPath()
FtpClient ftpClient = new FtpClient(ftpHost, port)// ftpHost为FTP服务器的IP地址,port为FTP服务器的登陆端口,ftpHost为String型,port为int型。
ftpClient.login(userName, passWord)// userName、passWord分别为FTP服务器的登陆用户名和密码
ftpClient.binary()
ftpClient.cd(path)// path为FTP服务器上保存上传文件的路径。
try {
TelnetInputStream in = ftpClient.get(fileName)
byte[] bytes = new byte[1024]
int cnt=0
while ((cnt=in.read(bytes,0,bytes.length)) != -1) {
outputStream.write(bytes, 0, cnt)
}
//##############################################
//这里文件就已经下载完了,自己理解一下
//#############################################
outputStream.close()
in.close()
} catch (Exception e) {
ftpClient.closeServer()
e.printStackTrace()
}
ftpClient.closeServer()
} catch (Exception e) {
System.out.println("下载文件失败!请检查系统FTP设置,并确认FTP服务启动")
}
}