• nbcio-boot移植到若依ruoyi-nbcio平台里一formdesigner部分(三)


          因为这个版本的若依plus不支持本地文件上传,所以需要增加这些本地上传文件的后端代码

    和前端代码修改。

      1、后端部分

      先配置跳过测试吧,平时编译也不需要这个

    1. org.apache.maven.plugins
    2. maven-surefire-plugin
    3. 2.22.2
    4. true

    增加一个公共上传接口

    1. package com.ruoyi.web.controller.common;
    2. import java.util.ArrayList;
    3. import java.util.List;
    4. import java.util.Map;
    5. import java.util.HashMap;
    6. import javax.servlet.http.HttpServletRequest;
    7. import javax.servlet.http.HttpServletResponse;
    8. import org.slf4j.Logger;
    9. import org.slf4j.LoggerFactory;
    10. import org.springframework.beans.factory.annotation.Autowired;
    11. import org.springframework.http.MediaType;
    12. import org.springframework.web.bind.annotation.GetMapping;
    13. import org.springframework.web.bind.annotation.PostMapping;
    14. import org.springframework.web.bind.annotation.RequestMapping;
    15. import org.springframework.web.bind.annotation.ResponseBody;
    16. import org.springframework.web.bind.annotation.RestController;
    17. import org.springframework.web.multipart.MultipartFile;
    18. import com.ruoyi.common.config.RuoYiConfig;
    19. import com.ruoyi.common.constant.Constants;
    20. import com.ruoyi.common.core.domain.R;
    21. import com.ruoyi.common.utils.StringUtils;
    22. import com.ruoyi.common.utils.file.FileUploadUtils;
    23. import com.ruoyi.common.utils.file.FileUtils;
    24. import com.ruoyi.framework.config.ServerConfig;
    25. /**
    26. * 通用请求处理
    27. *
    28. * @author ruoyi
    29. */
    30. @RestController
    31. @RequestMapping("/common")
    32. public class CommonController
    33. {
    34. private static final Logger log = LoggerFactory.getLogger(CommonController.class);
    35. @Autowired
    36. private ServerConfig serverConfig;
    37. private static final String FILE_DELIMETER = ",";
    38. /**
    39. * 通用下载请求
    40. *
    41. * @param fileName 文件名称
    42. * @param delete 是否删除
    43. */
    44. @GetMapping("/download")
    45. public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request)
    46. {
    47. try
    48. {
    49. if (!FileUtils.checkAllowDownload(fileName))
    50. {
    51. throw new Exception(StringUtils.format("文件名称({})非法,不允许下载。 ", fileName));
    52. }
    53. String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
    54. String filePath = RuoYiConfig.getDownloadPath() + fileName;
    55. response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
    56. FileUtils.setAttachmentResponseHeader(response, realFileName);
    57. FileUtils.writeBytes(filePath, response.getOutputStream());
    58. if (delete)
    59. {
    60. FileUtils.deleteFile(filePath);
    61. }
    62. }
    63. catch (Exception e)
    64. {
    65. log.error("下载文件失败", e);
    66. }
    67. }
    68. /**
    69. * 通用上传请求(单个)
    70. */
    71. @PostMapping("/upload")
    72. @ResponseBody
    73. public R> uploadFile(MultipartFile file) throws Exception
    74. {
    75. try
    76. {
    77. // 上传文件路径
    78. String filePath = RuoYiConfig.getUploadPath();
    79. // 上传并返回新文件名称
    80. String fileName = FileUploadUtils.upload(filePath, file);
    81. String url = serverConfig.getUrl() + fileName;
    82. Map map = new HashMap<>(2);
    83. map.put("url", url);
    84. map.put("fileName", fileName);
    85. map.put("newFileName", FileUtils.getName(fileName));
    86. map.put("originalFilename", file.getOriginalFilename());
    87. return R.ok(map);
    88. }
    89. catch (Exception e)
    90. {
    91. return R.fail(e.getMessage());
    92. }
    93. }
    94. /**
    95. * 通用上传请求(多个)
    96. */
    97. @PostMapping("/uploads")
    98. @ResponseBody
    99. public R> uploadFiles(List files) throws Exception
    100. {
    101. try
    102. {
    103. // 上传文件路径
    104. String filePath = RuoYiConfig.getUploadPath();
    105. List urls = new ArrayList();
    106. List fileNames = new ArrayList();
    107. List newFileNames = new ArrayList();
    108. List originalFilenames = new ArrayList();
    109. for (MultipartFile file : files)
    110. {
    111. // 上传并返回新文件名称
    112. String fileName = FileUploadUtils.upload(filePath, file);
    113. String url = serverConfig.getUrl() + fileName;
    114. urls.add(url);
    115. fileNames.add(fileName);
    116. newFileNames.add(FileUtils.getName(fileName));
    117. originalFilenames.add(file.getOriginalFilename());
    118. }
    119. Map map = new HashMap<>(2);
    120. map.put("urls", StringUtils.join(urls, FILE_DELIMETER));
    121. map.put("fileNames", StringUtils.join(fileNames, FILE_DELIMETER));
    122. map.put("newFileNames", StringUtils.join(newFileNames, FILE_DELIMETER));
    123. map.put("originalFilenames", StringUtils.join(originalFilenames, FILE_DELIMETER));
    124. return R.ok(map);
    125. }
    126. catch (Exception e)
    127. {
    128. return R.fail(e.getMessage());
    129. }
    130. }
    131. /**
    132. * 本地资源通用下载
    133. */
    134. @GetMapping("/download/resource")
    135. public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response)
    136. throws Exception
    137. {
    138. try
    139. {
    140. if (!FileUtils.checkAllowDownload(resource))
    141. {
    142. throw new Exception(StringUtils.format("资源文件({})非法,不允许下载。 ", resource));
    143. }
    144. // 本地资源路径
    145. String localPath = RuoYiConfig.getProfile();
    146. // 数据库资源地址
    147. String downloadPath = localPath + StringUtils.substringAfter(resource, Constants.RESOURCE_PREFIX);
    148. // 下载名称
    149. String downloadName = StringUtils.substringAfterLast(downloadPath, "/");
    150. response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
    151. FileUtils.setAttachmentResponseHeader(response, downloadName);
    152. FileUtils.writeBytes(downloadPath, response.getOutputStream());
    153. }
    154. catch (Exception e)
    155. {
    156. log.error("下载文件失败", e);
    157. }
    158. }
    159. }

    增加上传的全局参数

    1. # 本地:local\Minio:minio\阿里云:alioss
    2. uploadtype: local
    3. #文件上传根目录 设置
    4. profile: /home/nbcio

    增加文件工具类

    1. package com.ruoyi.common.utils.file;
    2. import java.io.File;
    3. import java.io.FileInputStream;
    4. import java.io.FileNotFoundException;
    5. import java.io.FileOutputStream;
    6. import java.io.IOException;
    7. import java.io.OutputStream;
    8. import java.io.UnsupportedEncodingException;
    9. import java.net.URLEncoder;
    10. import java.nio.charset.StandardCharsets;
    11. import javax.servlet.http.HttpServletRequest;
    12. import javax.servlet.http.HttpServletResponse;
    13. import org.apache.commons.io.IOUtils;
    14. import org.apache.commons.lang3.ArrayUtils;
    15. import com.ruoyi.common.config.RuoYiConfig;
    16. import com.ruoyi.common.utils.DateUtils;
    17. import com.ruoyi.common.utils.StringUtils;
    18. import com.ruoyi.common.utils.uuid.IdUtils;
    19. import org.apache.commons.io.FilenameUtils;
    20. /**
    21. * 文件处理工具类
    22. *
    23. * @author ruoyi
    24. */
    25. public class FileUtils
    26. {
    27. public static String FILENAME_PATTERN = "[a-zA-Z0-9_\\-\\|\\.\\u4e00-\\u9fa5]+";
    28. /**
    29. * 输出指定文件的byte数组
    30. *
    31. * @param filePath 文件路径
    32. * @param os 输出流
    33. * @return
    34. */
    35. public static void writeBytes(String filePath, OutputStream os) throws IOException
    36. {
    37. FileInputStream fis = null;
    38. try
    39. {
    40. File file = new File(filePath);
    41. if (!file.exists())
    42. {
    43. throw new FileNotFoundException(filePath);
    44. }
    45. fis = new FileInputStream(file);
    46. byte[] b = new byte[1024];
    47. int length;
    48. while ((length = fis.read(b)) > 0)
    49. {
    50. os.write(b, 0, length);
    51. }
    52. }
    53. catch (IOException e)
    54. {
    55. throw e;
    56. }
    57. finally
    58. {
    59. IOUtils.close(os);
    60. IOUtils.close(fis);
    61. }
    62. }
    63. /**
    64. * 写数据到文件中
    65. *
    66. * @param data 数据
    67. * @return 目标文件
    68. * @throws IOException IO异常
    69. */
    70. public static String writeImportBytes(byte[] data) throws IOException
    71. {
    72. return writeBytes(data, RuoYiConfig.getImportPath());
    73. }
    74. /**
    75. * 写数据到文件中
    76. *
    77. * @param data 数据
    78. * @param uploadDir 目标文件
    79. * @return 目标文件
    80. * @throws IOException IO异常
    81. */
    82. public static String writeBytes(byte[] data, String uploadDir) throws IOException
    83. {
    84. FileOutputStream fos = null;
    85. String pathName = "";
    86. try
    87. {
    88. String extension = getFileExtendName(data);
    89. pathName = DateUtils.datePath() + "/" + IdUtils.fastUUID() + "." + extension;
    90. File file = FileUploadUtils.getAbsoluteFile(uploadDir, pathName);
    91. fos = new FileOutputStream(file);
    92. fos.write(data);
    93. }
    94. finally
    95. {
    96. IOUtils.close(fos);
    97. }
    98. return FileUploadUtils.getPathFileName(uploadDir, pathName);
    99. }
    100. /**
    101. * 删除文件
    102. *
    103. * @param filePath 文件
    104. * @return
    105. */
    106. public static boolean deleteFile(String filePath)
    107. {
    108. boolean flag = false;
    109. File file = new File(filePath);
    110. // 路径为文件且不为空则进行删除
    111. if (file.isFile() && file.exists())
    112. {
    113. flag = file.delete();
    114. }
    115. return flag;
    116. }
    117. /**
    118. * 文件名称验证
    119. *
    120. * @param filename 文件名称
    121. * @return true 正常 false 非法
    122. */
    123. public static boolean isValidFilename(String filename)
    124. {
    125. return filename.matches(FILENAME_PATTERN);
    126. }
    127. /**
    128. * 检查文件是否可下载
    129. *
    130. * @param resource 需要下载的文件
    131. * @return true 正常 false 非法
    132. */
    133. public static boolean checkAllowDownload(String resource)
    134. {
    135. // 禁止目录上跳级别
    136. if (StringUtils.contains(resource, ".."))
    137. {
    138. return false;
    139. }
    140. // 检查允许下载的文件规则
    141. if (ArrayUtils.contains(MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION, FileTypeUtils.getFileType(resource)))
    142. {
    143. return true;
    144. }
    145. // 不在允许下载的文件规则
    146. return false;
    147. }
    148. /**
    149. * 下载文件名重新编码
    150. *
    151. * @param request 请求对象
    152. * @param fileName 文件名
    153. * @return 编码后的文件名
    154. */
    155. public static String setFileDownloadHeader(HttpServletRequest request, String fileName) throws UnsupportedEncodingException
    156. {
    157. final String agent = request.getHeader("USER-AGENT");
    158. String filename = fileName;
    159. if (agent.contains("MSIE"))
    160. {
    161. // IE浏览器
    162. filename = URLEncoder.encode(filename, "utf-8");
    163. filename = filename.replace("+", " ");
    164. }
    165. else if (agent.contains("Firefox"))
    166. {
    167. // 火狐浏览器
    168. filename = new String(fileName.getBytes(), "ISO8859-1");
    169. }
    170. else if (agent.contains("Chrome"))
    171. {
    172. // google浏览器
    173. filename = URLEncoder.encode(filename, "utf-8");
    174. }
    175. else
    176. {
    177. // 其它浏览器
    178. filename = URLEncoder.encode(filename, "utf-8");
    179. }
    180. return filename;
    181. }
    182. /**
    183. * 下载文件名重新编码
    184. *
    185. * @param response 响应对象
    186. * @param realFileName 真实文件名
    187. */
    188. public static void setAttachmentResponseHeader(HttpServletResponse response, String realFileName) throws UnsupportedEncodingException
    189. {
    190. String percentEncodedFileName = percentEncode(realFileName);
    191. StringBuilder contentDispositionValue = new StringBuilder();
    192. contentDispositionValue.append("attachment; filename=")
    193. .append(percentEncodedFileName)
    194. .append(";")
    195. .append("filename*=")
    196. .append("utf-8''")
    197. .append(percentEncodedFileName);
    198. response.addHeader("Access-Control-Expose-Headers", "Content-Disposition,download-filename");
    199. response.setHeader("Content-disposition", contentDispositionValue.toString());
    200. response.setHeader("download-filename", percentEncodedFileName);
    201. }
    202. /**
    203. * 百分号编码工具方法
    204. *
    205. * @param s 需要百分号编码的字符串
    206. * @return 百分号编码后的字符串
    207. */
    208. public static String percentEncode(String s) throws UnsupportedEncodingException
    209. {
    210. String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString());
    211. return encode.replaceAll("\\+", "%20");
    212. }
    213. /**
    214. * 获取图像后缀
    215. *
    216. * @param photoByte 图像数据
    217. * @return 后缀名
    218. */
    219. public static String getFileExtendName(byte[] photoByte)
    220. {
    221. String strFileExtendName = "jpg";
    222. if ((photoByte[0] == 71) && (photoByte[1] == 73) && (photoByte[2] == 70) && (photoByte[3] == 56)
    223. && ((photoByte[4] == 55) || (photoByte[4] == 57)) && (photoByte[5] == 97))
    224. {
    225. strFileExtendName = "gif";
    226. }
    227. else if ((photoByte[6] == 74) && (photoByte[7] == 70) && (photoByte[8] == 73) && (photoByte[9] == 70))
    228. {
    229. strFileExtendName = "jpg";
    230. }
    231. else if ((photoByte[0] == 66) && (photoByte[1] == 77))
    232. {
    233. strFileExtendName = "bmp";
    234. }
    235. else if ((photoByte[1] == 80) && (photoByte[2] == 78) && (photoByte[3] == 71))
    236. {
    237. strFileExtendName = "png";
    238. }
    239. return strFileExtendName;
    240. }
    241. /**
    242. * 获取文件名称 /profile/upload/2022/04/16/ruoyi.png -- ruoyi.png
    243. *
    244. * @param fileName 路径名称
    245. * @return 没有文件路径的名称
    246. */
    247. public static String getName(String fileName)
    248. {
    249. if (fileName == null)
    250. {
    251. return null;
    252. }
    253. int lastUnixPos = fileName.lastIndexOf('/');
    254. int lastWindowsPos = fileName.lastIndexOf('\\');
    255. int index = Math.max(lastUnixPos, lastWindowsPos);
    256. return fileName.substring(index + 1);
    257. }
    258. /**
    259. * 获取不带后缀文件名称 /profile/upload/2022/04/16/ruoyi.png -- ruoyi
    260. *
    261. * @param fileName 路径名称
    262. * @return 没有文件路径和后缀的名称
    263. */
    264. public static String getNameNotSuffix(String fileName)
    265. {
    266. if (fileName == null)
    267. {
    268. return null;
    269. }
    270. String baseName = FilenameUtils.getBaseName(fileName);
    271. return baseName;
    272. }
    273. }

    2、前端方面

    previewRender.js修改如下:

    1. import { isAttr,jsonClone } from '../utils';
    2. import childrenItem from './slot/index';
    3. import {remoteData} from './mixin';
    4. import { getToken } from "@/utils/auth";
    5. //先修改在这里,后续需要优化
    6. function vModel(self, dataObject) {
    7. dataObject.props.value = self.value;
    8. dataObject.on.input = val => {
    9. self.$emit('input', val)
    10. }
    11. //判断是否为上传组件
    12. if(self.conf.compType === 'upload'){
    13. //for token add by nbacheng 2022-09-07
    14. //dataObject.attrs['headers'] = {"Authorization":"Bearer " + getToken()};
    15. /**
    16. * 此处增加自定义的token,如果不能满足要求,可以重写此处代码
    17. */
    18. const token = getToken();
    19. dataObject.attrs['headers'] = {"Authorization":"Bearer " + token};
    20. console.log("dataObject.props.value",dataObject.props.value)
    21. if(dataObject.props.value!==undefined && dataObject.props.value !==''){
    22. const filevalue = JSON.parse(dataObject.props.value);
    23. dataObject.props['file-list'] = filevalue;
    24. }
    25. dataObject.attrs['before-upload'] = file=>{
    26. //非限定后缀不允许上传
    27. console.log("before-upload file",file);
    28. const fileName = file.name;
    29. console.log("before-upload fileName",fileName);
    30. const suffixName = fileName.split('.').pop();
    31. if(!self.conf.accept.includes(suffixName)){
    32. self.$message.error('该后缀文件不允许上传');
    33. return false;
    34. }
    35. const fileSize = file.size;
    36. if(fileSize>dataObject.props.fileSize*1024*1024){
    37. self.$message.error('文件大小超出限制,请检查!');
    38. return false;
    39. }
    40. }
    41. //for get return file url add by nbacheng 2022-09-07
    42. dataObject.attrs['on-success'] = file=>{
    43. console.log("on-success file",file);
    44. var filename=file.data.fileName.substring(file.data.fileName.lastIndexOf('/')+1) //获取文件名称
    45. let fileObj = {name: filename, url: file.data.fileName}
    46. console.log("dataObject=",dataObject);
    47. console.log("self.conf=",self.conf);
    48. let oldValue = [];
    49. if(dataObject.props.value) {
    50. oldValue = JSON.parse(dataObject.props.value);
    51. }else {
    52. oldValue = [];
    53. }
    54. if (oldValue) {
    55. oldValue.push(fileObj)
    56. } else {
    57. oldValue = [fileObj]
    58. }
    59. self.$emit('input',JSON.stringify(oldValue));
    60. console.log("on-success value",oldValue);
    61. }
    62. dataObject.attrs['on-remove'] = (file, fileList) => {
    63. console.log("on-remove file,fileList",file,fileList);
    64. let oldValue = JSON.parse(dataObject.props.value);
    65. console.log("on-remove oldValue",oldValue);
    66. //file 删除的文件
    67. //过滤掉删除的文件
    68. let newValue = oldValue.filter(item => item.name !== file.name)
    69. self.$emit('input',JSON.stringify(newValue));
    70. console.log("on-remove newValue",newValue);
    71. }
    72. dataObject.attrs['on-error'] = (file) => {
    73. console.log("on-error file",file);
    74. }
    75. dataObject.attrs['on-preview'] = (file) => {
    76. console.log("on-preview file",file);
    77. //download(file);
    78. }
    79. //for get return file url add by nbacheng 2022-09-07
    80. }
    81. }
    82. export default {
    83. render(h) {
    84. let dataObject = {
    85. attrs: {},
    86. props: {},
    87. on: {},
    88. style: {}
    89. }
    90. //远程获取数据
    91. this.getRemoteData();
    92. const confClone = jsonClone(this.conf);
    93. const children = childrenItem(h,confClone);
    94. Object.keys(confClone).forEach(key => {
    95. const val = confClone[key]
    96. if (dataObject[key]) {
    97. dataObject[key] = val
    98. } else if(key ==='width'){
    99. dataObject.style= 'width:'+val;
    100. } else if (!isAttr(key)) {
    101. dataObject.props[key] = val
    102. }else {
    103. if (key == 'classStyle' && val.length > 0){
    104. let style =""
    105. val.forEach(item =>{
    106. console.log(item)
    107. style+=item +" "
    108. })
    109. dataObject.attrs['class'] = style
    110. }else if (key == 'cssStyle'){
    111. dataObject.attrs['style'] = val
    112. }else if(key !== 'value'){
    113. dataObject.attrs[key] = val
    114. }
    115. }
    116. })
    117. /*调整赋值模式,规避cascader组件赋值props会出现覆盖预制参数的bug */
    118. vModel(this, dataObject);
    119. return h(confClone.ele, dataObject, children)
    120. },
    121. props: ['conf','value'],
    122. mixins:[remoteData]
    123. }

    3、效果图

  • 相关阅读:
    足球资讯查询易语言代码
    《Unix 网络编程》11:名字和地址转换
    自动驾驶的关键在于安全、智能与舒适
    ping与Traceroute是如何工作的
    数据可视化:四种关系图数据可视化的效果对比!
    c++ Primer 第九章:顺序容器 练习答案记录
    华为数通方向HCIP-DataCom H12-821题库(多选题:41-60)
    游戏动画技术简介
    云扩RPA携手中联教育引领财务机器人教学创新
    java计算机毕业设计学生健康信息管理源程序+mysql+系统+lw文档+远程调试
  • 原文地址:https://blog.csdn.net/qq_40032778/article/details/132870859