前段获取客户签字图片,传给后端,并且存入数据库
前段获取的是,生成的是base64图片,传到后端是base64,需要转化为jpg,png。
原因是。app,有客户签字图片。从前段传递到后端。后端需要获取保存到本地,并且存入数据库。
前端,线上签字,图片,传到后端 base64图片。后端接收转化为jpg
客户签字图片
@AutoLog(value = "ervice_Contract-合同编辑")
@ApiOperation(value="ervice_Contract-合同编辑", notes="ervice_Contract-合同编辑")
@PutMapping(value = "/editServiceContract")
public Result<?> editServiceContract(@RequestBody ServiceContract serviceContract) {
serviceContractService.updateById(serviceContract);
return Result.ok("编辑成功!");
public class FileController {
private final ResourceLoader resourceLoader;
@Autowired
public FileController(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
@Value("${web.upload-path}")
private String path;
@Autowired
private IServiceContractService iServiceContractService;
/**
* 上传图片文件
*
* @param file
* @return
*/
@PostMapping("imgUpload")
@ResponseBody
public String imgUpload(@RequestParam("img") MultipartFile file,
@RequestParam("articleId") String id) {
String response = " ";
if (file.isEmpty()) {
return new ApiResponse(-1, "文件为空!");
}
try {
//1.定义上传的文件
String localPath = path;
//2.获得文件名字
String fileName = file.getOriginalFilename();
//3.上传
//3.1 生成新的文件名
String realPath = localPath + "/" + FileNameUtils.getFileName(fileName);
//3.2 保存文件
File dest = new File(realPath);
//判断文件目目录是否存在,不存在则新建
if (!dest.getParentFile().exists()){
dest.getParentFile().mkdir();
}
file.transferTo(dest);
//保存路径到数据库
ArticlePictureEntity entity = new ArticlePictureEntity(id, realPath);
articlePictureService.save(entity);
response = "上传成功";
} catch (Exception e) {
e.printStackTrace();
response = "服务器出现错误,上传失败";
}
return response;
}
客户签字图片
@Value("${com.yangyouqi}")
//获取主机端口
private String post;
//获取本机ip
private String host;
//图片存放根路径
private String rootPath = "E:";
//图片存放根目录下的子目录
private String sonPath = "/img/";
//获取图片链接
private String signUrl;
private static final Logger logger = LoggerFactory.getLogger(ServiceController.class);
@RequestMapping(value = "upload")
@ResponseBody
public String upload(@RequestParam("test") MultipartFile file) {
//返回上传的文件是否为空,即没有选择任何文件,或者所选文件没有内容。
//防止上传空文件导致奔溃
if (file.isEmpty()) {
return "文件为空";
}
//获取本机IP
try {
host = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
logger.error("get server host Exception e:", e);
}
// 获取文件名
String fileName = file.getOriginalFilename();
//logger.info("上传的文件名为:" + fileName);
// 设置文件上传后的路径
String filePath = rootPath + sonPath;
logger.info("上传的文件路径" + filePath);
logger.info("整个图片路径:" + host + ":" + post + sonPath + fileName);
//创建文件路径
File dest = new File(filePath + fileName);
String imgPath = (host + ":" + post + sonPath + fileName).toString();
// 解决中文问题,liunx下中文路径,图片显示问题
// fileName = UUID.randomUUID() + suffixName;
// 检测是否存在目录
if (!dest.getParentFile().exists()) {
//假如文件不存在即重新创建新的文件已防止异常发生
dest.getParentFile().mkdirs();
}
try {
//transferTo(dest)方法将上传文件写到服务器上指定的文件
file.transferTo(dest);
//将链接保存到URL中
ServiceContract serviceContract = serviceContractService.getServiceContractImages(new ServiceContract(),signUrl);
return "上传成功";
} catch (Exception e) {
return "上传失败";
}
}
}
@AutoLog(value = "image-上传签约图片")
@ApiOperation(value="image-上传签约图片", notes="image-上传签约图片")
@PutMapping(value = "/image")
@RequestMapping(value = "/image", method = RequestMethod.POST)
public ServiceContract submit(MultipartFile file,ServiceContract serviceContract)
throws Exception {
//这里就可以获取里面的上传过来的数据了
//做一些存库操作,以及返回的数据
String filename = file.getOriginalFilename();
System.out.println(filename);
String filePath = System.getProperty("user.dir")+"\\src\\main\\resources\\static\\img";
if (!new File(filePath).exists()){
new File(filePath).mkdirs();
}
File dest = new File(filePath + File.separator + serviceContract.getSignUrl()+"_"+filename);
try {
file.transferTo(dest);
}catch (Exception e){
e.printStackTrace();
}
System.out.println(serviceContract);
serviceContract.setSignUrl(serviceContract.getSignUrl()+"_"+filename);
return serviceContract;
}
}
@AutoLog(value = "image-上传签约图片")
@ApiOperation(value="image-上传签约图片", notes="image-上传签约图片")
@PutMapping(value = "/image")
// @RequestMapping("image")
@ResponseBody
public ServiceContract image(MultipartFile file){
//调用工具类完成文件上传
String imagePath = UploadUtils.upload(file);
System.out.println(imagePath);
ServiceContract serviceContract = new ServiceContract();
if (imagePath != null){
//创建一个HashMap用来存放图片路径
HashMap hashMap = new HashMap();
hashMap.put("src",imagePath);
serviceContract.setId(0);
serviceContract.setSignUrl("上传成功");
serviceContract.setData(hashMap);
}else{
serviceContract.setId(0);
serviceContract.setSignUrl("上传失败");
}
return serviceContract;
}
@RequestMapping("addImage")
@ResponseBody
public String addImage(String imageDescribe,String imagePath){
//获得图片地址和图片描述
/*
这里我们只做打印的操作,实际上,我们应该在这里调用
方法,把图片地址和图片的描述加入到数据库中,但是这些
相信大家已经回了,所以,就不再写了。
*/
System.out.println("图片描述:"+imageDescribe);
System.out.println("图片地址:"+imagePath);
return "1";
}
@RequestMapping("deleteImage")
@ResponseBody
public String deleteImage(){
UploadUtils.delete("84e15dddb4284fc8a4877c93bcc9d81f-dl.jpg");
return "1";
}。