| | |
| | | package com.ruoyi.system.service.impl; |
| | | |
| | | import java.io.*; |
| | | import java.math.BigDecimal; |
| | | import java.util.Collections; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.ArrayList; |
| | | import java.util.stream.Collectors; |
| | | import java.io.File; |
| | | import java.io.FileOutputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.net.HttpURLConnection; |
| | | import java.net.URL; |
| | | |
| | |
| | | public Long uploadAttachment(Long taskId, MultipartFile file, String category) { |
| | | try { |
| | | // 上传文件,返回相对路径(如:/task/2025/01/15/xxx.jpg) |
| | | String fileName = FileUploadUtils.upload("/task", file); |
| | | String fileName = category+"_"+System.currentTimeMillis()+"_"+file.getOriginalFilename(); |
| | | |
| | | fileName=saveLocalPath(fileName,file.getInputStream()); |
| | | |
| | | SysTaskAttachment attachment = new SysTaskAttachment(); |
| | | attachment.setTaskId(taskId); |
| | |
| | | |
| | | } |
| | | |
| | | return result; |
| | | return attachment.getAttachmentId(); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException("文件上传失败:" + e.getMessage()); |
| | | } |
| | |
| | | String fileName = "wx_" + mediaId.substring(0, Math.min(20, mediaId.length())) + "_" + System.currentTimeMillis() + ".jpg"; |
| | | |
| | | // 保存到本地 |
| | | String baseDir = FileUploadUtils.getDefaultBaseDir(); |
| | | String datePath = DateUtils.datePath(); |
| | | String uploadDir = baseDir + "/task/" + datePath; |
| | | |
| | | // 创建目录 |
| | | File uploadPath = new File(uploadDir); |
| | | if (!uploadPath.exists()) { |
| | | uploadPath.mkdirs(); |
| | | } |
| | | |
| | | // 保存文件 |
| | | String filePath = uploadDir + "/" + fileName; |
| | | File file = new File(filePath); |
| | | try (FileOutputStream fos = new FileOutputStream(file)) { |
| | | fos.write(fileBytes); |
| | | } |
| | | |
| | | // 生成相对路径(不包含baseDir) |
| | | String relativeFilePath = "/task/" + datePath + "/" + fileName; |
| | | |
| | | String relativeFilePath = saveLocalPath(fileName, fileBytes); |
| | | |
| | | // 保存附件记录 |
| | | SysTaskAttachment attachment = new SysTaskAttachment(); |
| | | attachment.setTaskId(taskId); |
| | |
| | | throw new RuntimeException("从微信上传文件失败:" + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | |
| | | private static String saveLocalPath(String fileName, byte[] fileBytes) throws IOException { |
| | | String baseDir = FileUploadUtils.getDefaultBaseDir(); |
| | | String datePath = DateUtils.datePath(); |
| | | String uploadDir = baseDir + "/task/" + datePath; |
| | | |
| | | // 创建目录 |
| | | File uploadPath = new File(uploadDir); |
| | | if (!uploadPath.exists()) { |
| | | uploadPath.mkdirs(); |
| | | } |
| | | |
| | | // 保存文件 |
| | | String filePath = uploadDir + "/" + fileName; |
| | | File file = new File(filePath); |
| | | try (FileOutputStream fos = new FileOutputStream(file)) { |
| | | fos.write(fileBytes); |
| | | } |
| | | |
| | | // 生成相对路径(不包含baseDir) |
| | | String relativeFilePath = "/task/" + datePath + "/" + fileName; |
| | | return relativeFilePath; |
| | | } |
| | | |
| | | private String saveLocalPath(String fileName,InputStream stream){ |
| | | String baseDir = FileUploadUtils.getDefaultBaseDir(); |
| | | String datePath = DateUtils.datePath(); |
| | | String uploadDir = baseDir + "/task/" + datePath; |
| | | |
| | | // 创建目录 |
| | | File uploadPath = new File(uploadDir); |
| | | if (!uploadPath.exists()) { |
| | | uploadPath.mkdirs(); |
| | | } |
| | | |
| | | // 保存文件 |
| | | String filePath = uploadDir + "/" + fileName; |
| | | //将inputstream写入文件 |
| | | try (OutputStream os = new FileOutputStream(filePath)) { |
| | | byte[] buffer = new byte[1024]; // 缓冲区,减少 IO 次数 |
| | | int bytesRead; |
| | | // 循环读取输入流中的数据,写入输出流 |
| | | while ((bytesRead = stream.read(buffer)) != -1) { |
| | | os.write(buffer, 0, bytesRead); |
| | | } |
| | | os.flush(); // 强制刷新缓冲区,确保数据写入文件 |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | // 生成相对路径(不包含baseDir) |
| | | String relativeFilePath = "/task/" + datePath + "/" + fileName; |
| | | return relativeFilePath; |
| | | } |
| | | |
| | | /** |
| | | * 从 URL 下载文件 |
| | | */ |