版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38078822/article/details/81912167
package demo1.Bonze;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import demo1.Bonze.utils.UploadImageUtil;
import demo1.Bonze.utils.WaterMarkUtils;
public class w {
public static void main(String[] args) throws Exception {
//上传图片源地址
String str1 = "D:\\test";
//此处是创建新的文件夹,
// File file = new File(strPath);
// if(!file.exists()){
// file.mkdirs();
// }
isImgInfo(str1);
}
public static void isImgInfo(String path) throws Exception {
// 图片文件后缀
Set<String> sets = new HashSet<String>();
sets.add(".jpg");
sets.add(".png");
sets.add(".gif");
sets.add(".bmp");
sets.add(".tif");
File file = new File(path);
String str = file.toString();
System.out.println("====绝对路径为:"+file);
if(str.indexOf(".jpg")!=-1||str.indexOf(".png")!=-1
||str.indexOf(".gif")!=-1 ||str.indexOf(".JPG")!=-1||str.indexOf(".PNG")!=-1
||str.indexOf(".GIF")!=-1){
//上传文件
//获取文件名
String a = file.getName();
String parent = file.toString().replace("\\"+a, "");
System.out.println("parent父级路径:"+parent);
String parentRoad = parent.substring(parent.lastIndexOf("\\"),parent.length());
String parenttrue = parentRoad.replace("\\", "");
//父级路径attach
System.out.println("父级:"+parenttrue);
System.out.println("上传的文件名字"+a);
String fileType = a.substring(a.lastIndexOf("."),a.length());
String name = a.replace(fileType, " ");
System.out.println(""+name);
SimpleDateFormat formater = new SimpleDateFormat("yyy-MM-dd-HH:mm:ss:SSS");
Date date = new Date();
//attachmentTime====>c
String attachTime = formater.format(date);
System.out.println("图片上传时间:"+attachTime);
//图片上传
UploadImageUtil upload = new UploadImageUtil();
//此处为
//
String rename = UUID.randomUUID().toString()+fileType;
System.out.println("文件重命名后:"+rename);
upload.uploadImage(file,rename, "D:\\file");
// //打水印;获取上传后的文件名称
// WatermarkImgUtils water=new WatermarkImgUtils();
// water.addWatermark(sourceImgPath, tarImgPath, waterMarkContent, fileExt);
}
//获取原始文件的名称(作为表中图片描述);获取图片类型(譬如机构变迁)
// System.out.println(file.isDirectory());
if (file.isFile()) {
String fileName = file.getName();
System.out.println("--------文件名:"+fileName);
if (fileName.lastIndexOf(".") > 0) {
String sufix = fileName.substring(fileName.lastIndexOf("."));
if (sets.contains(sufix.toLowerCase())) {
String pathStr = file.getParentFile().getParent();
//获取原文件名
System.out.println("文件名:"+fileName);
//获取文件类型
String fileType = fileName.substring(fileName.lastIndexOf("."),fileName.length());
//attachmentName======>name
String name = fileName.replace(fileType, " ");
// System.out.println("attachName"+name);
System.out.println("文件类型:"+fileType);
System.out.println("文件父路径:"+file.getParentFile().getParent().substring(pathStr.lastIndexOf("\\"), pathStr.length()));
}
}
}
if (file.isDirectory()) {
// System.out.println(file.getCanonicalPath());
File[] files = file.listFiles();
for (File file2 : files) {
isImgInfo(file2.getAbsolutePath());
}
}
}
}