How to delete uploaded pictures after JAVA forms submit with pictures sql things are rolled back abnormally

the form is submitted with the picture. Save the picture first. After saving the picture, save the path to the database to get the id, and then assign the id to the form object entity.

if after the picture is saved, the sql of the saved form needs to be rolled back, and sql can roll back. How do I delete the previously uploaded file?

@Transactional(rollbackFor = Exception.class)
public int insert(Form form, MultipartFile file) throws IOException {
    if (!file.isEmpty()) {
    // 
    form.setImage(sysFileService.upLoad(file).getId());
    }
    return mapper.insert(form);
}

when an exception occurs, you can specify the exception handling class manually. Can I rewrite an exception handling class, send some path information after the file is uploaded, and then delete the file?

Jul.25,2021

you can consider not submitting the picture using form . Upload the picture to the special attachment table with the upload component, return the id, form and submit it to the background with the attachment id. When rolling back, delete the picture according to the attachment id find the picture path, delete the picture according to the attachment table record


Direct try catch insert method, delete any exception directly, and throw an exception

.
Menu