java前端上传图片保存到本地文件夹

废话不多说。上代码。jar包也给你们搞来了。。

后端java代码


import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Map;
import java.util.UUID;

@Configuration
@Controller
public class FileController implements WebMvcConfigurer {



    @PostMapping("/addparent")
    @ResponseBody
    public String upFile(@RequestParam("file") MultipartFile file, Map map) {
        String path = "D:/devsoft1/img/";
        String fileName = file.getOriginalFilename();
        String suffixName=fileName.substring(fileName.lastIndexOf("."));
        fileName= UUID.randomUUID()+suffixName;
        System.out.println(fileName);
        System.out.println("type::" + suffixName);
        System.out.println("filename::" + fileName);
        File targetFile = new File(path);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        File saveFile = new File(targetFile, fileName);
        try {
            file.transferTo(saveFile);
            System.out.println("执行成功");
            String path1 = path + fileName;
            System.out.println(path1);
       //     return "success";
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("执行失败");
            return "failed";
        }
        map.put("img",fileName);
        System.out.println("11111");
        return "success";

    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/devsoft1/**").addResourceLocations("file:D:/devsoft1/");

    }
}

前端html页面。别忘了引入js文件

     function upImg() {
            var fileObj = document.getElementById("file").files[0];
            var FileController = "/addparent";
            if(!fileObj.type.match('image.*')){
                alert("请选择正确的图片");

            }else{
                var form = new FormData();
                form.append("file",fileObj);
                var xhr = new XMLHttpRequest();
                xhr.onreadystatechange=function () {
                    if(this.readyState == 4 && this.status == 200){
                        var b = this.responseText;
                        if(b == "success"&&(fileObj.type.match('image.*'))){
                            alert("上传成功!");
                        }
                    }
                };
                xhr.open("post", FileController, true);
                xhr.send(form);
            }
        };
        function updateGuardian(){
            var imageurl=$('#file').val();
            alert(imageurl);
            var guname=$('#guname').val();
            var birth=$('#birth').val();
            var sex=$('#sex').val();
            var gunative=$('#gunative').val();
            var card=$('#card').val();
            var typecode=$('#typecode').val();
            var record=$('#record').val();
            var phone=$('#phone').val();
            var residence=$('#residence').val();
            var address=$('#address').val();
            var work=$('#work').val();
            var job=$('#job').val();
            if(guname!=""){
                $.ajax({
                    type: "POST",
                    dataType: "json",
                    url: "/stuguardian?stucode="+stucode+"&gucode"+gucode+"&imageurl="+imageurl+"&guname="+guname+"&birth="+birth+
                        "&sex="+sex+ "&gunative="+gunative+"&card="+card+"&typecode="+typecode+"&record="+record+"&record="+record+
                        "&phone="+phone+"&residence="+residence+"&address="+address+ "&work="+work+"&job="+job ,
                    contentType: "application/json",
                    success: function (data) {
                        if(data["code"]=="1"){
                            alert("修改成功")
                            window.location.reload();
                        }else{
                            alert("修改失败")
                        }
                    },
                    error : function() {
                        alert("异常!");
                    }
                });
            }else{
                alert("姓名未填写");
                return false;
            }
        };
    

html文件

基础信息


上传照片
姓名:
性别:
生日:

籍贯:
身份证号:

关系:
学历:
手机:
户籍所在地:
常住地址:

工作单位:
从事职业:

小明的父亲-系统关联信息


绑定账号:
考  勤  卡:

博主水平有限。。不足之处请指证。。

你可能感兴趣的:(上传文件,ajax,前端,springboot,上传图片)