/**
* 将数据按照指定格式写入
* @param data
* @param fileName
* @return
*/
public static InputStream write(String data){
InputStream input = null;
try {
input = new ByteArrayInputStream(("utf-8"));
} catch (UnsupportedEncodingException e) {
("文件写入异常:",e);
}
return input;
}
/**
* Description: 向FTP服务器上传文件
* @param host FTP服务器hostname
* @param username FTP登录账号
* @param password FTP登录密码
* @param basePath FTP服务器基础目录
* @param filePath FTP服务器文件存放路径。例如分日期存放:/2015/01/01。文件的路径为basePath+filePath
* @param filename 上传到FTP服务器上的文件名
* @param input 输入流
* @return 成功返回true,否则返回false
*/
public static boolean uploadFile(String host, String username, String password, String basePath,
String filePath, String filename, InputStream input) {
boolean result = false;
FTPClient ftp = new FTPClient();
try {
int reply;
(host);// 连接FTP服务器
// 如果采用默认端口,可以使用(host)的方式直接连接FTP服务器
(username, password);// 登录
reply = ();
if (!(reply)) {
();
return result;
}
("文件目录是否已存在:"+(filePath));
//切换到上传目录
if (!(filePath)) {
//如果目录不存在创建目录
String[] dirs = ("/");
String tempPath = basePath;
for (String dir : dirs) {
if (null == dir || "".equals(dir)) continue;
tempPath += "/" + dir;
if (!(tempPath)) {
if (!(tempPath)) {
return result;
} else {
(tempPath);
}
}
}
}
//设置上传文件的类型为二进制类型
(FTP.BINARY_FILE_TYPE);
if(input == null){
return false;
}
//上传文件
if (!(filename, input)) {
return result;
}
();
();
result = true;
} catch (IOException e) {
("异常:", e);
} finally {
if (()) {
try {
();
} catch (IOException ioe) {
("关闭ftp连接异常:", ioe);
}
}
}
return result;
}