• HTML5使用Ajax上传文件


    HTML5页面

    1. <form id="uploadForm" enctype="multipart/form-data">
    2. <input id="file" type="file" name="file" multiple="multiple" onchange="selectFile()"/><br>
    3. form>

    JS

    //引入jquery
    
    

    项目需要阿里巴巴的fastjson以便返回json数据

    pom.xml

    
       com.alibaba
       fastjson
       1.2.59
    

    后台代码

    保存路径是

    项目文件\target\classes\static\images\temp

    @PostMapping("upload")
    @ResponseBody
    public JSONObject upload(@RequestParam("file") MultipartFile file){
        JSONObject josn=new JSONObject();
        try {
            String pathA= ResourceUtils.getURL("classpath:").getPath()+"static";
            String detail_path="/images/temp/";
             
        if (file.isEmpty()){
            josn.put("code","error");
            josn.put("message","未选择文件");
            return josn;
        }
        File fileA=new File(pathA); 
        if(!fileA.exists){
             fileA.mkdirs();
       } 
        String filename = file.getOriginalFilename(); //获取上传文件原来的名称
        String filePath = pathA+detail_path;
        System.out.println(filePath);
        File temp = new File(filePath);
        if (!temp.exists()){
            temp.mkdirs();
        }
    
            File localFile = new File(filePath+filename);
            file.transferTo(localFile); //把上传的文件保存至本地
            System.out.println(file.getOriginalFilename()+" 上传成功");
            josn.put("code","sucess");
            //返回文件上传后的目录
            josn.put("message",detail_path+filename);
            return josn;
        }catch (IOException e){
            e.printStackTrace();
            josn.put("code","error");
            josn.put("message","文件上传失败,原因是:"+e.getMessage());
            return josn;
        }
    }
  • 相关阅读:
    android自定义Apk名称和指定生成的路径
    「干货」从动态的角度分析DDR的时序结构
    接口测试经验分享
    spark知识点总结(1)
    BSV 上的 PLONK
    基于javaweb的医院门诊查询系统(前端+后端)
    c 文本终端直接写framebuffer 显示直线
    OpenSSL生成PKCS#8私钥和公钥
    2022 极术通讯-基于安谋科技 “星辰” STAR-MC1的灵动MM32F2570开发板深度评测
    解决Qt 构建项目时报错找不到serialport模块
  • 原文地址:https://blog.csdn.net/weixin_44710155/article/details/126103491