zoukankan      html  css  js  c++  java
  • java删除附件图片

    package com.zjn.IO;
    
    import java.io.File;
    
    public class DeleteFileController {
    	/**   
         * 删除文件,可以是单个文件或文件夹   
         * @param   fileName    待删除的文件名   
         * @return 文件删除成功返回true,否则返回false   
         */   
        public static boolean delete(String fileName){    
            File file = new File(fileName);    
            if(!file.exists()){    
                System.out.println("删除文件失败:"+fileName+"文件不存在");    
                return false;    
            }else{    
                if(file.isFile()){    
                    //删除文件     
                    return deleteFile(fileName);    
                }else{ 
                	//删除文件夹
                    return deleteDirectory(fileName);    
                }    
            }    
        }    
             
        /**   
         * 删除单个文件   
         * @param   fileName    被删除文件的文件名   
         * @return 单个文件删除成功返回true,否则返回false   
         */   
        public static boolean deleteFile(String fileName){    
            File file = new File(fileName);    
            if(file.isFile() && file.exists()){    
                file.delete();    
                System.out.println("删除单个文件"+fileName+"成功!");    
                return true;    
            }else{    
                System.out.println("删除单个文件"+fileName+"失败!");    
                return false;    
            }    
        }    
             
        /**   
         * 删除目录(文件夹)以及目录下的文件   
         * @param   dir 被删除目录的文件路径   
         * @return  目录删除成功返回true,否则返回false   
         */   
        public static boolean deleteDirectory(String dir){    
            //如果dir不以文件分隔符结尾,自动添加文件分隔符    
            if(!dir.endsWith(File.separator)){    
                dir = dir+File.separator;    
            }    
            File dirFile = new File(dir);    
            //如果dir对应的文件不存在,或者不是一个目录,则退出    
            if(!dirFile.exists() || !dirFile.isDirectory()){    
                System.out.println("删除目录失败"+dir+"目录不存在!");    
                return false;    
            }    
            boolean flag = true;    
            //删除文件夹下的所有文件(包括子目录)    
            File[] files = dirFile.listFiles();    
            for(int i=0;i<files.length;i++){    
                //删除子文件    
                if(files[i].isFile()){    
                    flag = deleteFile(files[i].getAbsolutePath());    
                    if(!flag){    
                        break;    
                    }    
                }    
                //删除子目录    
                else{    
                    flag = deleteDirectory(files[i].getAbsolutePath());    
                    if(!flag){    
                        break;    
                    }    
                }    
            }    
                 
            if(!flag){    
                System.out.println("删除目录失败");    
                return false;    
            }    
                 
            //删除当前目录    
            if(dirFile.delete()){    
                System.out.println("删除目录"+dir+"成功!");    
                return true;    
            }else{    
                System.out.println("删除目录"+dir+"失败!");    
                return false;    
            }    
        }    
      //删除文件夹
      //param folderPath 文件夹完整绝对路径
           public static void delFolder(String folderPath) {
           try {
              delAllFile(folderPath); //删除完里面所有内容
              String filePath = folderPath;
              filePath = filePath.toString();
              java.io.File myFilePath = new java.io.File(filePath);
              myFilePath.delete(); //删除空文件夹
           } catch (Exception e) {
             e.printStackTrace(); 
           }
      }
     
      //删除指定文件夹下所有文件
      //param path 文件夹完整绝对路径
         public static boolean delAllFile(String path) {
             boolean flag = false;
             File file = new File(path);
             if (!file.exists()) {
               return flag;
             }
             if (!file.isDirectory()) {
               return flag;
             }
             String[] tempList = file.list();
             File temp = null;
             for (int i = 0; i < tempList.length; i++) {
                if (path.endsWith(File.separator)) {
                   temp = new File(path + tempList[i]);
                } else {
                    temp = new File(path + File.separator + tempList[i]);
                }
                if (temp.isFile()) {
                   temp.delete();
                }
                if (temp.isDirectory()) {
                   delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
                   delFolder(path + "/" + tempList[i]);//再删除空文件夹
                   flag = true;
                }
             }
             return flag;
           }
        
        public static void main(String[] args) {
            String fileDir = "D:\logo.png";    
            DeleteFileController.delete(fileDir);
            System.out.println("删除文件结束");
            DeleteFileController t = new DeleteFileController();
            delFolder("D:/zz");
            System.out.println("deleted");
     
                 
        }    
    
    
    }
    

      // pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.cn</groupId>
      <artifactId>upload</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>
      <dependencies>
    	<dependency>
        	<groupId>org.springframework</groupId>
        	<artifactId>spring-webmvc</artifactId>
        	<version>4.0.6.RELEASE</version>
        </dependency>
       <!--  <dependency>
        	<groupId>com.fasterxml.jackson.core</groupId>
        	<artifactId>jackson-annotations</artifactId>
        	<version>2.2.3</version>
        </dependency> 
        <dependency>
        	<groupId>com.fasterxml.jackson.core</groupId>
        	<artifactId>jackson-databind</artifactId>
        	<version>2.2.3</version>
        </dependency> -->
        <!--文件上传下载所用的包============  -->
        <dependency>
        	<groupId>commons-fileupload</groupId>
        	<artifactId>commons-fileupload</artifactId>
        	<version>1.3.1</version>
        </dependency>
        <dependency>
        	<groupId>commons-io</groupId>
        	<artifactId>commons-io</artifactId>
        	<version>2.4</version>
        </dependency>
        <dependency> 
    	   <groupId>javax.servlet</groupId> 
    	   <artifactId>servlet-api</artifactId> 
    	   <version>2.5</version> 
    	   <scope>provided</scope> 
        </dependency> 
        <!-- jstl-api -->
            <dependency>
                <groupId>javax.servlet.jsp.jstl</groupId>
                <artifactId>jstl-api</artifactId>
                <version>1.2</version>
            </dependency>
            <!-- jstl-impl -->
            <dependency>
                <groupId>org.glassfish.web</groupId>
                <artifactId>jstl-impl</artifactId>
                <version>1.2</version>
            </dependency>
            <!-- 标签standard包 -->
            <dependency>
                <groupId>taglibs</groupId>
                <artifactId>standard</artifactId>
                <version>1.1.2</version>
            </dependency>
            <!-- 数据库配置 -->
            <!--mybatis mysql-->
    		<dependency>
    		    <groupId>com.alibaba</groupId>
    		    <artifactId>druid</artifactId>
    		    <version>1.0.2</version>
    		</dependency>
    		<dependency>
    		    <groupId>org.mybatis</groupId>
    		    <artifactId>mybatis-spring</artifactId>
    		    <version>1.2.2</version>
    		</dependency>
    		<dependency>
    		    <groupId>org.mybatis</groupId>
    		    <artifactId>mybatis</artifactId>
    		    <version>3.2.6</version>
    		</dependency>
    		<dependency>
    		    <groupId>mysql</groupId>
    		    <artifactId>mysql-connector-java</artifactId>
    		    <version>5.1.29</version>
    		</dependency>
    		<!-- jdbc驱动jar包 -->
    		<dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>4.1.1.RELEASE</version>
            </dependency>
            <!-- 生成token需要的jar包 -->
    	    <dependency>
    	      <groupId>com.auth0</groupId>
    	      <artifactId>java-jwt</artifactId>
    	      <version>3.8.2</version>
    	    </dependency>
    	    <!--  企业微信-->
    		<dependency>
    		    <groupId>com.google.code.gson</groupId>
    		    <artifactId>gson</artifactId>
    		    <version>2.8.2</version>
    		</dependency>
    		<dependency>
    		    <groupId>org.apache.httpcomponents</groupId>
    		    <artifactId>httpclient</artifactId>
    		    <version>4.5.2</version>
    		</dependency>
    		 
    		<dependency>
    		    <groupId>org.apache.httpcomponents</groupId>
    		    <artifactId>httpcore</artifactId>
    		    <version>4.4.5</version>
    		</dependency>
    		<dependency>
    		    <groupId>net.sf.json-lib</groupId>
    		   <artifactId>json-lib</artifactId>
    		   <version>2.4</version>
    		  <classifier>jdk15</classifier>
    		</dependency>
    		<dependency>
    		    <groupId>com.alibaba</groupId>
    		    <artifactId>fastjson</artifactId>
    		    <version>1.2.51</version>
    		</dependency>
    </dependencies>
    </project>
    

      

  • 相关阅读:
    如果获取 上传域的文件名
    更改织梦后台广告
    html5 让IE6,7支持HTML5语义化标签的文件
    如何获取选中的复选框
    手机下的ev.pageX无效
    Ajax
    列出远程git的全部分支
    扫描局域网中Gogs服务器(ruby)
    Xcode删除无用的Symbols信息
    Docker
  • 原文地址:https://www.cnblogs.com/xianz666/p/14780712.html
Copyright © 2011-2022 走看看