获取文件路径 java_java获取文件路径

java获取文件路径

2017/8/7 12:31:19  shuihui  程序员俱乐部  我要评论(0)

摘要:/***

Title:main*

Description:*@paramargs*/publicstaticvoidmain(String[]args){System.out.println(Thread.currentThread().getContextClassLoader().getResource(""));System.out.println(FilePath.class.getClassLoader().getResource(""))

/**

*

Title: main

*

Description:

* @param args

*/

public static void main(String[] args) {

System.out.println(Thread.currentThread().getContextClassLoader().getResource(""));

System.out.println(FilePath.class.getClassLoader().getResource(""));

System.out.println(ClassLoader.getSystemResource(""));

System.out.println(FilePath.class.getResource(""));

System.out.println(FilePath.class.getResource("/"));//Class文件所在路径

//System.out.println(newFile("/").getAbsolutePath());

System.out.println(System.getProperty("user.dir"));

}

输出结果:

file:/E:/WorkSpace/4G_NGJXPT/WebRoot/WEB-INF/classes/

file:/E:/WorkSpace/4G_NGJXPT/WebRoot/WEB-INF/classes/

file:/E:/WorkSpace/4G_NGJXPT/WebRoot/WEB-INF/classes/

file:/E:/WorkSpace/4G_NGJXPT/WebRoot/WEB-INF/classes/com/wellcom/dfo/biz_modules/coa/web/

file:/E:/WorkSpace/4G_NGJXPT/WebRoot/WEB-INF/classes/

E:\WorkSpace\4G_NGJXPT

public static void main(String[] args) throws Exception {

String httpPath = "http://122.225.207.133:81/coachregister/330100/20170731/b5d371d8ca7a4cd99330efc6447b3514.jpg";

System.out.println("本地路径  " + getHttpFile(httpPath));

System.out.println("本地路径  " + getClassPath());

}

/**

* 取class本地路径

* @return 本地路径

* @throws Exception

*/

public static String getClassPath() throws Exception{

String httpPath = "C:\\Users\\well\\AppData\\Local\\Temp\\VedioCapJP.bmp";

httpPath = httpPath.replace("\\", "/");

String localPath = PathLocate.getClassPath() + "tempFile"+ httpPath.substring(httpPath.lastIndexOf("/"));

localPath = java.net.URLDecoder.decode(localPath,"utf-8");

File files = new File(localPath);

try {

//判断目标文件所在的目录是否存在

if(!files.getParentFile().exists()) {

//如果目标文件所在的目录不存在,则创建父目录

if(!files.getParentFile().mkdirs()) {

throw new Exception("创建临时目标文件所在目录失败!");

}

}

if(!files.exists()){

if(!files.createNewFile()){

throw new Exception("文件不存在,创建失败!");

}

}

FileInputStream fin = new FileInputStream(files);

ByteArrayOutputStream outSteam = new ByteArrayOutputStream();

int sizeFile = (int) files.length();

int len = 0;

byte[] buffer = new byte[sizeFile];

while ((len = fin.read(buffer)) != -1) {

outSteam.write(buffer, 0, len);

}

outSteam.close();

fin.close();

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

return localPath;

}

获取当前文件所在的路径

File f = new File(FilePath.class.getResource("").getPath());

System.out.println(f);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值