java 删除服务器文件(linux)

1、yml配置

# 本地文件上传    
file:
    domain: http://171.11.1.1:9300(ip+端口)
    path: /home/admin/test
    prefix: /statics

2、文件删除代码

/**
     * 上传文件存储在本地的根路径
     */
    @Value("${file.path}")
    private String localFilePath;

    /**
     * 文件存储ip
     */
    @Value("${file.domain}")
    private String domain;

    //文件存储前缀
    @Value("${file.prefix}")
    private String prefix;
/**
     * 文件删除
     */
    @GetMapping("/delete")
    public R delete(@RequestParam("fileUrl") String fileUrl) {
        try {
            String replace1 = localFilePath.replace("\\", "/");
            String url = fileUrl.replace(domain+prefix,replace1);
            File file = new File(url);
            if (file.exists()){
                file.delete();
            }else {
                return R.fail("文件不存在");
            }
            return R.ok("文件删除成功");
        } catch (Exception e) {
            log.error("文件删除失败", e);
            return R.fail(e.getMessage());
        }
    }
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java可以使用inotify-tools库来监听Linux服务器上的文件变化。 inotify-tools是一个用于Linux的C库,允许程序监视文件系统事件,例如文件或目录的创建,删除,修改等。Java可以使用JNA库来调用inotify-tools的API。 下面是一个使用inotify-tools和JNA库的Java程序,用于监听远程Linux服务器上的文件变化: ```java import com.sun.jna.Library; import com.sun.jna.Native; import com.sun.jna.Pointer; public class FileMonitor { public interface INotify extends Library { INotify INSTANCE = (INotify) Native.loadLibrary("c", INotify.class); int inotify_init(); int inotify_add_watch(int fd, String pathname, int mask); int inotify_rm_watch(int fd, int wd); int read(int fd, Pointer buf, int size); } public static void main(String[] args) throws Exception { int fd = INotify.INSTANCE.inotify_init(); String pathname = "/path/to/file"; int wd = INotify.INSTANCE.inotify_add_watch(fd, pathname, 0x00000100); byte[] buf = new byte[4096]; Pointer pointer = new Pointer(buf); while (true) { int bytesRead = INotify.INSTANCE.read(fd, pointer, buf.length); for (int i = 0; i < bytesRead; ) { int wd2 = pointer.getInt(i); i += 4; int mask = pointer.getInt(i); i += 4; int cookie = pointer.getInt(i); i += 4; int nameLen = pointer.getByte(i); i += 1; byte[] nameBytes = new byte[nameLen]; pointer.read(i, nameBytes, 0, nameBytes.length); String name = new String(nameBytes); i += nameLen; System.out.println("wd=" + wd2 + " mask=" + mask + " cookie=" + cookie + " name=" + name); } } } } ``` 这个程序使用了JNA库,它提供了对inotify-tools库的封装和调用。程序连接到远程主机,并使用inotify_add_watch函数监视指定文件的变化。在文件发生更改时,程序将打印出相应的事件信息。在使用前,需要将JNA库添加到项目依赖中。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值