1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
   | import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths;
  import org.apache.commons.io.IOUtils; import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
 
 
 
 
  public class FileCopyUtils {
  	private static InputStream getResource(String location) throws IOException { 		PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); 		InputStream in = resolver.getResource(location).getInputStream(); 		byte[] byteArray = IOUtils.toByteArray(in); 		in.close(); 		return new ByteArrayInputStream(byteArray); 	} 	 	
 
 
  	private static String getCurrentDirPath() { 		URL url = FileCopyUtils.class.getProtectionDomain().getCodeSource().getLocation(); 		String path = url.getPath(); 		if(path.startsWith("file:")) { 			path = path.replace("file:", ""); 		} 		if(path.contains(".jar!/")) { 			path = path.substring(0, path.indexOf(".jar!/")+4); 		}                    File file = new File(path);         path = file.getParentFile().getAbsolutePath();          return path; 	} 	 	private static Path getDistFile(String path) throws IOException { 		String currentRealPath = getCurrentDirPath(); 		Path dist = Paths.get(currentRealPath + File.separator + path); 		Path parent = dist.getParent(); 		if(parent != null) { 			Files.createDirectories(parent); 		} 		Files.deleteIfExists(dist); 		return dist; 	} 	 	
 
 
 
 
  	public static String copy(String location) throws IOException { 		InputStream in = getResource("classpath:"+location); 		Path dist = getDistFile(location); 		Files.copy(in, dist); 		in.close(); 		return dist.toAbsolutePath().toString(); 	} 	 }
   |