String pdfFileAddr="E:/phpstudy_pro/WWW/php_jr/HEMSTEST/orderPdfFile/2025/0522/czout_material/32/YCHC202503171559_271344_32_out_baoguan_material.pdf";
File sourcefilePdf=new File(pdfFileAddr);
String attFile = "E:/phpstudy_pro/WWW/php_jr/HEMSTEST/attachFiles";
Date date = new Date();
SimpleDateFormat yyyy = new SimpleDateFormat("yyyy");
SimpleDateFormat mmdd = new SimpleDateFormat("MMdd");
String fileYyyy = yyyy.format(date);
String fileMmdd = mmdd.format(date);
String newPath=attFile+"/"+fileYyyy + "/" + fileMmdd;
File newFile=new File(newPath);
if(!newFile.exists()){
newFile.mkdirs();
}
//文件重新命名
String relativeFullPath = (FileUtil.generateFilename(sourcefilePdf.getName()));
System.out.println("=====relativeFullPath=>"+relativeFullPath);
String[] arrStr=relativeFullPath.split("/");
InputStream inStream = new FileInputStream(sourcefilePdf);
OutputStream outStream=new FileOutputStream(new File(newPath,arrStr[2]));
byte[] bytes=new byte[1024];
int length;
while ((length = inStream.read(bytes)) >= 0) {
outStream.write(bytes, 0, length);
}
outStream.close();
inStream.close();
public static String generateFilename(String originalFilename) {
Date date = new Date();
SimpleDateFormat yyyy = new SimpleDateFormat("yyyy");
SimpleDateFormat mmdd = new SimpleDateFormat("MMdd");
String fileYyyy = yyyy.format(date);
String fileMmdd = mmdd.format(date);
String fileExt = "";
int lastIndex = originalFilename.lastIndexOf('.');
if (lastIndex != -1) {
fileExt = originalFilename.substring(lastIndex);
}
SnowFlake idWorker = new SnowFlake(0, 0);
long id = idWorker.nextId();
String filename = fileYyyy + "/" + fileMmdd + "/" + id + fileExt;
return filename;
}