记录layui文件上传方法,前端页面直接看layui文件上传相关文档就行,主要是记录后端Java接收上传流并保存的方法
layui文档:https://www.layui.com/doc/modules/upload.html
因为该方法使用MultipartFile来接收layui上传流,所以需要在SpringMVC中配置multipart
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="1000000000" />
</bean>
用于封装返回值编写的实体类Result(规范接口吐数据格式)
package demo.util; import java.io.Serializable; import java.util.Map; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; /** * * 类名称:Result * 类描述: 结果对象 * @version */ @ApiModel(description = "Result实体类") public class Result<T> implements Serializable { private static final long serialVersionUID = 1L; /** * 编码 */ @ApiModelProperty(value = "编码") private int code; /** * 是否操作成功 */ @ApiModelProperty(value = "是否操作成功") private boolean success; /** * 提示信息 */ @ApiModelProperty(value = "提示信息") private String message; /** * 返回结果 */ @ApiModelProperty(value = "返回结果") private T result; public Result() { this(1, null, null, null); } public Result(int code) { this(code, null, null, null); } public Result(int code, Map<String, String> params) { this(code, params, null, null); } public Result(int code, T result) { this(code, null, null, result); } public Result(int code, Map<String, String> params, T result) { this(code, params, null, result); } public Result(int code, String message, T result) { this(code, null, message, result); } public Result(boolean isSuccess, String message, T result) { this((isSuccess ? 1 : 0), null, message, result); } public Result(int code, Map<String, String> params, String message, T result) { this.code = code; this.success = 1 == code; this.message = message ; this.result = result; } /** * 新建结果对象 * * @param isSuccess * @return */ public static <T> Result<T> newResult(boolean isSuccess) { return new Result<T>(isSuccess, null, null); } /** * 新建结果对象 * * @param code * @param result * @return */ public static <T> Result<T> newResult(int code, T result) { return new Result<T>(code, result); } /** * 新建结果对象 * * @param code * @param params * @return */ public static <T> Result<T> newResult(int code, Map<String, String> params) { return new Result<T>(code, params, null); } /** * 新建结果对象 * * @param code * @param params * @param result * @return */ public static <T> Result<T> newResult(int code, Map<String, String> params, T result) { return new Result<T>(code, params, result); } /** * 新建结果对象 * * @param code * @param message * @param result * @return */ public static <T> Result<T> newResult(int code, String message, T result) { return new Result<T>(code, message, result); } /** * 新建结果对象 * * @param isSuccess * @param message * @param result * @return */ public static <T> Result<T> newResult(boolean isSuccess, String message, T result) { return new Result<T>(isSuccess, message, result); } /** * 新建成功结果对象 * * @return */ public static <T> Result<T> newSuccessResult() { return newResult(true, null, null); } /** * 新建成功结果对象 * * @param result * @return */ public static <T> Result<T> newSuccessResult(T result) { return newResult(true, null, result); } /** * 新建失败结果对象 * * @return */ public static <T> Result<T> newFailureResult(String message) { return new Result<T>(0, null, message, null); } /** * 新建失败结果对象 * * @param code * @return */ public static <T> Result<T> newFailureResult(int code) { return new Result<T>(code, null, null, null); } public void setCode(int code) { this.code = code; } public int getCode() { return code; } public void setSuccess(boolean success) { this.success = success; } public boolean getSuccess() { return success; } public void setMessage(String message) { this.message = message; } public String getMessage() { return message; } public void setResult(T result) { this.result = result; } public T getResult() { return result; } }
Java上传方法
/** * layui图片上传 * @param multipartFile 上传文件file * @param version 传递的参数 * @return */ @RequestMapping("/layupload") @ResponseBody public Result saveUploadPicture(@RequestParam(value="file") MultipartFile multipartFile,String version) { //FIXME:此行改为所在工程的对应的上传物理路径 String uploadAbsolutePath = "D:///file//image"; // 根据绝对路径在本地生成路径所需目录 File file = new File(uploadAbsolutePath); if (!file.exists() && !file.mkdirs()) { // 如果file对象不存在,那么就将该对象的路径名中不存在的文件夹目录建立出来 return Result.newFailureResult("文件上传路径不存在"); } //原文件名 String names = multipartFile.getOriginalFilename(); // 文件扩展名 String fileExt = names.substring(names.lastIndexOf(".") + 1).toLowerCase(); // 文件上传后的新名 String newName = version+ "." + fileExt; // 文件的绝对路径File File uploadFile = new File(uploadAbsolutePath + "/" + newName); try { // 将上传的图片二进制流保存为文件 FileCopyUtils.copy(multipartFile.getInputStream(), new FileOutputStream(uploadFile)); } catch (IOException ioException) { log.info("图片保存到文件夹中出错!"); Result.newFailureResult("文件上传失败"); } catch (Exception e) { log.info("文件没有复制到指定的目录下"); } Map<String, Object> result = new HashMap<>(2); result.put("oldName", names); result.put("newName", newName); //如果不使用封装返回方式,也可以直接返回map对象,可根据自己喜好和业务做修改 return Result.newResult(1,"上传文件成功",result); }