String path=Thread.currentThread().getContextClassLoader().getResource("").toString(); path=path.replace('/', '\\'); // 将/换成\
path=path.replace("file:", ""); //去掉file:
path=path.replace("classes\\", ""); //去掉classes\
path=path.replace("target\\", ""); //去掉target\
path=path.substring(1); //去掉第一个\,如 \D:\JavaWeb...
//文件添加下级目录地址
path+= "WEB-INF"+File.separator +"view" + File.separator +"image";
File file = new File(path);
if (!file.exists()) {
}
String filename = System.currentTimeMillis() + UUID.randomUUID().toString() + ".png";
FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path + File.separator +filename));
imageOutput.write(fs, 0, fs.length);
imageOutput.close();
本文介绍了一种在Java中通过当前线程获取指定文件路径的方法,并演示了如何利用该路径保存图片到特定目录的过程。文章详细展示了字符串处理技巧,包括替换和截取操作,最终实现了图片的正确保存。
310

被折叠的 条评论
为什么被折叠?



