wangeditor实现图片上传操作

wangeditor初始化:

editor = new wangEditor(this.$refs.editor);
                //设置上传图片的地址
editor.customConfig.uploadImgServer= "/course/upload";//设置图片上传地址
 //默认上传时使用的参数名
 editor.customConfig.uploadFileName = 'img';//设置图片上传参数
 editor.create();//创建wangEditor
 //设置初始内容
 editor.txt.html(this.form.description);

java srping boot实现:
引入依赖:

<dependency>
            <groupId>commons-fileuploadgroupId>
            <artifactId>commons-fileuploadartifactId>
            <version>1.4version>
        dependency>

将其加入spring boot容器中

@Configuration
public class WebConfig {
    @Bean
    public CommonsMultipartResolver multipartResolver(){
        CommonsMultipartResolver commonsMultipartFile=new CommonsMultipartResolver();
        commonsMultipartFile.setDefaultEncoding("utf-8");
        return commonsMultipartFile;
    }
}

controller层的方法:

@PostMapping("/upload")
    @ResponseBody
    public Map add(@RequestParam("img")MultipartFile file, HttpServletRequest httpServletRequest) throws IOException {

        String s = httpServletRequest.getServletContext().getResource("/").getPath() + "/upload/";
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String format = simpleDateFormat.format(new Date());
        String substring = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        file.transferTo(new File(s+format+substring));//保存文件
        Map re=new LinkedHashMap();
        re.put("errno",0);	//这两个key值不能变
        re.put("data",new String[]{"/upload/"+format+substring});


        return re;
    }

你可能感兴趣的:(java,spring,boot,maven)