图片上传,我的数字人形象表

main
liushujing 3 months ago
parent 916954454c
commit 12bcc9f503

@ -2,6 +2,7 @@ package com.pjilisense.flxai.controller;
import com.pjilisense.flxai.base.constant.Constant; import com.pjilisense.flxai.base.constant.Constant;
import com.pjilisense.flxai.page.PageData; import com.pjilisense.flxai.page.PageData;
import com.pjilisense.flxai.utils.FileUtil;
import com.pjilisense.flxai.utils.Result; import com.pjilisense.flxai.utils.Result;
import com.pjilisense.flxai.dto.DigitalImgDTO; import com.pjilisense.flxai.dto.DigitalImgDTO;
import com.pjilisense.flxai.service.DigitalImgService; import com.pjilisense.flxai.service.DigitalImgService;
@ -10,12 +11,18 @@ import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore; import springfox.documentation.annotations.ApiIgnore;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletResponse; 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.List;
import java.util.Map; import java.util.Map;
@ -36,6 +43,31 @@ public class DigitalImgController {
@Autowired @Autowired
private DigitalImgService digitalImgService; private DigitalImgService digitalImgService;
@Autowired
FileUtil fileUtil;
@PostMapping("/uploadPicture")
public Result<String> uploadPicture(@RequestParam("file") MultipartFile file) {
if (file.isEmpty()) {
return new Result<String>().error("文件为空");
}
try {
String relfilename =fileUtil.uploadFile(file);
return new Result<String>().ok(relfilename);
} catch (RuntimeException e) {
return new Result<String>().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") @GetMapping("page")
@ApiOperation("分页") @ApiOperation("分页")
@ApiImplicitParams({ @ApiImplicitParams({
@ -52,7 +84,7 @@ public class DigitalImgController {
@GetMapping("{id}") @GetMapping("{id}")
@ApiOperation("信息") @ApiOperation("信息")
public Result<DigitalImgDTO> get(@PathVariable("id") Long id){ public Result<DigitalImgDTO> get(@PathVariable("id") String id){
DigitalImgDTO data = digitalImgService.get(id); DigitalImgDTO data = digitalImgService.get(id);
return new Result<DigitalImgDTO>().ok(data); return new Result<DigitalImgDTO>().ok(data);

@ -12,7 +12,7 @@ import java.util.Date;
* ${comments} * ${comments}
* *
* @author liushujing liushujing@philisense.com * @author liushujing liushujing@philisense.com
* @since 1.0.0 2025-02-26 * @since 1.0.0 2025-02-27
*/ */
@Data @Data
@ApiModel(value = "${comments}") @ApiModel(value = "${comments}")
@ -37,20 +37,8 @@ public class MyDigitalHumanDTO implements Serializable {
@ApiModelProperty(value = "选择的声音ID") @ApiModelProperty(value = "选择的声音ID")
private String voiceid; private String voiceid;
@ApiModelProperty(value = "声音的语言") @ApiModelProperty(value = "编辑后的图片")
private String voicelanguage; private String videoimg;
@ApiModelProperty(value = "声音风格ID")
private String voicestyleid;
@ApiModelProperty(value = "语速")
private String speedspeech;
@ApiModelProperty(value = "语调")
private String intonation;
@ApiModelProperty(value = "音量")
private String volume;
@ApiModelProperty(value = "视频存放目录") @ApiModelProperty(value = "视频存放目录")
private String videodir; private String videodir;

@ -9,7 +9,7 @@ import java.util.Date;
* ${comments} * ${comments}
* *
* @author liushujing liushujing@philisense.com * @author liushujing liushujing@philisense.com
* @since 1.0.0 2025-02-26 * @since 1.0.0 2025-02-27
*/ */
@Data @Data
@TableName("my_digital_human") @TableName("my_digital_human")
@ -40,25 +40,9 @@ public class MyDigitalHumanEntity {
*/ */
private String voiceid; private String voiceid;
/** /**
* *
*/ */
private String voicelanguage; private String videoimg;
/**
* ID
*/
private String voicestyleid;
/**
*
*/
private String speedspeech;
/**
*
*/
private String intonation;
/**
*
*/
private String volume;
/** /**
* *
*/ */

@ -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;
}
}

@ -60,4 +60,11 @@ mybatis-plus:
configuration-properties: configuration-properties:
prefix: prefix:
blobType: BLOB blobType: BLOB
boolValue: TRUE boolValue: TRUE
file:
common:
## windows系统下访问路径
uploadWindow: d:/ceshi/sysfile
## Linux系统下访问路径
uploadLinux: /user/img/
uploadUrl: /upload/

Loading…
Cancel
Save