public class FileUtil {
private String filePath;
private SimpleDateFormat sdf;
public FileUtil() {
sdf = new SimpleDateFormat("yyyy-MM-dd");
filePath = "C:" + File.separator + "files" + File.separator + sdf.format(System.currentTimeMillis());
}
public List<AttachFile> fileSave(Collection<Part> parts) {
File f = new File(filePath);
List<AttachFile> attachFileList = new ArrayList<>();
if (!f.exists()) {
f.mkdirs();
}
for (Part part : parts) {
String originalName = part.getSubmittedFileName();
String saveFileName = UUID.randomUUID().toString().substring(0, 8) + "_" + originalName;
if (part.getSubmittedFileName() != null) {
try {
part.write(f.getAbsolutePath() + File.separator + saveFileName);
attachFileList.add(new AttachFile(originalName, saveFileName, f.getAbsolutePath()));
} catch (IOException ie) {
ie.printStackTrace();
}
}
}
return attachFileList;
}
}
The file is stored in a path outside the context root. After adding or updating a file, the file name and path are stored in the database, and I confirmed that the file is written in the specified path. However, when I go into the post, I cannot load the file information from the database using the post’s primary key (pk). But if I turn off and then restart the server, I can properly load it through the post’s pk. I don’t know why. Please help.
I want to immediately check the added attachment by retrieving file information from the database using the post’s primary key (PK) after writing or editing a post.