使用edtftpj-***.jar上传下载中文问题的解决方案和注意点

FileTransferClient ftpClient = null;
try {
ftpClient = new FileTransferClient();
// set remote host
ftpClient.setRemoteHost(FTPAddress);
ftpClient.setRemotePort(portInt);
ftpClient.setUserName(FTPAccount);
ftpClient.setPassword(FTPPwd);
//注意:下面红色的部分必须写在connect之前,因为做connect的时候会到ftpClient对象的设置参数进行赋值。
ftpClient.getAdvancedSettings().setTransferBufferSize(2048);
ftpClient.getAdvancedSettings().setTransferNotifyInterval(5000);
ftpClient.getAdvancedSettings().setControlEncoding("GBK");
ftpClient.connect();
ftpClient.changeDirectory("/");
ftpClient.setDetectContentType(true);
ftpClient.setContentType(FTPTransferType.BINARY);
String[] names = ftpClient.directoryNameList();
for (String oName : names) {
log.debug("file is:" + oName);
}
for (String kngAttachName : kngAttachs) {
int i = kngAttachName.lastIndexOf(".");
// 附件列表路径中不存在"."
if (i != -1) {
// 获取附件文件类型
String fileType = kngAttachName.substring(i + 1, kngAttachName.length());
String localFileName = this.getCSPId() + "." + fileType;
File file = new File(fileServerPath);
if (!file.exists()) {
file.mkdirs();
}
String path = fileServerPath + "/" + localFileName;
File file1 = new File(path);
file1.createNewFile();
FileOutputStream fos = new FileOutputStream(file1);
if (log.isDebugEnabled()) {
log.debug("Local filepath is:" + path);
log.debug("Remote host is:" + ftpClient.getUserName() +
                   "@" + ftpClient.getRemoteHost() + ":" +
ftpClient.getRemotePort() + "@" + ftpClient.getRemoteDirectory());
log.debug("kngAttachName is:" + kngAttachName);
}
String kngAttachName_ecode = new String(kngAttachName.getBytes(), "UTF-8");
if (ftpClient.exists(kngAttachName_ecode)) {
final int BUFFER_SIZE = 2048;
final byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = 0;
FileTransferInputStream inputStream = null;
try {
inputStream = ftpClient.downloadStream(kngAttachName_ecode);
while ((bytesRead = inputStream.read(buffer, 0, BUFFER_SIZE)) != -1) {
fos.write(buffer, 0, bytesRead);
}
fos.flush();
}
catch (IOException e) {
if (log.isDebugEnabled()) {
log.error("知识附件下载到文件" + kngAttachName + "服务器ftp发生异常" +
                      e.getMessage(), e);
}
String message = new StringBuilder("the kngAffix ").
                    append(kngAttachName).append(" transFailed!").toString();
SoapUtil.recordLog("transFail", message);
}
finally {
if (inputStream != null) {
inputStream.close();
}
if (fos != null) {
fos.close();// MUST be closed to complete the transfer
IOUtils.closeQuietly(fos);
}
}
}
else {
log.error("经分知识(" + kngId + ")的附件(" + kngAttachName + ")不存在!");
}
// 知识附件对象
DataObject affixObject = new DataObjectSupport(
                "com.huawei.csp.customization.kbs.domain", "c_kbs_kngAdjunctInfoEdit");
affixObject.setString("adjId", this.getCSPId());
affixObject.setString("kngId", kngId);
affixObject.setInt("versionNum", 0);
affixObject.setInt("adjClass", 2);
int j = kngAttachName.lastIndexOf("/");
if (j != -1) {
affixObject.setString("adjName", kngAttachName.substring(j + 1,
                  kngAttachName.length()));
}
else {
affixObject.setString("adjName", kngAttachName);
}
affixObject.setString("adjPath", kngAttachPath + "/" + dateDirPath
                  + "/" + localFileName);
affixObject.setString("adjType", fileType);
affixObject.setInt("adjSize", 0);
String staffId = this.tableDataAccessService.
                getSystemParam("107", "107993").getValue();
affixObject.setString("creatorId", staffId);
affixObject.setString("modifierId", staffId);
affixObject.setString("hostedCcId", "001");
affixObject.setInt("orderId", 1);
this.collectKngInterface.addKngAffix(affixObject);
}
else {
String message = new StringBuilder("the kngAffix file type error!").toString();
SoapUtil.recordLog("transFail", message);
}
}
retInt = (0);
}
catch (Exception e) {
log.error(e.getMessage(), e);
}
finally {
if (ftpClient != null && ftpClient.isConnected()) {
try {
ftpClient.disconnect();
}
catch (IOException ioex) {
log.error(ioex.getMessage(), ioex);
}
catch (FTPException ftpEx) {
log.error(ftpEx.getMessage(), ftpEx);
}
}
}

本文转自:http://blog.sina.com.cn/s/blog_5b254b7701014qbd.html

上一篇:This Android SDK requires Android Developer Toolkit version 23.0.0 or above


下一篇:Android课程---远程服务器存储