04. Upload avatar with spring and easyUI

Time:2020-12-1
/**
     *The method of saving or updating here is mainly to format the problem of saving and modifying function parameters, and directly create a class to standardize success and MSG
     *Note: here is the processing of uploading the Avatar:
     *1. Get the path to the uploaded file and judge whether the folder exists or not. If not, create one directly
     *2. Get the ID of employee and the file name of fileimage
     *Modification: 3. If the ID is not empty and the file name is not an empty string, the modification is in progress
     *3.1. Get the name of the object's head image according to the ID and delete it directly if it exists according to the parent path
     * 3.2. and then upload the file upload tool class to upload.
     *Add: 4. If the ID is empty and the file name part is empty string, it means that when adding users, the upload tool class will be called directly to upload, and the new file name will be set into the new object
     * @param employee
     * @return
     */
    public AjaxResult saveOrUpdate(Employee employee, MultipartFile fileImage, HttpServletRequest req){
        try {
            //Dynamic access to upload file path
            String parentPath = req.getServletContext().getRealPath("/upload");
            //Get folder based on file path
            File file = new File(parentPath);
            //If this folder does not exist, create a folder directly
            if (!file.exists()){
                file.mkdir();
            }
            //Get the ID of the incoming object. If the ID is null, it means that it is added. If it is not null, it means that it is modified
            Long id = employee.getId();
            //Get the file name. If the file name is null and no file has been uploaded, if it is not null, the file has been uploaded
            String filename = fileImage.getOriginalFilename();


            //If the ID is not equal to null, it means that it is being modified, while if the file name is not equal to null, it means that the user has uploaded it
            if (id != null && filename != "") {
                //This is to get the original image name of the current object
                String sourceImage = employeeService.findById(employee.getId()).getHeadImage();
                //Here is to intercept the head image name and the parent path splicing, and then delete
                File file1 = new File(parentPath, sourceImage.split("/")[2]);
                //If the ID is not null and the file name passed in is not null, it is determined that the modification is in progress, and the modification operation is performed
                //If the file exists, it will be deleted directly because it is a modification and a new avatar is passed in
                if (file1 != null) {
                    file1.delete();
                }
                //If the file name is no longer uploaded, call the upload file tool class to upload
                String uploadName = FileUpLoad.upload(parentPath, fileImage);
                //Then set the file name to the modified object, and then save it
                employee.setHeadImage("/upload"+"/"+uploadName);
            }

            //This means that new users are being added, so there is no ID, but the file has been uploaded
            if (id == null && filename != ""){
                //If the file name is no longer uploaded, call the upload file tool class to upload
                String uploadName = FileUpLoad.upload(parentPath, fileImage);
                //Then set the file name to the modified object, and then save it
                employee.setHeadImage("/upload"+"/"+uploadName);
            }

            //Save the object directly
            employeeService.save(employee);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            Return new Ajax result (false, "operation failed" + e.getmessage());
        }
    }

JS part

save: function () {
            //Get the input tag under the form tag with ID FF and the value of name = ID
            var id = $("#ff input[name='id']").val();
            //The default URL address is save
            var url = "/employee/save";
            console.log(1);
            If (ID) {// if the tag has a value, it means that the modification is made, and the URL address will be different
                url = "/employee/update";
            }
            console.log(url);
            console.log(id);
            console.log(2);
            ff.form("submit",{

                type:'post',
                url:url,
                onsubmit:function () {
                    console.log(ff.form("validate"));
                    return ff.form("validate");
                },
                success:function (r) {
                    console.log(r);
                    if (r){
                        dlg.dialog("close");
                        dg.datagrid("load");
                    }else {
                        $. messager.alert ("error", "save failed", "error");
                    }
                }
            })
        }

HTML section

<tr>
   <td>Head image:</td>
       <td>
       <input name="fileImage" id="fileImage"  class="easyui-filebox" ></input>
       </td>
</tr>

Recommended Today

C # regular implementation of UBB parsing class code

The parsed code can pass XHTML   one   Strict verification;It includes title, link, font, alignment, picture, reference, list and other functions  Ubb.ReadMe.htm UBB code description title [H1] title I [/ H1] Title I [H2] Title II [/ H2] Title II [H1] Title III [/ H1] Title III [H4] Title IV [/ H4] Title IV […]