java 文件上传 配置_JAVA从入门到精通之使用java的MultipartFile实现layui官网文件上传实现全部示例,java文件上传…

  • Post author:
  • Post category:java

本篇文章探讨了JAVA从入门到精通之使用java的MultipartFile实现layui官网文件上传实现全部示例,java文件上传,希望阅读本篇文章以后大家有所收获,帮助大家对相关内容的理解更加深入。

85eb9496ad5ada44c88fffa8f113f73f.png

layui(谐音:类UI) 是一款采用自身模块规范编写的前端 UI 框架,遵循原生 HTML/CSS/JS 的书写与组织形式,门槛极低,拿来即用。

本次教程是基于springboot2.0的。废话少说,进入主题。

在pom文件导入commons-fileupload 1.3.3依赖或者jar包commons-fileupload            commons-fileupload            1.3.3

1.导入完毕之后配置bean@Configurationpublic class SpringBean {    //设置文件上传的配置

@Bean(name = “”multipartResolver””)    public CommonsMultipartResolver multipartResolver(){

CommonsMultipartResolver resolver = new CommonsMultipartResolver();

resolver.setDefaultEncoding(“”UTF-8″”);

resolver.setMaxUploadSize(52428800);//设置上传文件的最大值

resolver.setResolveLazily(true);        return resolver;

}

}

2.在yml配置文件file:

staticAccessPath: /upload/**

uploadFolder: G://upload/

3.然后配置文件夹映射,不然无法在项目中获取电脑上的文件

不过这个好像和文件上传没有关系,哈哈哈。想配置的就配置一下吧,反正之后获取图片时也会配置的。@Configurationpublic class WebMvcConfig extends WebMvcConfigurerAdapter {

@Value(“”${file.staticAccessPath}””)    private String staticAccessPath;

@Value(“”${file.uploadFolder}””)    private String uploadFolder;

@Override    public void addResourceHandlers(ResourceHandlerRegistry registry) {

registry.addResourceHandler(staticAccessPath).addResourceLocations(“”file:”” + uploadFolder);

registry.addResourceHandler(“”/static/**””).addResourceLocations(“”classpath:/static/””);

}}

4.之后创建一个实体类,当然你也可以不创建,直接把MultipartFile写在方法参数中,但是一定要做注意参数命名一定要是file,一定要是file,一定要是file。重要的事情说三遍。

实体类中MultipartFile属性名也必须叫file。写实体类的好处是可以方便携带其他的参数,比如和图片绑定的用户id等等FileVofile;    public FileVo(MultipartFile multipartFile) {        this.file = multipartFile;

}    public FileVo(){}    public MultipartFile getFile() {        return file;

}    public void setFile(MultipartFile file) {        this.file = file;

}

}

5.然后在Controller层加入以下方法。因为只是测试所以就写到了controller层,当然你也可以把文件上传方法放到util类,或者service层@RestController

@RequestMapping(“”/upload””)public class UploadController {

@Value(“”${file.uploadFolder}””)    private String uploadFolder;    /**

* UUID随机的命名

* 普通上传,多图片上传

* @param fileVo

* @return     */

@RequestMapping(“”/randomUplad””)    public Map randomUplad(FileVo fileVo){

System.out.println(“”fileVo:””+fileVo.getFile());

String fileUrl = fileUpload(fileVo,true);        return this.file(fileUrl);

}    /**

* 上传文件的全名称命名

* 指定允许上传的文件类型,允许上传的文件后缀,视频

* @param fileVo

* @return     */

@RequestMapping(“”/nameUplad””)    public Map nameUplad(FileVo fileVo){

System.out.println(“”fileVo:””+fileVo.getFile());

String fileUrl = fileUpload(fileVo,false);        return this.file(fileUrl);

}    /**

* 文件上传,随机UUID

* @param fileVo 上传的实体

* @param b 判断是用随机的uuid还是文件的名称,默认为UUID

* @return 上传的URL地址     */    public String fileUpload(FileVo fileVo,boolean b) {        if (fileVo.getFile()==null) {            return null;

}        //随机一个id

String name = fileVo.getFile().getOriginalFilename();        if (b) {   //判断是否用什么命名,true为UUID,false为上传文件的全名称            //获取文件的后缀

String[] split = name.split(“”\\.””);     //  匹配小数点(.)必须转义,否则split方法无效

name = UUID.randomUUID().toString().replaceAll(“”-“”, “”””)+””.””+split[split.length-1];

}

String fileUrl = this.uploadFolder+””/””+name;

System.out.println(“”fileUrl:””+fileUrl);

File file = new File(fileUrl);        try {

fileVo.getFile().transferTo(file);

} catch (IOException e) {

fileUrl = null;

}        return fileUrl;

}

@RequestMapping(“”/fileUploads””)    public Map fileUploads(MultipartFile file[]){        return this.file(null);

}    /**

* 返回是否上传成功的参数

* @param url

* @return     */    public Map file(String url){

Map map = new HashMap();        if (url==null||url==””””) {

map.put(“”msg””,””上传失败””);

map.put(“”code””,500);            return map;

}

map.put(“”msg””,””上传成功””);

map.put(“”code””,200);

map.put(“”url””,url);        return map;

}

}

本文由职坐标整理发布,学习更多的相关知识,请关注职坐标IT知识库!


版权声明:本文为weixin_33460449原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。