controller层代码,文件FTP上传
private Service thisService = new Service();
File fansDataText = thisService.createTextForWxUser();
DBCollection dbLog = MogoDBUtil.getDBCollection("OperationLog", "ocm_log");
String hn = params.get("hn");
Integer pt = Integer.parseInt(params.get("pt"));
String un = params.get("un");
String pd = params.get("pd");
FTPClient ftp = new FTPClient();
InputStream local = null;
try {
ftp.connect(hn, pt);
ftp.login(un, pd);
String path="/";
boolean flag = ftp.changeWorkingDirectory(path);
if(!flag){
ftp.makeDirectory(path);
}
ftp.setControlEncoding("UTF-8");
ftp.enterLocalPassiveMode();
ftp.changeWorkingDirectory(path);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
Integer reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
dbLog.save(dbObject);
ftp.disconnect();
}
local = new FileInputStream(fansDataText);
boolean resuBool = ftp.storeFile(fansDataText.getName(), local);
thisService.delFile(fansDataText.getParent());
service层代码,包含txt文件写入生成,删除文件夹下所有内容
public File createTextForWxUser(){
final String filePath = this.getClass().getResource("/").getPath()+"file/sendDataToGuangDong";
String fileName = "WECHAT_USERINFO_0755_ID"+ MDateUtil.formatDate(new Date(),"yyyy-MM-dd")+".txt";
File fansDataText = new File(filePath + File.separator + fileName);
try {
if (!fansDataText.getParentFile().exists()) {
fansDataText.getParentFile().mkdirs();
}
if (!fansDataText.exists()) {
fansDataText.createNewFile();
}
} catch (IOException e) {
}
FileWriter fw = null;
List<String> wxUser = new LinkedList<>();
try {
fw = new FileWriter(fansDataText,true);
for (String s : wxUser) {
fw.write(s);
}
wxUser.clear();
} catch (IOException e) {
}finally {
try {
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return fansDataText;
}
public static void delFile(String path){
File file = new File(path);
if (file.isDirectory()){
File [] files = file.listFiles();
for (File subFile : files){
delFile(subFile.getPath());
}
}
file.delete();
}