Commit e0c8a682 by 宋珺琪

修改选项图片存到服务器

parent 4b07a7b8
......@@ -92,6 +92,10 @@
<artifactId>commons-net</artifactId>
<version>3.8.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
</dependencies>
<build>
......
......@@ -11,6 +11,8 @@ import com.exam.vo.AnswerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
@RestController
public class AnswerController {
......@@ -36,8 +38,13 @@ public class AnswerController {
}
}
/**
* 修改试题
* @param answerVO
* @return
*/
@PostMapping("/updateAnswers")
public ApiResult updateAnswers(@RequestBody AnswerVO answerVO){
public ApiResult updateAnswers(@RequestBody AnswerVO answerVO) throws IOException {
int res = answerService.updateAnswers(answerVO);
if (res == 0) {
return ApiResultHandler.buildApiResult(400,"修改失败",res);
......@@ -51,6 +58,11 @@ public class AnswerController {
return ApiResultHandler.buildApiResult(200,"查询题库照片",answerService.answersPhoto(questionid,tablename,paperid));
}
/**
* 修改题库图片
* @param paperPhotoParams
* @return
*/
@PostMapping("/updateAnswersPhoto")
public ApiResult updateAnswersPhoto(PaperPhotoParams paperPhotoParams){
int res = answerService.updateAnswersPhoto(paperPhotoParams);
......
......@@ -10,6 +10,8 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
@RestController
public class MultiQuestionController {
......@@ -22,8 +24,14 @@ public class MultiQuestionController {
return ApiResultHandler.buildApiResult(200,"查询成功",res);
}
/**
* 添加题
* @param multiQuestion
* @return
* @throws IOException
*/
@PostMapping("/MultiQuestion")
public ApiResult add(@RequestBody MultiQuestion multiQuestion) {
public ApiResult add(@RequestBody MultiQuestion multiQuestion) throws IOException {
int res = multiQuestionService.add(multiQuestion);
if (res != 0) {
......
......@@ -49,6 +49,11 @@ public class PaperController {
return map;
}
/**
* 添加题干照片
* @param paperManage
* @return
*/
@PostMapping("/paperManage")
public ApiResult add(PaperManage paperManage) {
int res = paperService.add(paperManage);
......
......@@ -3,6 +3,8 @@ package com.exam.entity;
import lombok.Data;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
@Data
public class PaperPhotoParams {
private Integer paperId;
......@@ -11,5 +13,7 @@ public class PaperPhotoParams {
private Integer questionId;
private MultipartFile[] files;// 照片二进制
private MultipartFile[] files;//新增的照片信息放的是raw
private List<String> UrlFiles;//之前就有的照片信息放的是Url
}
\ No newline at end of file
......@@ -16,6 +16,14 @@ public interface PaperPhotoMapper {
@Delete("delete from paper_photo where paperId = #{paperId} and questionType = #{questionType} and questionId = #{questionId}")
int deletePaperPhoto(PaperPhoto paperPhoto);
/**
* 编辑的时候根据img去删除对应的数据
* @param img
* @return
*/
@Delete("delete from paper_photo where img = #{img}")
int updatePaperPhoto(String img);
@Delete("delete from paper_photo where questionid = #{questionid} and questionType = #{questionType} and paperid = (select paperid from exam_manage where type = #{subject})")
int deletePaperPhotoAll(@Param("questionid") String questionid, @Param("questionType") String questionType, @Param("subject") String subject);
}
......@@ -6,6 +6,7 @@ import com.exam.entity.PaperPhoto;
import com.exam.entity.PaperPhotoParams;
import com.exam.vo.AnswerVO;
import java.io.IOException;
import java.util.List;
public interface AnswerService {
......@@ -14,7 +15,7 @@ public interface AnswerService {
int deleteQuestion(String questionid,String tablename,String subject);
int updateAnswers(AnswerVO answerVO);
int updateAnswers(AnswerVO answerVO) throws IOException;
List<PaperPhoto> answersPhoto(String questionid, String tablename, String subject);
......
......@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.entity.MultiQuestion;
import java.io.IOException;
import java.util.List;
public interface MultiQuestionService {
......@@ -14,7 +15,7 @@ public interface MultiQuestionService {
MultiQuestion findOnlyQuestionId();
int add(MultiQuestion multiQuestion);
int add(MultiQuestion multiQuestion) throws IOException;
List<Integer> findBySubject(String subject,Integer pageNo);
}
package com.exam.serviceimpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.entity.PaperPhoto;
import com.exam.entity.PaperPhotoParams;
......@@ -8,14 +9,18 @@ import com.exam.mapper.AnswerMapper;
import com.exam.mapper.PaperPhotoMapper;
import com.exam.service.AnswerService;
import com.exam.util.Base64Util;
import com.exam.util.FtpUtil;
import com.exam.util.KeyUtil;
import com.exam.vo.AnswerVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class AnswerServiceImpl implements AnswerService {
......@@ -25,6 +30,9 @@ public class AnswerServiceImpl implements AnswerService {
@Autowired
private PaperPhotoMapper paperPhotoMapper;
@Value("${imagesUrl}")
public String imagesUrl;
@Override
public IPage<AnswerVO> findAll(Page<AnswerVO> page,String question) {
return answerMapper.findAll(page,question);
......@@ -43,40 +51,118 @@ public class AnswerServiceImpl implements AnswerService {
return flag;
}
/**
* 修改选项图片
* @param answerVO
* @return
* @throws IOException
*/
@Override
public int updateAnswers(AnswerVO answerVO) {
public int updateAnswers(AnswerVO answerVO) throws IOException {
int update = 0 ;
if("multi_question".equals(answerVO.getTablename())){
updatePhoto(answerVO);
update = answerMapper.updateMultiQuestionByQuestionid(answerVO);
}else if("judge_question".equals(answerVO.getTablename())){
update = answerMapper.updatejudgeQuestionByQuestionid(answerVO);
}
return update;
}
public void updatePhoto(AnswerVO answerVO) throws IOException {
//如果包含data:image/jpg;base64说明是新图片
if (answerVO.getAimg() != null && !answerVO.getAimg().isEmpty() && answerVO.getAimg().contains("data:image/jpg;base64")) {
String photoId = KeyUtil.getPhotoKey();
//base转为文件流上传服务器返回链接
answerVO.setAimg(getImgUrl(Base64Util.base64ToMultipartFile(answerVO.getAimg(),photoId+".jpg"),photoId));
}
if (answerVO.getBimg() != null && !answerVO.getBimg().isEmpty() && answerVO.getBimg().contains("data:image/jpg;base64")) {
String photoId = KeyUtil.getPhotoKey();
answerVO.setAimg(getImgUrl(Base64Util.base64ToMultipartFile(answerVO.getBimg(),photoId+".jpg"),photoId));
}
if (answerVO.getCimg() != null && !answerVO.getCimg().isEmpty() && answerVO.getCimg().contains("data:image/jpg;base64")) {
String photoId = KeyUtil.getPhotoKey();
answerVO.setAimg(getImgUrl(Base64Util.base64ToMultipartFile(answerVO.getCimg(),photoId+".jpg"),photoId));
}
if (answerVO.getDimg() != null && !answerVO.getDimg().isEmpty() && answerVO.getDimg().contains("data:image/jpg;base64")) {
String photoId = KeyUtil.getPhotoKey();
answerVO.setAimg(getImgUrl(Base64Util.base64ToMultipartFile(answerVO.getDimg(),photoId+".jpg"),photoId));
}
}
public String getImgUrl(MultipartFile file,String id){
if (file != null) {
try {
String type = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); // 视频后缀
String photoId = id; //照片唯一主键
String name = photoId + type;
// 上传视频到服务器
if(FtpUtil.uploadFile(name,file.getInputStream())){
return imagesUrl+name;
}else{
return null;
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
}else {
return null;
}
}
@Override
public List<PaperPhoto> answersPhoto(String questionid, String tablename, String paperid) {
return answerMapper.answersPhoto(questionid,getQuestionType(tablename),paperid);
}
/**
* 更新题干的照片
* @param paperPhotoParams
* @return
*/
@Override
public int updateAnswersPhoto(PaperPhotoParams paperPhotoParams) {
//先获取有哪些照片
List<PaperPhoto> listPhoto = answerMapper.answersPhoto(String.valueOf(paperPhotoParams.getQuestionId()),String.valueOf(paperPhotoParams.getQuestionType()),String.valueOf(paperPhotoParams.getPaperId()));
//当前题目有的图片
List<String> imgList = listPhoto.stream()
.map(PaperPhoto::getImg)
.collect(Collectors.toList());
//前端传过来之前就有的Url
List<String> urlFiles = paperPhotoParams.getUrlFiles();
//获取要删除的数据的img
List<String> missingUrls = imgList.stream()
.filter(url -> !urlFiles.contains(url))
.collect(Collectors.toList());
missingUrls.forEach(e->{
String urlName = getUrlName(e);//获取照片文件名称
if (urlName != "" && urlName!=null){
if (FtpUtil.delFile(urlName)){
//删除数据库上的照片
paperPhotoMapper.updatePaperPhoto(e);
}
}
});
int i = 0;
// 先删除相关图片
PaperPhoto paperPhotoDelete = new PaperPhoto();
BeanUtils.copyProperties(paperPhotoParams,paperPhotoDelete);
i = paperPhotoMapper.deletePaperPhoto(paperPhotoDelete);
// 照片处理
//上传新的照片
if (paperPhotoParams.getFiles() != null) {
String base64 = "";
for (MultipartFile file : paperPhotoParams.getFiles()) {
try {
base64 = "data:image/jpg;base64," + Base64Util.getBase64(file);
// 入照片库
PaperPhoto paperPhoto = new PaperPhoto();
BeanUtils.copyProperties(paperPhotoParams,paperPhoto);
paperPhoto.setImg(base64);
i = paperPhotoMapper.add(paperPhoto);
String type = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); //类型
String videoId = KeyUtil.getPhotoKey(); //照片的唯一主键
String name = videoId + type;//照片名称 = 照片唯一主键+ 照片的类型
// 上传视频到服务器
if(FtpUtil.uploadFile(name,file.getInputStream())){
// 入照片库
PaperPhoto paperPhoto = new PaperPhoto();
BeanUtils.copyProperties(paperPhotoParams,paperPhoto);
paperPhoto.setImg(imagesUrl+name);
i = paperPhotoMapper.add(paperPhoto);;
}else{
return 0;
};
} catch (IOException e) {
e.printStackTrace();
return 0;
......@@ -86,6 +172,18 @@ public class AnswerServiceImpl implements AnswerService {
return i;
}
public String getUrlName(String urlString){
int lastSlashIndex = urlString.lastIndexOf("/");
int dotIndex = urlString.lastIndexOf(".");
if (lastSlashIndex != -1 && dotIndex != -1 && dotIndex > lastSlashIndex) {
String fileName = urlString.substring(lastSlashIndex + 1);
return fileName;
} else {
return "";
}
}
@Override
public int deleteAnswersPhoto(PaperPhotoParams paperPhotoParams) {
......
package com.exam.serviceimpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.entity.MultiQuestion;
import com.exam.entity.PaperPhoto;
import com.exam.entity.Video;
import com.exam.mapper.AnswerMapper;
import com.exam.mapper.MultiQuestionMapper;
import com.exam.service.MultiQuestionService;
import com.exam.util.Base64Util;
import com.exam.util.FtpUtil;
import com.exam.util.KeyUtil;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
......@@ -21,6 +30,8 @@ public class MultiQuestionServiceImpl implements MultiQuestionService {
@Autowired
AnswerMapper answerMapper;
@Value("${imagesUrl}")
public String imagesUrl;
@Override
public List<MultiQuestion> findByIdAndType(Integer PaperId) {
List<MultiQuestion> list = multiQuestionMapper.findByIdAndType(PaperId);
......@@ -44,11 +55,54 @@ public class MultiQuestionServiceImpl implements MultiQuestionService {
return multiQuestionMapper.findOnlyQuestionId();
}
/**
* 添加选项图片
* @param multiQuestion
* @return
*/
@Override
public int add(MultiQuestion multiQuestion) {
public int add(MultiQuestion multiQuestion) throws IOException {
if (StringUtils.isNotEmpty(multiQuestion.getAimg())){
String photoId = KeyUtil.getPhotoKey();
//base转为文件流上传服务器返回链接
multiQuestion.setAimg(getImgUrl(Base64Util.base64ToMultipartFile(multiQuestion.getAimg(),photoId+".jpg"),photoId));
}
if (StringUtils.isNotEmpty(multiQuestion.getBimg())){
String photoId = KeyUtil.getPhotoKey();
multiQuestion.setAimg(getImgUrl(Base64Util.base64ToMultipartFile(multiQuestion.getBimg(),photoId+".jpg"),photoId));
}
if (StringUtils.isNotEmpty(multiQuestion.getCimg())){
String photoId = KeyUtil.getPhotoKey();
multiQuestion.setAimg(getImgUrl(Base64Util.base64ToMultipartFile(multiQuestion.getCimg(),photoId+".jpg"),photoId));
}
if (StringUtils.isNotEmpty(multiQuestion.getDimg())){
String photoId = KeyUtil.getPhotoKey();
multiQuestion.setAimg(getImgUrl(Base64Util.base64ToMultipartFile(multiQuestion.getDimg(),photoId+".jpg"),photoId));
}
return multiQuestionMapper.add(multiQuestion);
}
public String getImgUrl(MultipartFile file,String id){
if (file != null) {
try {
String type = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); // 视频后缀
String photoId = id; //照片唯一主键
String name = photoId + type;
// 上传视频到服务器
if(FtpUtil.uploadFile(name,file.getInputStream())){
return imagesUrl+name;
}else{
return null;
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
}else {
return null;
}
}
@Override
public List<Integer> findBySubject(String subject, Integer pageNo) {
return multiQuestionMapper.findBySubject(subject,pageNo);
......
......@@ -4,12 +4,16 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.entity.PaperManage;
import com.exam.entity.PaperPhoto;
import com.exam.entity.Video;
import com.exam.mapper.PaperMapper;
import com.exam.mapper.PaperPhotoMapper;
import com.exam.service.PaperService;
import com.exam.util.Base64Util;
import com.exam.util.FtpUtil;
import com.exam.util.KeyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
......@@ -19,6 +23,8 @@ import java.util.List;
@Service
public class PaperServiceImpl implements PaperService {
@Value("${imagesUrl}")
public String imagesUrl;
@Autowired
private PaperMapper paperMapper;
@Autowired
......@@ -32,24 +38,35 @@ public class PaperServiceImpl implements PaperService {
public List<PaperManage> findById(Integer paperId) {
return paperMapper.findById(paperId);
}
/**
* 添加题干照片
* @param paperManage
* @return
*/
@Override
public int add(PaperManage paperManage) {
int add = paperMapper.add(paperManage);
if(add >0 ){
// 照片处理
if (paperManage.getFiles() != null) {
String base64 = "";
for (MultipartFile file : paperManage.getFiles()) {
try {
base64 = "data:image/jpg;base64," + Base64Util.getBase64(file);
// 入照片库
PaperPhoto paperPhoto = new PaperPhoto();
BeanUtils.copyProperties(paperManage,paperPhoto);
paperPhoto.setImg(base64);
paperPhotoMapper.add(paperPhoto);
String type = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); //类型
String videoId = KeyUtil.getPhotoKey(); //照片的唯一主键
String name = videoId + type;//照片名称 = 照片唯一主键+ 照片的类型
// 上传视频到服务器
if(FtpUtil.uploadFile(name,file.getInputStream())){
// 入照片库
PaperPhoto paperPhoto = new PaperPhoto();
BeanUtils.copyProperties(paperManage,paperPhoto);
paperPhoto.setImg(imagesUrl+name);
paperPhotoMapper.add(paperPhoto);
}else{
return 0;
};
} catch (IOException e) {
e.printStackTrace();
return 0;
}
}
}
......
package com.exam.util;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.util.Base64Utils;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
......@@ -66,6 +66,23 @@ public class Base64Util {
return base64EncoderImg;
}
/**
* base64转为文件流
* @param base64
* @param name
* @return
* @throws IOException
*/
public static MultipartFile base64ToMultipartFile(String base64,String name) throws IOException {
// 解码 base64 字符串为字节数组
byte[] bytes = Base64.getDecoder().decode(base64.split(",")[1]);
// 创建一个 MultipartFile 对象
String fileName = name; // 文件名可以自己指定
InputStream inputStream = new ByteArrayInputStream(bytes);
return new MockMultipartFile(fileName, fileName, "image/jpeg", inputStream);
}
public static void main(String[] args) {
System.out.println(Base64Util.encode("0"));
System.out.println(Base64Util.decode("MA=="));
......
......@@ -32,6 +32,15 @@ public class KeyUtil {
return "VIDEO"+dateNowStr+ String.valueOf(num).substring(0,7);
}
public static synchronized String getPhotoKey() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = new Date();
String dateNowStr = sdf.format(date);
int num = UUID.randomUUID().toString().replaceAll("-","").hashCode();
num = num < 0 ? -num:num;
return "PHOTO"+dateNowStr+ String.valueOf(num).substring(0,7);
}
/**
*
* @Description: 生成唯一的主键 格式: 19位
......
#spring.datasource.username=root
#spring.datasource.password=gkptCcYy123
#spring.datasource.url=jdbc:mysql://47.92.48.137:3900/gkpt?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#mybatis.configuration.mapUnderscoreToCamelCase=true
spring.datasource.username=root
spring.datasource.password=gkptCcYy123
spring.datasource.url=jdbc:mysql://47.92.48.137:3900/gkpt?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/gkpt?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
mybatis.configuration.mapUnderscoreToCamelCase=true
#FTP服务器ip
#FTP\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFDip
host=47.92.48.137
#FTP服务器端口
#FTP\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\u02FF\uFFFD
port=54322
#FTP登录账号
#FTP\uFFFD\uFFFD\u00BC\uFFFD\u02FA\uFFFD
usernameFtp=root
#FTP登录密码
#FTP\uFFFD\uFFFD\u00BC\uFFFD\uFFFD\uFFFD\uFFFD
password=CcYunzheng900118.
#FTP路径
#FTP\u00B7\uFFFD\uFFFD
basePath=/data/images
#video服务器路径
#video\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\u00B7\uFFFD\uFFFD
imagesUrl=http://47.92.48.137:1008/images/
spring.profiles.active=dev
spring.profiles.active=prod
server.port=8080
logging.level.com.exam.mapper=debug
#设置单个文件大小
#\uFFFD\uFFFD\uFFFD\u00F5\uFFFD\uFFFD\uFFFD\uFFFD\u013C\uFFFD\uFFFD\uFFFD\u0421
spring.servlet.multipart.max-file-size= 1024MB
#设置单次请求文件的总大小
#\uFFFD\uFFFD\uFFFD\u00F5\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\u013C\uFFFD\uFFFD\uFFFD\uFFFD\u0734\uFFFD\u0421
spring.servlet.multipart.max-request-size= 1024MB
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment