Commit e0c8a682 by 宋珺琪

修改选项图片存到服务器

parent 4b07a7b8
...@@ -92,6 +92,10 @@ ...@@ -92,6 +92,10 @@
<artifactId>commons-net</artifactId> <artifactId>commons-net</artifactId>
<version>3.8.0</version> <version>3.8.0</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
...@@ -11,6 +11,8 @@ import com.exam.vo.AnswerVO; ...@@ -11,6 +11,8 @@ import com.exam.vo.AnswerVO;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.io.IOException;
@RestController @RestController
public class AnswerController { public class AnswerController {
...@@ -36,8 +38,13 @@ public class AnswerController { ...@@ -36,8 +38,13 @@ public class AnswerController {
} }
} }
/**
* 修改试题
* @param answerVO
* @return
*/
@PostMapping("/updateAnswers") @PostMapping("/updateAnswers")
public ApiResult updateAnswers(@RequestBody AnswerVO answerVO){ public ApiResult updateAnswers(@RequestBody AnswerVO answerVO) throws IOException {
int res = answerService.updateAnswers(answerVO); int res = answerService.updateAnswers(answerVO);
if (res == 0) { if (res == 0) {
return ApiResultHandler.buildApiResult(400,"修改失败",res); return ApiResultHandler.buildApiResult(400,"修改失败",res);
...@@ -51,6 +58,11 @@ public class AnswerController { ...@@ -51,6 +58,11 @@ public class AnswerController {
return ApiResultHandler.buildApiResult(200,"查询题库照片",answerService.answersPhoto(questionid,tablename,paperid)); return ApiResultHandler.buildApiResult(200,"查询题库照片",answerService.answersPhoto(questionid,tablename,paperid));
} }
/**
* 修改题库图片
* @param paperPhotoParams
* @return
*/
@PostMapping("/updateAnswersPhoto") @PostMapping("/updateAnswersPhoto")
public ApiResult updateAnswersPhoto(PaperPhotoParams paperPhotoParams){ public ApiResult updateAnswersPhoto(PaperPhotoParams paperPhotoParams){
int res = answerService.updateAnswersPhoto(paperPhotoParams); int res = answerService.updateAnswersPhoto(paperPhotoParams);
......
...@@ -10,6 +10,8 @@ import org.springframework.web.bind.annotation.PostMapping; ...@@ -10,6 +10,8 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
@RestController @RestController
public class MultiQuestionController { public class MultiQuestionController {
...@@ -22,8 +24,14 @@ public class MultiQuestionController { ...@@ -22,8 +24,14 @@ public class MultiQuestionController {
return ApiResultHandler.buildApiResult(200,"查询成功",res); return ApiResultHandler.buildApiResult(200,"查询成功",res);
} }
/**
* 添加题
* @param multiQuestion
* @return
* @throws IOException
*/
@PostMapping("/MultiQuestion") @PostMapping("/MultiQuestion")
public ApiResult add(@RequestBody MultiQuestion multiQuestion) { public ApiResult add(@RequestBody MultiQuestion multiQuestion) throws IOException {
int res = multiQuestionService.add(multiQuestion); int res = multiQuestionService.add(multiQuestion);
if (res != 0) { if (res != 0) {
......
...@@ -49,6 +49,11 @@ public class PaperController { ...@@ -49,6 +49,11 @@ public class PaperController {
return map; return map;
} }
/**
* 添加题干照片
* @param paperManage
* @return
*/
@PostMapping("/paperManage") @PostMapping("/paperManage")
public ApiResult add(PaperManage paperManage) { public ApiResult add(PaperManage paperManage) {
int res = paperService.add(paperManage); int res = paperService.add(paperManage);
......
...@@ -3,6 +3,8 @@ package com.exam.entity; ...@@ -3,6 +3,8 @@ package com.exam.entity;
import lombok.Data; import lombok.Data;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.util.List;
@Data @Data
public class PaperPhotoParams { public class PaperPhotoParams {
private Integer paperId; private Integer paperId;
...@@ -11,5 +13,7 @@ public class PaperPhotoParams { ...@@ -11,5 +13,7 @@ public class PaperPhotoParams {
private Integer questionId; 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 { ...@@ -16,6 +16,14 @@ public interface PaperPhotoMapper {
@Delete("delete from paper_photo where paperId = #{paperId} and questionType = #{questionType} and questionId = #{questionId}") @Delete("delete from paper_photo where paperId = #{paperId} and questionType = #{questionType} and questionId = #{questionId}")
int deletePaperPhoto(PaperPhoto paperPhoto); 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})") @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); int deletePaperPhotoAll(@Param("questionid") String questionid, @Param("questionType") String questionType, @Param("subject") String subject);
} }
...@@ -6,6 +6,7 @@ import com.exam.entity.PaperPhoto; ...@@ -6,6 +6,7 @@ import com.exam.entity.PaperPhoto;
import com.exam.entity.PaperPhotoParams; import com.exam.entity.PaperPhotoParams;
import com.exam.vo.AnswerVO; import com.exam.vo.AnswerVO;
import java.io.IOException;
import java.util.List; import java.util.List;
public interface AnswerService { public interface AnswerService {
...@@ -14,7 +15,7 @@ public interface AnswerService { ...@@ -14,7 +15,7 @@ public interface AnswerService {
int deleteQuestion(String questionid,String tablename,String subject); 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); List<PaperPhoto> answersPhoto(String questionid, String tablename, String subject);
......
...@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage; ...@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.entity.MultiQuestion; import com.exam.entity.MultiQuestion;
import java.io.IOException;
import java.util.List; import java.util.List;
public interface MultiQuestionService { public interface MultiQuestionService {
...@@ -14,7 +15,7 @@ public interface MultiQuestionService { ...@@ -14,7 +15,7 @@ public interface MultiQuestionService {
MultiQuestion findOnlyQuestionId(); MultiQuestion findOnlyQuestionId();
int add(MultiQuestion multiQuestion); int add(MultiQuestion multiQuestion) throws IOException;
List<Integer> findBySubject(String subject,Integer pageNo); List<Integer> findBySubject(String subject,Integer pageNo);
} }
package com.exam.serviceimpl; package com.exam.serviceimpl;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.entity.PaperPhoto; import com.exam.entity.PaperPhoto;
import com.exam.entity.PaperPhotoParams; import com.exam.entity.PaperPhotoParams;
...@@ -8,14 +9,18 @@ import com.exam.mapper.AnswerMapper; ...@@ -8,14 +9,18 @@ import com.exam.mapper.AnswerMapper;
import com.exam.mapper.PaperPhotoMapper; import com.exam.mapper.PaperPhotoMapper;
import com.exam.service.AnswerService; import com.exam.service.AnswerService;
import com.exam.util.Base64Util; import com.exam.util.Base64Util;
import com.exam.util.FtpUtil;
import com.exam.util.KeyUtil;
import com.exam.vo.AnswerVO; import com.exam.vo.AnswerVO;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
@Service @Service
public class AnswerServiceImpl implements AnswerService { public class AnswerServiceImpl implements AnswerService {
...@@ -25,6 +30,9 @@ public class AnswerServiceImpl implements AnswerService { ...@@ -25,6 +30,9 @@ public class AnswerServiceImpl implements AnswerService {
@Autowired @Autowired
private PaperPhotoMapper paperPhotoMapper; private PaperPhotoMapper paperPhotoMapper;
@Value("${imagesUrl}")
public String imagesUrl;
@Override @Override
public IPage<AnswerVO> findAll(Page<AnswerVO> page,String question) { public IPage<AnswerVO> findAll(Page<AnswerVO> page,String question) {
return answerMapper.findAll(page,question); return answerMapper.findAll(page,question);
...@@ -43,40 +51,118 @@ public class AnswerServiceImpl implements AnswerService { ...@@ -43,40 +51,118 @@ public class AnswerServiceImpl implements AnswerService {
return flag; return flag;
} }
/**
* 修改选项图片
* @param answerVO
* @return
* @throws IOException
*/
@Override @Override
public int updateAnswers(AnswerVO answerVO) { public int updateAnswers(AnswerVO answerVO) throws IOException {
int update = 0 ; int update = 0 ;
if("multi_question".equals(answerVO.getTablename())){ if("multi_question".equals(answerVO.getTablename())){
updatePhoto(answerVO);
update = answerMapper.updateMultiQuestionByQuestionid(answerVO); update = answerMapper.updateMultiQuestionByQuestionid(answerVO);
}else if("judge_question".equals(answerVO.getTablename())){ }else if("judge_question".equals(answerVO.getTablename())){
update = answerMapper.updatejudgeQuestionByQuestionid(answerVO); update = answerMapper.updatejudgeQuestionByQuestionid(answerVO);
} }
return update; 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 @Override
public List<PaperPhoto> answersPhoto(String questionid, String tablename, String paperid) { public List<PaperPhoto> answersPhoto(String questionid, String tablename, String paperid) {
return answerMapper.answersPhoto(questionid,getQuestionType(tablename),paperid); return answerMapper.answersPhoto(questionid,getQuestionType(tablename),paperid);
} }
/**
* 更新题干的照片
* @param paperPhotoParams
* @return
*/
@Override @Override
public int updateAnswersPhoto(PaperPhotoParams paperPhotoParams) { 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; int i = 0;
// 先删除相关图片 //上传新的照片
PaperPhoto paperPhotoDelete = new PaperPhoto();
BeanUtils.copyProperties(paperPhotoParams,paperPhotoDelete);
i = paperPhotoMapper.deletePaperPhoto(paperPhotoDelete);
// 照片处理
if (paperPhotoParams.getFiles() != null) { if (paperPhotoParams.getFiles() != null) {
String base64 = "";
for (MultipartFile file : paperPhotoParams.getFiles()) { for (MultipartFile file : paperPhotoParams.getFiles()) {
try { try {
base64 = "data:image/jpg;base64," + Base64Util.getBase64(file); String type = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); //类型
// 入照片库 String videoId = KeyUtil.getPhotoKey(); //照片的唯一主键
PaperPhoto paperPhoto = new PaperPhoto(); String name = videoId + type;//照片名称 = 照片唯一主键+ 照片的类型
BeanUtils.copyProperties(paperPhotoParams,paperPhoto); // 上传视频到服务器
paperPhoto.setImg(base64); if(FtpUtil.uploadFile(name,file.getInputStream())){
i = paperPhotoMapper.add(paperPhoto); // 入照片库
PaperPhoto paperPhoto = new PaperPhoto();
BeanUtils.copyProperties(paperPhotoParams,paperPhoto);
paperPhoto.setImg(imagesUrl+name);
i = paperPhotoMapper.add(paperPhoto);;
}else{
return 0;
};
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
return 0; return 0;
...@@ -86,6 +172,18 @@ public class AnswerServiceImpl implements AnswerService { ...@@ -86,6 +172,18 @@ public class AnswerServiceImpl implements AnswerService {
return i; 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 @Override
public int deleteAnswersPhoto(PaperPhotoParams paperPhotoParams) { public int deleteAnswersPhoto(PaperPhotoParams paperPhotoParams) {
......
package com.exam.serviceimpl; package com.exam.serviceimpl;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.entity.MultiQuestion; import com.exam.entity.MultiQuestion;
import com.exam.entity.PaperPhoto; import com.exam.entity.PaperPhoto;
import com.exam.entity.Video;
import com.exam.mapper.AnswerMapper; import com.exam.mapper.AnswerMapper;
import com.exam.mapper.MultiQuestionMapper; import com.exam.mapper.MultiQuestionMapper;
import com.exam.service.MultiQuestionService; 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.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -21,6 +30,8 @@ public class MultiQuestionServiceImpl implements MultiQuestionService { ...@@ -21,6 +30,8 @@ public class MultiQuestionServiceImpl implements MultiQuestionService {
@Autowired @Autowired
AnswerMapper answerMapper; AnswerMapper answerMapper;
@Value("${imagesUrl}")
public String imagesUrl;
@Override @Override
public List<MultiQuestion> findByIdAndType(Integer PaperId) { public List<MultiQuestion> findByIdAndType(Integer PaperId) {
List<MultiQuestion> list = multiQuestionMapper.findByIdAndType(PaperId); List<MultiQuestion> list = multiQuestionMapper.findByIdAndType(PaperId);
...@@ -44,11 +55,54 @@ public class MultiQuestionServiceImpl implements MultiQuestionService { ...@@ -44,11 +55,54 @@ public class MultiQuestionServiceImpl implements MultiQuestionService {
return multiQuestionMapper.findOnlyQuestionId(); return multiQuestionMapper.findOnlyQuestionId();
} }
/**
* 添加选项图片
* @param multiQuestion
* @return
*/
@Override @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); 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 @Override
public List<Integer> findBySubject(String subject, Integer pageNo) { public List<Integer> findBySubject(String subject, Integer pageNo) {
return multiQuestionMapper.findBySubject(subject,pageNo); return multiQuestionMapper.findBySubject(subject,pageNo);
......
...@@ -4,12 +4,16 @@ import com.baomidou.mybatisplus.core.metadata.IPage; ...@@ -4,12 +4,16 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.entity.PaperManage; import com.exam.entity.PaperManage;
import com.exam.entity.PaperPhoto; import com.exam.entity.PaperPhoto;
import com.exam.entity.Video;
import com.exam.mapper.PaperMapper; import com.exam.mapper.PaperMapper;
import com.exam.mapper.PaperPhotoMapper; import com.exam.mapper.PaperPhotoMapper;
import com.exam.service.PaperService; import com.exam.service.PaperService;
import com.exam.util.Base64Util; import com.exam.util.Base64Util;
import com.exam.util.FtpUtil;
import com.exam.util.KeyUtil;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
...@@ -19,6 +23,8 @@ import java.util.List; ...@@ -19,6 +23,8 @@ import java.util.List;
@Service @Service
public class PaperServiceImpl implements PaperService { public class PaperServiceImpl implements PaperService {
@Value("${imagesUrl}")
public String imagesUrl;
@Autowired @Autowired
private PaperMapper paperMapper; private PaperMapper paperMapper;
@Autowired @Autowired
...@@ -32,24 +38,35 @@ public class PaperServiceImpl implements PaperService { ...@@ -32,24 +38,35 @@ public class PaperServiceImpl implements PaperService {
public List<PaperManage> findById(Integer paperId) { public List<PaperManage> findById(Integer paperId) {
return paperMapper.findById(paperId); return paperMapper.findById(paperId);
} }
/**
* 添加题干照片
* @param paperManage
* @return
*/
@Override @Override
public int add(PaperManage paperManage) { public int add(PaperManage paperManage) {
int add = paperMapper.add(paperManage); int add = paperMapper.add(paperManage);
if(add >0 ){ if(add >0 ){
// 照片处理 // 照片处理
if (paperManage.getFiles() != null) { if (paperManage.getFiles() != null) {
String base64 = "";
for (MultipartFile file : paperManage.getFiles()) { for (MultipartFile file : paperManage.getFiles()) {
try { try {
base64 = "data:image/jpg;base64," + Base64Util.getBase64(file); String type = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); //类型
// 入照片库 String videoId = KeyUtil.getPhotoKey(); //照片的唯一主键
PaperPhoto paperPhoto = new PaperPhoto(); String name = videoId + type;//照片名称 = 照片唯一主键+ 照片的类型
BeanUtils.copyProperties(paperManage,paperPhoto); // 上传视频到服务器
paperPhoto.setImg(base64); if(FtpUtil.uploadFile(name,file.getInputStream())){
paperPhotoMapper.add(paperPhoto); // 入照片库
PaperPhoto paperPhoto = new PaperPhoto();
BeanUtils.copyProperties(paperManage,paperPhoto);
paperPhoto.setImg(imagesUrl+name);
paperPhotoMapper.add(paperPhoto);
}else{
return 0;
};
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
return 0;
} }
} }
} }
......
package com.exam.util; 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 org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder; import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder; import sun.misc.BASE64Encoder;
import java.io.ByteArrayOutputStream; import java.io.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.util.Base64; import java.util.Base64;
...@@ -66,6 +66,23 @@ public class Base64Util { ...@@ -66,6 +66,23 @@ public class Base64Util {
return base64EncoderImg; 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) { public static void main(String[] args) {
System.out.println(Base64Util.encode("0")); System.out.println(Base64Util.encode("0"));
System.out.println(Base64Util.decode("MA==")); System.out.println(Base64Util.decode("MA=="));
......
...@@ -32,6 +32,15 @@ public class KeyUtil { ...@@ -32,6 +32,15 @@ public class KeyUtil {
return "VIDEO"+dateNowStr+ String.valueOf(num).substring(0,7); 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位 * @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.username=root
spring.datasource.password=gkptCcYy123 spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://47.92.48.137:3900/gkpt?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai 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.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
mybatis.configuration.mapUnderscoreToCamelCase=true mybatis.configuration.mapUnderscoreToCamelCase=true
#FTP服务器ip #FTP\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFDip
host=47.92.48.137 host=47.92.48.137
#FTP服务器端口 #FTP\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\u02FF\uFFFD
port=54322 port=54322
#FTP登录账号 #FTP\uFFFD\uFFFD\u00BC\uFFFD\u02FA\uFFFD
usernameFtp=root usernameFtp=root
#FTP登录密码 #FTP\uFFFD\uFFFD\u00BC\uFFFD\uFFFD\uFFFD\uFFFD
password=CcYunzheng900118. password=CcYunzheng900118.
#FTP路径 #FTP\u00B7\uFFFD\uFFFD
basePath=/data/images basePath=/data/images
#video服务器路径 #video\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\u00B7\uFFFD\uFFFD
imagesUrl=http://47.92.48.137:1008/images/ imagesUrl=http://47.92.48.137:1008/images/
spring.profiles.active=dev spring.profiles.active=prod
server.port=8080 server.port=8080
logging.level.com.exam.mapper=debug 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 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 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