SpringBoot实现文件上传接口[通俗易懂]

SpringBoot实现文件上传接口[通俗易懂]摘要公司都是采用SpringBoot作为项目框架,其实SpringBoot和SSM框架很接近,基本上只是将SSM的一些配置项修改为自动配置或者简单的注解配置就可以了,建议不了解的SpringBoot的朋友们可以了解一下,上手很快,其实文件上传框架根本没有多大关系。我只是顺便帮SpringBoot打个广告罢了。正题需求:需要实现一个文件上传的web接口。1、先实现一个Controll…

大家好,又见面了,我是你们的朋友全栈君。

摘要

公司都是采用SpringBoot作为项目框架,其实SpringBoot和SSM框架很接近,基本上只是将SSM的一些配置项修改为自动配置或者简单的注解配置就可以了,建议不了解的SpringBoot的朋友们可以了解一下,上手很快,其实文件上传框架根本没有多大关系。我只是顺便帮SpringBoot打个广告罢了。

正题

需求:需要实现一个文件上传的web接口。
1、先实现一个Controller接口,如下:

package com.lanxuewei.utils.aspect;

import com.lanxuewei.utils.interceptor.annotation.AppIdAuthorization;
import com.lanxuewei.utils.model.ReturnCodeAndMsgEnum;
import com.lanxuewei.utils.model.ReturnValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/** * @author lanxuewei Create in 2018/7/3 20:01 * Description: aop 测试控制器 */
@RestController
@RequestMapping(value = "/aop")
public class TestController {

    private static final Logger logger = LoggerFactory.getLogger(TestController.class);

    @Autowired
    private TestService testService;

    /** * 文件上传测试接口 * @return */
    @AppIdAuthorization
    @RequestMapping("/upload")
    public ReturnValue uploadFileTest(@RequestParam("uploadFile") MultipartFile zipFile) {
        return testService.uploadFileTest(zipFile);
    }
}

2、Service接口如下:

package com.lanxuewei.utils.aspect;

import org.springframework.web.multipart.MultipartFile;

import com.lanxuewei.utils.model.ReturnValue;

public interface TestService {

    public ReturnValue uploadFileTest(MultipartFile zipFile);
}

3、Service实现如下:

package com.lanxuewei.utils.aspect;

import com.lanxuewei.utils.model.ReturnCodeAndMsgEnum;
import com.lanxuewei.utils.model.ReturnValue;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

/** * @author lanxuewei Create in 2018/8/14 10:01 * Description: */
@Service
public class TestServiceImp implements TestService { 
   

    private static final Logger logger = LoggerFactory.getLogger(TestServiceImp.class);

    @Override
    public ReturnValue uploadFileTest(MultipartFile zipFile) {
        String targetFilePath = "D:\\test\\uploadTest";
        String fileName = UUID.randomUUID().toString().replace("-", "");
        File targetFile = new File(targetFilePath + File.separator + fileName);

        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(targetFile);
            IOUtils.copy(zipFile.getInputStream(), fileOutputStream);
            logger.info("------>>>>>>uploaded a file successfully!<<<<<<------");
        } catch (IOException e) {
            return new ReturnValue<>(-1, null);
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                logger.error("", e);
            }
        }
        return new ReturnValue<>(ReturnCodeAndMsgEnum.Success, null);
    }
}

说明:
1、targetFilePath为文件保存路径,本人用于测试所以指定路径,可根据实际情况进行修改。
2、fileName采用UUID生成,保证文件名唯一不重复,但是没有保留原文件后缀,可通过获取原文件文件名后,调用lastIndexOf(“.”)获取文件原后缀加上。
3、IOUtils为org.apache.commons.io.IOUtils,注意别导入错误。
4、本文中采用logback日志系统,可根据实际情况修改或删除。

附上ReturnValue以及ReturnCodeAndMsgEnum类,用于Controller层统一返回前端的model,如下:

package com.lanxuewei.utils.model;

import java.io.Serializable;

/** * @author lanxuewei Create in 2018/7/3 20:05 * Description: 统一web返回结果 */
public class ReturnValue<T> implements Serializable {
    private static final long serialVersionUID = -1959544190118740608L;
    private int ret;
    private String msg;
    private T data;

    public ReturnValue() {
        this.ret = 0;
        this.msg = "";
        this.data = null;
    }

    public ReturnValue(int retCode, String msg, T data) {
        this.ret = 0;
        this.msg = "";
        this.data = null;
        this.ret = retCode;
        this.data = data;
        this.msg = msg;
    }

    public ReturnValue(int retCode, String msg) {
        this.ret = 0;
        this.msg = "";
        this.data = null;
        this.ret = retCode;
        this.msg = msg;
    }

    public ReturnValue(ReturnCodeAndMsgEnum codeAndMsg) {
        this(codeAndMsg.getCode(), codeAndMsg.getMsg(), null);
    }

    public ReturnValue(ReturnCodeAndMsgEnum codeAndMsg, T data) {
        this(codeAndMsg.getCode(), codeAndMsg.getMsg(), data);
    }

    public int getRet() {
        return this.ret;
    }

    public void setRet(int ret) {
        this.ret = ret;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "ReturnValue{" +
                "ret=" + ret +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
package com.lanxuewei.utils.model;

/** * @author lanxuewei Create in 2018/7/3 20:06 * Description: web相关接口返回状态枚举 */
public enum  ReturnCodeAndMsgEnum {
    Success(0, "ok"),
    No_Data(-1, "no data"),
    SYSTEM_ERROR(10004, "system error");

    private String msg;
    private int code;

    private ReturnCodeAndMsgEnum(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static ReturnCodeAndMsgEnum getByCode(int code) {
        ReturnCodeAndMsgEnum[] var1 = values();
        int var2 = var1.length;

        for(int var3 = 0; var3 < var2; ++var3) {
            ReturnCodeAndMsgEnum aiTypeEnum = var1[var3];
            if (aiTypeEnum.code == code) {
                return aiTypeEnum;
            }
        }

        return Success;
    }

    public String getMsg() {
        return this.msg;
    }

    public int getCode() {
        return this.code;
    }
}

Postman发请求返回结果成功,以上代码只需要uploadFile一个参数即可。
这里写图片描述

注意事项: application.properties配置文件中可以配置文件上传相关属性,配置上传文件大小限制。
单个文件最大限制:spring.servlet.multipart.max-file-size=50Mb
单次请求最大限制:spring.servlet.multipart.max-request-size=70Mb

总结:本文功能较为简单,所以有些过程并没有更细致过程以及规范代码,比如存放路径采用项目路径,新文件名保持和原文件后缀一致等,需要的小伙伴可以根据自己业务进行修改。


续更,总觉得代码过于随意了,补充文件上传获得文件后缀相关函数

private String getFileSuffix(MultipartFile file) {
        if (file == null) {
            return null;
        }
        String fileName = file.getOriginalFilename();
        int suffixIndex = fileName.lastIndexOf(".");
        if (suffixIndex == -1) {    // 无后缀
            return null;
        } else {                    // 存在后缀
            return fileName.substring(suffixIndex, fileName.length());
        }
    }

在随机生成文件名后补充如下代码即可,如果返回文件后缀不为空则将其加入新产生的文件名中即可:

        String fileSuffix = getFileSuffix(zipFile);
        if (fileSuffix != null) {   // 拼接后缀
           fileName += fileSuffix;
        }
        File targetFile = new File(targetFilePath + File.separator + fileName);
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/131404.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 关于解决Codeblocks中文乱码问题

    关于解决Codeblocks中文乱码问题最近有很多博友在问我关于另一篇博文“表白小心心”等问题,我于是又年轻了一把———把代码复制下来,运行了一下,发现了诸多问题,当然不是代码的问题,而是编译器的一些问题。1、Codeblocks乱码Codeblocks在编译时没有任何问题,但是显示出来的汉字字符是乱码的。针对这一问题,我也找了一下解决方法,在这里给大家分享一下。首先打开你的Codeblocks,设置–&gt;编辑器,中英…

    2022年7月26日
    19
  • kafka安装

    kafka安装

    2020年11月19日
    210
  • RewriteRule指令[通俗易懂]

    RewriteRule指令[通俗易懂]作为RewriteRule指令的第三个参数。Flags是一个包含以逗号分隔的下列标记的列表: ‘last|L'(最后一个规则last)立即停止重写操作,并不再应用其他重写规则。它对应于Perl中的last命令或C语言中的break命令。这个标记可以阻止当前已被重写的URL为其后继的规则所重写。举例,使用它可以重写根路径的URL(‘/’)为实际存在的URL,比如,’/e/w

    2022年5月14日
    32
  • zabbix添加snmp监控项_SNMP协议

    zabbix添加snmp监控项_SNMP协议目录一、SNMPTrap消息处理流程二、snmptt1、SNMPTrap、snmptt安装2、配置文件修改3、SNMPTrapFile文件创建4、监控项创建三、perl脚本 1、SNMPTrap安装2、从zabbix源码包中拷贝perl脚本到/usr/bin/目录下,并增加执行权限3、修改snmptrapd.conf配置4、修改zabbix配置 …

    2022年8月20日
    4
  • 关于用户态和内核态的理解和认识_计算机内核态和用户态

    关于用户态和内核态的理解和认识_计算机内核态和用户态究竟什么是用户态,什么是内核态,这两个基本概念以前一直理解得不是很清楚,根本原因个人觉得是在于因为大部分时候我们在写程序时关注的重点和着眼的角度放在了实现的功能和代码的逻辑性上,先看一个例子:1)例子C代码1.     void testfork(){  2.     if(0 = = fork()){  3.     printf(“create new process su

    2022年9月18日
    0
  • J2ME开发环境配置(MyEclipse插件+WTK+jdk)

    J2ME开发环境配置(MyEclipse插件+WTK+jdk)MyeclipseJ2ME开发之环境配置的前言随着移动设备的普及和应用,在小型存储设备方面的研发进入了一个全新的时期,比如数字电视,PDA,移动存储通信设备等。而各方面的技术也进入了一个飞速发展的时期。尤其是近几年J2ME技术的发展。  而开发MIDlet应用程序有很多种开发工具可以选择,这些开发工具盒开发环境主要分为三大类:第一种是Sun公司的J2ME通用开发工具,例如J2ME无线开发工具包

    2022年7月27日
    1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号