while (calendar .getTime().before(TUpdEndTm)) Date date = calendar .getTime(); calendar.add(Calendar .DAY_OF_YEARamount: 1) TUpdTm = sdf.format(date) ; String dir = rootdir + "/" + TUpdTm; // 连接FTP ChannelSftp connect = FtpUtil.connect(username, password, host, port);List<String> rootFilelists = FtpUtil.listFiles(connect, rootdir); if (!rootFilelists.isEmpty() && rootFilelists.contains(TUpdTm)) { try {vatService.deletefile( delpath: imageFile + "/" + TUpdTm);vatService.deletefile( delpath: imageFile + "/" + TUpdTm + ",zip");} catch (Exception e1) { e1.printStackTrace(); // 创建存放单证的文件夹 File cImFile = new File(imageFile);if (!clmFile.exists() && !clmFile.isDirectory()) {cImFile.mkdir(): // 创建以公式日期命名存放单证的文件夹 File clmNoFile = new File( pathname: imageFile + TUpdTm); if (!clmNoFile.exists() && !clmNoFile.isDirectory()) { clmNoFile.mkdir(); OutputStream os = null; try { List<String> fileLists = FtpUtil.listFiles(connect, dir);List<String> gsNumList = new ArrayList<String>(0); if (!fileLists.isEmpty()) { /*List<String> gsList = ps.getGsList(fileLists); for (String gs : gsList) { for (String file : fileLists) {if(file.contains(gs)){gsNumList.add(file); gsNumList = fileLists; if (!gsNumList.isEmpty()) { for (String file : gsNumList) { File localFile = new File( pathname: imageFile + TUpdTm + "/" + file);if (!localFile.getParentFile().exists()) { localFile.getParentFile().mkdirs(); TocalFile.createNewFile() connect.cd(dir);// 进入所在路径 os = new FileOutputStream(localFile); connect.get(file, os); os.close(); } else { serviceResponse.setReturn_type(ActionConstant.RESPONSE_RESULT_TYPE_ERROR);serviceResponse.setReturnMessage("该日期不存在公示异议!"); // /打包Zip vatService.createZip( sourcePath: imageFile + TUpdTm,zipPath: imageFile + TUpdTm + ".zip"); } catch (Exception e) { e.printStackTrace() : } finally { if (null != ftpFileInput) { ftpFileInput.close() : os.close(); l*//删除文件夹下所有文件 serviceResponse.setReturnMessage("true");} else { serviceResponse .setReturn_type(ActionConstant.RESPONSE_RESULT_TYPE_ERROR);serviceResponse.setReturnMessage(该日期不存在公示异议!"); return serviceResponse; 在这段代码中,TUpdTm会在while的循环中变成三个不同的值,如何能让只要有其中一次的TUpdTm满足if条件就不进入else,相反,三次TUpdTm的值均不满足if条件才进入else,不能使用break跳出循环的方法
时间: 2024-02-21 14:58:34 浏览: 135
Date与Calendar详解
您可以使用一个boolean类型的变量来记录是否满足if条件,只要有一次满足就将这个变量置为true,当三次TUpdTm的值均不满足if条件时,判断这个变量是否为false,如果是则进入else。代码示例:
```java
boolean isTUpdTmValid = false; // 记录TUpdTm是否满足if条件
while (calendar.getTime().before(TUpdEndTm)) {
Date date = calendar.getTime();
calendar.add(Calendar.DAY_OF_YEAR, 1);
TUpdTm = sdf.format(date);
String dir = rootdir + "/" + TUpdTm;
// 连接FTP
ChannelSftp connect = FtpUtil.connect(username, password, host, port);
List<String> rootFilelists = FtpUtil.listFiles(connect, rootdir);
if (!rootFilelists.isEmpty() && rootFilelists.contains(TUpdTm)) {
try {
vatService.deletefile(delpath: imageFile + "/" + TUpdTm);
vatService.deletefile(delpath: imageFile + "/" + TUpdTm + ",zip");
} catch (Exception e1) {
e1.printStackTrace();
}
// 创建存放单证的文件夹
File cImFile = new File(imageFile);
if (!clmFile.exists() && !clmFile.isDirectory()) {
cImFile.mkdir();
}
// 创建以公式日期命名存放单证的文件夹
File clmNoFile = new File(pathname: imageFile + TUpdTm);
if (!clmNoFile.exists() && !clmNoFile.isDirectory()) {
clmNoFile.mkdir();
OutputStream os = null;
try {
List<String> fileLists = FtpUtil.listFiles(connect, dir);
List<String> gsNumList = new ArrayList<String>(0);
if (!fileLists.isEmpty()) {
/*List<String> gsList = ps.getGsList(fileLists);
for (String gs : gsList) {
for (String file : fileLists) {
if(file.contains(gs)){
gsNumList.add(file);
}
}
}*/
gsNumList = fileLists;
if (!gsNumList.isEmpty()) {
for (String file : gsNumList) {
File localFile = new File(pathname: imageFile + TUpdTm + "/" + file);
if (!localFile.getParentFile().exists()) {
localFile.getParentFile().mkdirs();
}
TocalFile.createNewFile();
connect.cd(dir);// 进入所在路径
os = new FileOutputStream(localFile);
connect.get(file, os);
os.close();
}
} else {
serviceResponse.setReturn_type(ActionConstant.RESPONSE_RESULT_TYPE_ERROR);
serviceResponse.setReturnMessage("该日期不存在公示异议!");
}
}
// /打包Zip
vatService.createZip(sourcePath: imageFile + TUpdTm, zipPath: imageFile + TUpdTm + ".zip");
isTUpdTmValid = true; // 标记TUpdTm满足if条件
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != ftpFileInput) {
ftpFileInput.close();
}
os.close();
// 删除文件夹下所有文件
serviceResponse.setReturnMessage("true");
}
}
}
}
if (!isTUpdTmValid) { // TUpdTm三次均不满足if条件
serviceResponse.setReturn_type(ActionConstant.RESPONSE_RESULT_TYPE_ERROR);
serviceResponse.setReturnMessage("该日期不存在公示异议!");
return serviceResponse;
} else {
// do something
}
```
阅读全文