| | |
| | | package com.whyc.util; |
| | | |
| | | import org.springframework.boot.system.ApplicationHome; |
| | | import org.springframework.util.ResourceUtils; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileNotFoundException; |
| | | import java.io.FileOutputStream; |
| | | import java.io.IOException; |
| | | import java.net.InetAddress; |
| | | import java.net.UnknownHostException; |
| | | |
| | | public class FileUtils { |
| | | |
| | |
| | | out.flush(); |
| | | out.close(); |
| | | } |
| | | |
| | | /** |
| | | * 项目同级路径 |
| | | * @return |
| | | */ |
| | | public static String getFilePath() { |
| | | ApplicationHome ah = new ApplicationHome(FileUtils.class); |
| | | File file = ah.getSource(); |
| | | // String projectDir = file.getParentFile().getAbsolutePath(); |
| | | String projectDir = file.getAbsolutePath(); |
| | | String dir = projectDir + File.separator + "upload"+File.separator; |
| | | System.out.println(dir); |
| | | return dir; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 生成项目同级路径 |
| | | * @return |
| | | * @throws FileNotFoundException |
| | | */ |
| | | public static String getProjectPath(){ |
| | | //获取跟目录 |
| | | File path = null; |
| | | try { |
| | | path = new File(ResourceUtils.getURL("classpath:").getPath()); |
| | | } catch (FileNotFoundException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | if (!path.exists()) { |
| | | path = new File(""); |
| | | } |
| | | //如果上传目录为/static/images/upload/,则可以如下获取 |
| | | File upload = new File(path.getAbsolutePath(), "static/upload/"); |
| | | if (!upload.exists()) { |
| | | upload.mkdirs(); |
| | | //在开发测试模式时,得到地址为:{项目跟目录}/target/static/images/upload/ |
| | | //在打成jar正式发布时,得到的地址为:{发布jar包目录}/static/images/upload/ |
| | | } |
| | | return upload.toString()+File.separator; |
| | | } |
| | | |
| | | } |