package com.de.debook.bo; import java.util.HashMap; import java.util.Map; public class ResponseBean { private Map<String, Object> responseMap = new HashMap<>(); private static final Integer success = 0; private static final Integer error = -1; private static final String message = "ok"; // 默认应答成功 public ResponseBean() { responseMap.put("status", success); responseMap.put("message", message); } // 出错时消息 public void putError(String message) { responseMap.put("status", error); responseMap.put("message", message); } // 数据 public void putData(String key, Object value) { responseMap.put(key, value); } // 分页数据 public void putPageData(Object data, Object count){ responseMap.put("code", success); responseMap.put("data", data); responseMap.put("count", count); } public Map<String, Object> getResponseMap() { return responseMap; } }
时间: 2024-01-05 15:04:45 浏览: 28
这是一个 ResponseBean 类,用于封装返回给前端的数据。其中包含了成功和失败的状态码、消息以及数据的相关方法。代码中使用了一个 HashMap 来存储返回的数据,可以通过 putData 方法来添加数据,也可以通过 putError 方法来添加出错时的消息。此外,还提供了一个 putPageData 方法,用于返回分页数据。最后,通过 getResponseMap 方法可以获取整个返回的 Map 对象。
相关问题
package com.de.debook.controller; import com.de.debook.bo.ResponseBean; import com.de.debook.constant.WebConstant; import com.de.debook.utils.FileUploadUtils; 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; import java.io.File; import java.io.IOException; import java.util.Map; @RestController public class UploadFileController { private static final int FILE_SIZE_MAX = 20 * 1024 * 1024; // 上传限制大小 /** * @param multipartFile * @description: 通用文件上传处理器 * @return: java.util.Map<java.lang.String , java.lang.Object> */ @RequestMapping(value = "/uploadFile", produces = "application/json;charset=UTF-8") public Map<String, Object> fileUpload(@RequestParam("file") MultipartFile multipartFile) { ResponseBean responseBean = new ResponseBean(); if (multipartFile != null) { String realName = multipartFile.getOriginalFilename(); // 原始文件名 String suffix = FileUploadUtils.fileSuffix(realName); // 文件名后缀 String tmpFileName = FileUploadUtils.createTmpFileName(suffix); // 生成保证不重复的临时文件名 if (multipartFile.getSize() > FILE_SIZE_MAX) { responseBean.putError("上传失败:文件大小不得超过20MB"); return responseBean.getResponseMap(); } File tmpFile = new File(WebConstant.FILE_STORAGE_ROOT,tmpFileName); try { multipartFile.transferTo(tmpFile); // 写入本地 responseBean.putData("data", "/images/" + tmpFileName); } catch (IOException e) { responseBean.putError("上传失败:" + e.getMessage()); e.printStackTrace(); } } return responseBean.getResponseMap(); } }
这是一个基于Spring框架开发的文件上传处理器,它接收一个MultipartFile类型的参数,即上传的文件,然后进行处理,最终返回一个包含上传结果的ResponseBean对象。其中,文件大小限制为20MB,超过限制则返回上传失败的信息;上传成功后,会将文件写入到本地,并将其访问路径存储到ResponseBean中返回。
package com.de.debook.init; import com.de.debook.constant.WebConstant; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; import org.springframework.util.ClassUtils; @Component public class InitConfig implements CommandLineRunner { @Override public void run(String... strings) throws Exception { // 项目路径 WebConstant.WEB_FILE_ROOT = ClassUtils.getDefaultClassLoader() .getResource("").getPath().substring(1); // 图片存储目录 WebConstant.FILE_STORAGE_ROOT = WebConstant.WEB_FILE_ROOT + "static/images/"; } }
这是一个Spring Boot项目的初始化配置类,实现了CommandLineRunner接口,用于在Spring Boot应用程序启动时执行一些初始化操作。
具体来说,该配置类的run方法实现了以下两个操作:
1. 获取项目路径:通过ClassUtils.getDefaultClassLoader().getResource("").getPath()方法获取当前类所在的classpath路径,然后通过substring(1)方法去掉开头的“/”字符,得到项目的根路径。
2. 设置图片存储目录:将WebConstant.WEB_FILE_ROOT和"static/images/"拼接起来,得到图片存储目录的绝对路径,保存到WebConstant.FILE_STORAGE_ROOT中。
这里的WebConstant是一个常量类,用于保存项目中的常量值。通过这个配置类,我们可以方便地获取项目路径和设置图片存储目录,从而方便地进行文件操作。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)