diff --git a/flx-ai/src/main/java/com/pjilisense/flxai/controller/DigitalImgController.java b/flx-ai/src/main/java/com/pjilisense/flxai/controller/DigitalImgController.java index 3dd3a8f..427f2d6 100644 --- a/flx-ai/src/main/java/com/pjilisense/flxai/controller/DigitalImgController.java +++ b/flx-ai/src/main/java/com/pjilisense/flxai/controller/DigitalImgController.java @@ -2,6 +2,7 @@ package com.pjilisense.flxai.controller; import com.pjilisense.flxai.base.constant.Constant; import com.pjilisense.flxai.page.PageData; +import com.pjilisense.flxai.utils.FileUtil; import com.pjilisense.flxai.utils.Result; import com.pjilisense.flxai.dto.DigitalImgDTO; import com.pjilisense.flxai.service.DigitalImgService; @@ -10,12 +11,18 @@ import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; import springfox.documentation.annotations.ApiIgnore; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.List; import java.util.Map; @@ -36,6 +43,31 @@ public class DigitalImgController { @Autowired private DigitalImgService digitalImgService; + @Autowired + FileUtil fileUtil; + @PostMapping("/uploadPicture") + public Result uploadPicture(@RequestParam("file") MultipartFile file) { + if (file.isEmpty()) { + return new Result().error("文件为空"); + } + try { + String relfilename =fileUtil.uploadFile(file); + return new Result().ok(relfilename); + } catch (RuntimeException e) { + return new Result().error("上传失败: " + e.getMessage()); + } + } + @PostMapping("/getPicture") + public void getPicture(HttpServletResponse response,@RequestParam(name = "id", required = true) String id) { + try { + DigitalImgDTO data = digitalImgService.get(id); + if(StringUtils.hasText(data.getImgPath())) { + fileUtil.downLoadFile(data.getImgPath(), null, response); + } + } catch (RuntimeException e) { + } + } + @GetMapping("page") @ApiOperation("分页") @ApiImplicitParams({ @@ -52,7 +84,7 @@ public class DigitalImgController { @GetMapping("{id}") @ApiOperation("信息") - public Result get(@PathVariable("id") Long id){ + public Result get(@PathVariable("id") String id){ DigitalImgDTO data = digitalImgService.get(id); return new Result().ok(data); diff --git a/flx-ai/src/main/java/com/pjilisense/flxai/dto/MyDigitalHumanDTO.java b/flx-ai/src/main/java/com/pjilisense/flxai/dto/MyDigitalHumanDTO.java index d08a12c..7a27ab4 100644 --- a/flx-ai/src/main/java/com/pjilisense/flxai/dto/MyDigitalHumanDTO.java +++ b/flx-ai/src/main/java/com/pjilisense/flxai/dto/MyDigitalHumanDTO.java @@ -12,7 +12,7 @@ import java.util.Date; * ${comments} * * @author liushujing liushujing@philisense.com - * @since 1.0.0 2025-02-26 + * @since 1.0.0 2025-02-27 */ @Data @ApiModel(value = "${comments}") @@ -37,20 +37,8 @@ public class MyDigitalHumanDTO implements Serializable { @ApiModelProperty(value = "选择的声音ID") private String voiceid; - @ApiModelProperty(value = "声音的语言") - private String voicelanguage; - - @ApiModelProperty(value = "声音风格ID") - private String voicestyleid; - - @ApiModelProperty(value = "语速") - private String speedspeech; - - @ApiModelProperty(value = "语调") - private String intonation; - - @ApiModelProperty(value = "音量") - private String volume; + @ApiModelProperty(value = "编辑后的图片") + private String videoimg; @ApiModelProperty(value = "视频存放目录") private String videodir; diff --git a/flx-ai/src/main/java/com/pjilisense/flxai/entity/MyDigitalHumanEntity.java b/flx-ai/src/main/java/com/pjilisense/flxai/entity/MyDigitalHumanEntity.java index ba0aefe..3b33153 100644 --- a/flx-ai/src/main/java/com/pjilisense/flxai/entity/MyDigitalHumanEntity.java +++ b/flx-ai/src/main/java/com/pjilisense/flxai/entity/MyDigitalHumanEntity.java @@ -9,7 +9,7 @@ import java.util.Date; * ${comments} * * @author liushujing liushujing@philisense.com - * @since 1.0.0 2025-02-26 + * @since 1.0.0 2025-02-27 */ @Data @TableName("my_digital_human") @@ -40,25 +40,9 @@ public class MyDigitalHumanEntity { */ private String voiceid; /** - * 声音的语言 + * 编辑后的图片 */ - private String voicelanguage; - /** - * 声音风格ID - */ - private String voicestyleid; - /** - * 语速 - */ - private String speedspeech; - /** - * 语调 - */ - private String intonation; - /** - * 音量 - */ - private String volume; + private String videoimg; /** * 视频存放目录 */ diff --git a/flx-ai/src/main/java/com/pjilisense/flxai/utils/FileUtil.java b/flx-ai/src/main/java/com/pjilisense/flxai/utils/FileUtil.java new file mode 100644 index 0000000..da2d5fc --- /dev/null +++ b/flx-ai/src/main/java/com/pjilisense/flxai/utils/FileUtil.java @@ -0,0 +1,146 @@ +package com.pjilisense.flxai.utils; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Repository; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.multipart.MultipartHttpServletRequest; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.net.URLEncoder; +import java.text.SimpleDateFormat; +import java.util.*; + +@Repository +public class FileUtil { + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + @Value("${file.common.uploadWindow}") + private String diskPath; + @Value("${file.common.uploadLinux}") + private String uploadLinux; + + @Value("${file.common.uploadUrl}") + private String uploadUrl; + + public String uploadFile(MultipartFile multipartFile){ + String relPath="uploads/"; + SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM"); + relPath = relPath+sdf.format(new Date()).substring(0,6)+"/"; + //⽂件的完整名称,如spring.jpeg + String filename = multipartFile.getOriginalFilename(); + //⽂件后缀,如.jpeg + assert filename != null; + String suffix = filename.substring(filename.lastIndexOf(".")); + relPath =relPath+"/"+UUID.randomUUID().toString().replace("-","")+"."+suffix; + //⽬标⽂件 + File descFile = new File(getFilepath(relPath)); + //判断⽬标⽂件所在的⽬录是否存在 + if (!descFile.getParentFile().exists()) { + descFile.getParentFile().mkdirs(); + } + try (InputStream is = multipartFile.getInputStream(); + BufferedInputStream bis = new BufferedInputStream(is); + BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(descFile))) { + int num = 0; + while ((num = bis.read()) != -1) { + bos.write(num); + } + } catch (Exception e) { + //log.error(e.getMessage()); + throw new RuntimeException("文件上传错误,请联系管理员"); + } + return relPath; + } + + /** + * + * @return int 删除结果 1 已删除 0 未删除 + */ + public int delFile(String filepath, String fileName) { + String rootPath = getFilepath (filepath); + File file = new File(rootPath); + if (file.exists()) { + if(file.isDirectory()) { + File filex = new File(rootPath+"/"+fileName); + if(filex.exists()) { + filex.delete(); + } + return 1; + } else { + file.delete(); + return 1; + } + } + return 0; + } + + public void downLoadFile(String filepath, String filename, HttpServletResponse response){ + String rootPath = getFilepath (filepath); + File file = new File(rootPath); + if(file.exists()){ + InputStream inputStream = null; + OutputStream outputStream = null; + try { + // 读到流中 + if(file.isDirectory()) { + inputStream = new FileInputStream(filepath +"/"+ filename);// 文件的存放路径 + } else { + inputStream = new FileInputStream(filepath);//filepath可能包含文件名 + } + response.reset(); + response.setContentType("application/octet-stream"); + response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8")); + outputStream = response.getOutputStream(); + byte[] b = new byte[1024]; + int len; + //从输入流中读取一定数量的字节,并将其存储在缓冲区字节数组中,读到末尾返回-1 + while ((len = inputStream.read(b)) > 0) { + outputStream.write(b, 0, len); + } + } catch (IOException e) { + //log.error(e.getMessage(), e); + }finally { + try { + inputStream.close(); + inputStream.close(); + } catch (IOException e) { + //log.error(e.getMessage(), e); + } + } + } + } + + public String getFilepath (final String filepath) {//filepath可能包含文件名 + String rootPath = getRootpath(); + rootPath = rootPath +"/"+ filepath; + rootPath =rootPath.replace("//","/"); + if(rootPath.endsWith("/")){ + rootPath=rootPath.substring(0,rootPath.length()-1); + } + return rootPath; + } + + public String getRootpath () {//filepath可能包含文件名 + String rootPath = null; + if (System.getProperty("os.name").startsWith("Windows")) { + rootPath = diskPath; + } else if (System.getProperty("os.name").startsWith("Linux")) { + rootPath = uploadLinux; + } + rootPath =rootPath.replace("//","/"); + if(rootPath.endsWith("/")){ + rootPath=rootPath.substring(0,rootPath.length()-1); + } + return rootPath; + } + + +} diff --git a/flx-ai/src/main/resources/application.yml b/flx-ai/src/main/resources/application.yml index 26f77d5..b263511 100644 --- a/flx-ai/src/main/resources/application.yml +++ b/flx-ai/src/main/resources/application.yml @@ -60,4 +60,11 @@ mybatis-plus: configuration-properties: prefix: blobType: BLOB - boolValue: TRUE \ No newline at end of file + boolValue: TRUE +file: + common: + ## windows系统下访问路径 + uploadWindow: d:/ceshi/sysfile + ## Linux系统下访问路径 + uploadLinux: /user/img/ + uploadUrl: /upload/