Recently I wrote a small demo, there are several documents related to the case method of operation, stay in this next direct paste the copied
1 / ** 2 * Save the file as a byte array . 3 * . 4 * @param BFILE . 5 * byte array . 6 * @param filePath . 7 * file path . 8 * @param fileName . 9 * file name 10 * @author zhangyanhua . 11 * @ date 2019 Nian 6 afternoon of January 5 3:52:50 12 * / 13 Private boolean saveFile ( byte [] BFILE, String filePath, String fileName) 14 { 15 BufferedOutputStream bos = null; 16 FileOutputStream fos = null; 17 File file = null; 18 try 19 { 20 File dir = new File(this.getFolder(filePath)); 21 if (!dir.exists() && !dir.isDirectory()) 22 { 23 dir.mkdirs(); 24 } 25 file = new File(filePath + "\\" + fileName); 26 File parentFile = file.getParentFile(); 27 if (!parentFile.exists() && !parentFile.isDirectory()) 28 { 29 parentFile.mkdirs(); 30 } 31 32 fos = new FileOutputStream(file); 33 bos = new BufferedOutputStream(fos); 34 bos.write(bfile); 35 return true; 36 } 37 catch (Exception e) 38 { 39 logger.error ( "Save File Error" + E); 40 return to false ; 41 is } 42 is the finally 43 is { 44 is IF (! BOS = null ) 45 { 46 is the try 47 { 48 bos.close (); 49 } 50 the catch (IOException E1) 51 is { 52 is e1.printStackTrace (); 53 is } 54 is } 55 IF (! Fos = null ) 56 is { 57 is the try 58 { 59 fos.close (); 60 } 61 is the catch (IOException E1) 62 is { 63 is e1.printStackTrace (); 64 } 65 } 66 } 67 } 68 69 / ** 70 * Gets the value conf.properties file name of a configuration corresponding to 71 * 72 * @param name 73 * @return 74 * @author zhangyanhua 75 * @date 2019年4月10日 上午10:50:39 76 */ 77 private String getConfValue(String name) 78 { 79 InputStream inStream; 80 try 81 { 82 Properties prop = new Properties(); 83 inStream = new FileInputStream(new File(confPath)); 84 prop.load(inStream); 85 return prop.getProperty (name); 86 } 87 the catch (a FileNotFoundException E) 88 { 89 logger.error ( "Profiles" + confPath + + "does not exist!" E); 90 } 91 is the catch (IOException E) 92 { 93 logger.error ( "Properties initialization failed!" + E); 94 } 95 return null ; 96 } 97 98 / ** 99 * directory string processing 100 * 101 * @param folder 102 * @return 103 * @author zhangyanhua 104 * @date 2019年4月11日 上午11:26:13 105 */ 106 private String getFolder(String folder) 107 { 108 if (StringUtils.isEmpty(folder)) 109 { 110 folder = ""; 111 } 112 else 113 { 114 if (!folder.endsWith("/")) 115 { 116 + = Folder "/" ; 117 } 1 18 } 119 return Folder; 120 } 121 122 / ** 123 * Get all files in the specified directory 124 * 125 * @param the fileList 126 * file set, the recursive call 127 * @param path 128 * need to get the catalog file 129 * @return 130 * @author zhangyanhua 131 * @date 2019 Nian 4 morning of October 10 11:26:16 132 * / 133 Private List <File> getFileList (List <File> the fileList, String path) 134 { 135 IF (StringUtils.isEmpty (path)) 136 { 137 logger.error ( "target file folder does not exist or is empty! " ); 138 return null ; 139 } 140 141 is File File = new new File (path); 142 IF (File.Exists ()) 143 { 144 File [] = Files File.listFiles (); 145 IF ( nullFiles || files.length == 0 == ) 146 { 147 logger.error ( "target file folder does not exist or is empty!" ); 148 return null ; 149 } 150 the else 151 { 152 for (File file1: files) 153 { 154 IF (file1.isDirectory ()) 155 { 156 // Get the absolute path of the file 157 getFileList (the fileList, file1.getAbsolutePath ()); 158 Continue ; 159 } 160. the else 161 { 162 fileList.add (file1); 163 } 164 is } 165 } 166 } 167 is the else 168 { 169 logger.error ( "target file does not exist!" ); 170. } 171 is return the fileList; 172 }