一、配置上传文件大小
# 开发环境配置 spring: profiles: dev servlet: multipart: max-request-size: 100MB max-file-size: 100MB
二、文件类型分类(非必须)
package com.xxxxx; import java.util.Arrays; import java.util.List; public interface Consts { /** * 文件后缀 */ //文档 List<String> document= Arrays.asList(".doc",".docx",".xls",".xlsx",".ppt",".pptx",".txt",".pdf"); //图片 List<String> picture=Arrays.asList(".jpg",".png",".JPEG",".bmp",".gif"); //视屏 List<String> video=Arrays.asList(".avi",".mp4",".rmvb",".flv",".wmv"); //音频 List<String> audio=Arrays.asList(".mp3",".wav",".amr"); //压缩 List<String> compress=Arrays.asList(".zip",".rar",".7z"); }
三、不同类型对应的文件夹(非必须)
package com.xxxxx.enums; /** * 文件类型枚举 * @Author xxx */ public enum FileUploadTypeTypeEnum { //文档 DOCUMENT ("/document/"), //图片 PICTURE ("/picture/"), //视屏 VIDEO ("/video/"), //音频 AUDIO ("/audio/"), //压缩 COMPRESS ("/compress/"); private String name; FileUploadTypeTypeEnum(String name) { this.name = name; } public String getName() { return name; } }
四、上传工具类
package com.xxxx.utils; import com.xxxx.FileUploadTypeTypeEnum; import com.xxxx.ServiceException; import org.xxxx.MultipartFile; import javax.servlet.http.HttpServletRequest; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.UUID; public class FileUtil { static SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd/"); public static String upload(MultipartFile uplodFile, HttpServletRequest request){ //文件名称 String oldName=uplodFile.getOriginalFilename(); //文件后缀 String fileSuf=oldName.substring(oldName.lastIndexOf("."),oldName.length()).toLowerCase(); //判断文件类型并选择合适目录 String pathSuf=null; if (Consts.document.contains(fileSuf)) { pathSuf= FileUploadTypeTypeEnum.DOCUMENT.getName(); } if (pathSuf==null&&Consts.picture.contains(fileSuf)) { pathSuf= FileUploadTypeTypeEnum.PICTURE.getName(); } if (pathSuf==null&&Consts.video.contains(fileSuf)) { pathSuf= FileUploadTypeTypeEnum.VIDEO.getName(); } if (pathSuf==null&&Consts.audio.contains(fileSuf)) { pathSuf= FileUploadTypeTypeEnum.AUDIO.getName(); } if (pathSuf==null&&Consts.compress.contains(fileSuf)) { pathSuf= FileUploadTypeTypeEnum.COMPRESS.getName(); } if (pathSuf==null) { throw new ServiceException("该文件类型暂时不支持"); } //创建新名称 String newFileName= UUID.randomUUID().toString()+fileSuf; //创建新文件目录(tomcat)临时目录下的 document或picture……/yyyy/MM/dd/ 可以自定义目录配置nginx进行回显 String realPath=request.getSession().getServletContext().getRealPath(pathSuf)+sdf.format(new Date()); //如果文件目录不存在 新创建 File folder=new File(realPath); if (!folder.isDirectory()) { folder.mkdirs(); } try { //复制文件到目标文件和名字 uplodFile.transferTo(new File(folder,newFileName)); //返回可访问地址 String resStr=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+pathSuf+sdf.format(new Date())+newFileName; return resStr; } catch (IOException e) { e.printStackTrace(); } return "上传失败"; } }
五、接口层
package com.xxx.common; import com.xxx.utils.FileUtil; import com.xxxx.AjaxResult; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; /** * 公共模块-文件上传 * * @author xxx */ @RestController @RequestMapping("/api/admin/common/file") @Api(tags = "公共模块-文件上传") public class CommonFileUploadController extends BaseController { @PostMapping("/upload") @ApiOperation("文件上传") public AjaxResult upload(MultipartFile file, HttpServletRequest request) { return toAjax(FileUtil.upload(file,request)); } }
六、测试
七、涉及其他类
返回类
package com.xxxx.utils; import java.util.HashMap; /** * 操作消息提醒 * * @author lets */ public class AjaxResult extends HashMap<String, Object> { private static final long serialVersionUID = 1L; /** * 状态码 */ public static final String CODE_TAG = "code"; /** * 返回内容 */ public static final String MSG_TAG = "msg"; /** * 数据对象 */ public static final String DATA_TAG = "data"; /** * 初始化一个新创建的 AjaxResult 对象,使其表示一个空消息。 */ public AjaxResult() { } /** * 初始化一个新创建的 AjaxResult 对象 * * @param code 状态码 * @param msg 返回内容 */ public AjaxResult(int code, String msg) { super.put(CODE_TAG, code); super.put(MSG_TAG, msg); } /** * 初始化一个新创建的 AjaxResult 对象 * * @param code 状态码 * @param msg 返回内容 * @param data 数据对象 */ public AjaxResult(int code, String msg, Object data) { super.put(CODE_TAG, code); super.put(MSG_TAG, msg); if (data != null) { super.put(DATA_TAG, data); } } /** * 返回成功消息 * * @return 成功消息 */ public static AjaxResult success() { return AjaxResult.success("操作成功"); } /** * 返回成功数据 * * @return 成功消息 */ public static AjaxResult success(Object data) { return AjaxResult.success("操作成功", data); } /** * 返回成功消息 * * @param msg 返回内容 * @return 成功消息 */ public static AjaxResult success(String msg) { return AjaxResult.success(msg, null); } /** * 返回成功消息 * * @param msg 返回内容 * @param data 数据对象 * @return 成功消息 */ public static AjaxResult success(String msg, Object data) { return new AjaxResult(200, msg, data); } /** * 返回错误消息 * * @return */ public static AjaxResult error() { return AjaxResult.error("操作失败"); } /** * 返回错误消息 * * @param msg 返回内容 * @return 警告消息 */ public static AjaxResult error(String msg) { return AjaxResult.error(msg, null); } /** * 返回错误消息 * * @param msg 返回内容 * @param data 数据对象 * @return 警告消息 */ public static AjaxResult error(String msg, Object data) { return new AjaxResult(900, msg, data); } /** * 返回错误消息 * * @param code 状态码 * @param msg 返回内容 * @return 警告消息 */ public static AjaxResult error(int code, String msg) { return new AjaxResult(code, msg, null); } }
业务异常
package com.xxxx.exception; import com.xxxxx.MsgCode; /** * 业务逻辑异常 * * @author DELL */ public class ServiceException extends RuntimeException { /** * */ private static final long serialVersionUID = 5909435651426033878L; private String code; public ServiceException(String message) { super(message); this.code = MsgCode.CODE_FAIL; } public ServiceException(String code, String message) { super(message); this.code = code; } public ServiceException(Throwable cause) { super(cause); } public ServiceException(String message, Throwable cause) { super(message, cause); } public String getCode() { return code; } public void setCode(String code) { this.code = code; } }