java 删除本地图片_java删除服务器上的图片

import java.io.File;

import org.apache.log4j.Logger;

public class ClearTempImg {

private static final Logger logger = Logger.getLogger(ClearTempImg.class);

private final static String FS = System.getProperty("file.separator");

private static String TOMCAT_HOME = System.getProperty("catalina.home");

private static String PATH = "temp";

private static final String TEMP_PATH = TOMCAT_HOME + FS + PATH + FS;

private static boolean isRunning = false;

public void run() {

if (!isRunning) {

logger.debug("删除/Tomcat/Temp/目录下过期图片执行开始...");

isRunning = true;

logger.debug("开始执行删除tomcat下temp文件夹png文件任务");

logger.debug("要删除图片文件所在路径为" + TEMP_PATH);

File fileTemp = new File(TEMP_PATH);

// 判断文件是否存在

boolean falg = false;

falg = fileTemp.exists();

if (falg) {

logger.debug("temp文件存在");

if (true == fileTemp.isDirectory()) {

logger.debug("temp文件是个目录");

String[] png = fileTemp.list();

for (int i = 0; i < png.length; i++) {

if (true == png[i].endsWith("png")) {

File file = new File(TEMP_PATH + FS + png[i]);

if (true==file.isFile()) {

boolean flag = false;

flag = file.delete();

if (flag) {

logger.debug("成功删除无效图片文件:" + file.getName());

}

}

}

}

}

} else {

logger.debug("未找到tomcat/temp文件夹,执行失败,请手动删除temp下所有png文件");

}

isRunning = false;

logger.debug("删除/Tomcat/Temp/目录下过期图片执行结束...");

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用JSch库来实现Java SFTP文件上传和删除。以下是一个示例代码,可以上传Jar文件并删除服务器上的旧Jar文件: ```java import com.jcraft.jsch.*; import java.io.File; public class SftpUploadAndDelete { public static void main(String[] args) { String hostname = "hostname"; int port = 22; String username = "username"; String password = "password"; String localFilePath = "localFilePath"; String remoteDirectoryPath = "remoteDirectoryPath"; String remoteJarFileName = "remoteJarFileName"; JSch jsch = new JSch(); try { Session session = jsch.getSession(username, hostname, port); session.setPassword(password); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp"); channelSftp.connect(); // 上传新的Jar文件 channelSftp.put(localFilePath, remoteDirectoryPath + remoteJarFileName); // 删除旧的Jar文件 String oldJarFilePath = remoteDirectoryPath + getLatestJarFileName(channelSftp, remoteDirectoryPath, remoteJarFileName); channelSftp.rm(oldJarFilePath); channelSftp.disconnect(); session.disconnect(); System.out.println("Jar file uploaded and old jar file deleted successfully."); } catch (JSchException | SftpException e) { e.printStackTrace(); } } private static String getLatestJarFileName(ChannelSftp channelSftp, String remoteDirectoryPath, String remoteJarFileName) throws SftpException { String latestJarFileName = ""; SftpATTRS attrs = null; String[] files = channelSftp.ls(remoteDirectoryPath); for (String file : files) { if (file.startsWith(remoteJarFileName) && file.endsWith(".jar")) { if (attrs == null) { attrs = channelSftp.stat(remoteDirectoryPath + file); latestJarFileName = file; } else { SftpATTRS newAttrs = channelSftp.stat(remoteDirectoryPath + file); if (newAttrs.getMTime() > attrs.getMTime()) { attrs = newAttrs; latestJarFileName = file; } } } } return latestJarFileName; } } ``` 您需要将上面的代码中的以下变量替换为您的实际值: - hostname:SFTP服务器的主机名。 - port:SFTP服务器的端口号。 - username:SFTP服务器的用户名。 - password:SFTP服务器的密码。 - localFilePath:本地Jar文件的路径。 - remoteDirectoryPath:远程目录的路径。 - remoteJarFileName:远程Jar文件的名称。 代码将上传新的Jar文件,然后查找并删除远程目录中旧的Jar文件。删除旧Jar文件的方法是通过获取远程目录中所有以指定名称开头且以“.jar”结尾的文件列表,然后比较它们的修改时间,找到最新的Jar文件并删除

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值