|
@@ -12,6 +12,7 @@ import java.util.HashMap;
|
|
import java.util.HashSet;
|
|
import java.util.HashSet;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
+import java.util.Random;
|
|
import java.util.Set;
|
|
import java.util.Set;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
import java.util.regex.Pattern;
|
|
@@ -20,6 +21,7 @@ import com.qmth.cqb.utils.exception.PaperException;
|
|
|
|
|
|
import main.java.com.UpYun;
|
|
import main.java.com.UpYun;
|
|
|
|
|
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.nlpcn.commons.lang.util.StringUtil;
|
|
import org.nlpcn.commons.lang.util.StringUtil;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -113,22 +115,22 @@ public class PaperService {
|
|
private QuestionAudioRepo questionAudioRepo;
|
|
private QuestionAudioRepo questionAudioRepo;
|
|
|
|
|
|
@Value("${upyun.radioType}")
|
|
@Value("${upyun.radioType}")
|
|
- protected String radioType;
|
|
|
|
|
|
+ private String radioType;
|
|
|
|
|
|
@Value("${upyun.audio.maxsize}")
|
|
@Value("${upyun.audio.maxsize}")
|
|
- protected String maxsize;
|
|
|
|
|
|
+ private String audioMaxsize;
|
|
|
|
|
|
@Value("${upyun.audio.uploadUrl}")
|
|
@Value("${upyun.audio.uploadUrl}")
|
|
- protected String upyunRadioPath;
|
|
|
|
|
|
+ private String upyunRadioPath;
|
|
|
|
|
|
@Value("${upyun.bucketName}")
|
|
@Value("${upyun.bucketName}")
|
|
- protected String bucketName;
|
|
|
|
|
|
+ private String bucketName;
|
|
|
|
|
|
@Value("${upyun.userName}")
|
|
@Value("${upyun.userName}")
|
|
- protected String userName;
|
|
|
|
|
|
+ private String userName;
|
|
|
|
|
|
@Value("${upyun.password}")
|
|
@Value("${upyun.password}")
|
|
- protected String password;
|
|
|
|
|
|
+ private String password;
|
|
|
|
|
|
public static final String TEMP_FILE_EXP = "docxExport/";
|
|
public static final String TEMP_FILE_EXP = "docxExport/";
|
|
|
|
|
|
@@ -992,85 +994,73 @@ public class PaperService {
|
|
* @throws IOException
|
|
* @throws IOException
|
|
*/
|
|
*/
|
|
public void uploadRadio(List<MultipartFile> files,String paperId,AccessUser accessUser) throws IOException{
|
|
public void uploadRadio(List<MultipartFile> files,String paperId,AccessUser accessUser) throws IOException{
|
|
- String mp3DirectoryPath = TEMP_FILE_EXP + File.separator + paperId;
|
|
|
|
- //新建文件夹
|
|
|
|
- File mp3Directory = new File(mp3DirectoryPath);
|
|
|
|
- if(!mp3Directory.exists()){
|
|
|
|
- mp3Directory.mkdirs();
|
|
|
|
- }
|
|
|
|
- byte[] bufs = new byte[1024 * 10];
|
|
|
|
|
|
+ //根据试卷id,查询该试卷
|
|
|
|
+ Paper paper = paperRepo.findOne(paperId);
|
|
for(MultipartFile file: files){
|
|
for(MultipartFile file: files){
|
|
//判断文件大小
|
|
//判断文件大小
|
|
long fileSize = file.getSize();
|
|
long fileSize = file.getSize();
|
|
- int size = Integer.parseInt(maxsize);
|
|
|
|
|
|
+ int size = Integer.parseInt(audioMaxsize);
|
|
if(fileSize>size * 1048576){
|
|
if(fileSize>size * 1048576){
|
|
- throw new RuntimeException("音频文件超过5M");
|
|
|
|
|
|
+ throw new RuntimeException("音频文件大小超过5M,不能上传");
|
|
}
|
|
}
|
|
- //新建MP3文件
|
|
|
|
|
|
+ //根据试卷查询所有的小题,根据文件名匹配出当前小题ID
|
|
String questionId = "";
|
|
String questionId = "";
|
|
String numbers[] = file.getOriginalFilename().split("_");
|
|
String numbers[] = file.getOriginalFilename().split("_");
|
|
- //根据试卷id,查询该试卷
|
|
|
|
- Paper paper = paperRepo.findOne(paperId);
|
|
|
|
- //根据试卷查询所有的小题
|
|
|
|
List<PaperDetailUnit> paperDetailUnits = paperDetailUnitRepo.findByPaper(paper);
|
|
List<PaperDetailUnit> paperDetailUnits = paperDetailUnitRepo.findByPaper(paper);
|
|
for(PaperDetailUnit paperDetailUnit:paperDetailUnits){
|
|
for(PaperDetailUnit paperDetailUnit:paperDetailUnits){
|
|
if(paperDetailUnit.getNumber().toString().equals(numbers[0])){
|
|
if(paperDetailUnit.getNumber().toString().equals(numbers[0])){
|
|
questionId = paperDetailUnit.getQuestion().getId();
|
|
questionId = paperDetailUnit.getQuestion().getId();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- String mp3FileNameString = questionId + "_" + file.getOriginalFilename();
|
|
|
|
- File mp3File = new File(mp3DirectoryPath + File.separator +mp3FileNameString);
|
|
|
|
- FileOutputStream outputStream = new FileOutputStream(mp3File);
|
|
|
|
- BufferedInputStream bis = new BufferedInputStream(file.getInputStream(),1024*10);
|
|
|
|
- int read = 0;
|
|
|
|
- while ((read = bis.read(bufs,0,1024*10)) != -1) {
|
|
|
|
- outputStream.write(bufs,0,read);
|
|
|
|
- }
|
|
|
|
- bis.close();
|
|
|
|
- outputStream.flush();
|
|
|
|
- outputStream.close();
|
|
|
|
- //上传到又拍云
|
|
|
|
- UpYun upYun = new UpYun(bucketName, userName, password);
|
|
|
|
- upYun.writeFile(upyunRadioPath+mp3FileNameString, mp3File,true);
|
|
|
|
- //删除服务器上文件
|
|
|
|
- mp3File.delete();
|
|
|
|
|
|
+ uploadAudioFile(paperId,questionId,file,accessUser);
|
|
|
|
+ appendAudioTag(file.getOriginalFilename(),questionId);
|
|
}
|
|
}
|
|
|
|
+ paper.setHasAudio(true);
|
|
|
|
+ paperRepo.save(paper);
|
|
//删除服务器文件夹
|
|
//删除服务器文件夹
|
|
- mp3Directory.delete();
|
|
|
|
- saveQuestionAudio(files, paperId, accessUser);
|
|
|
|
- appendAudioTag(files,paperId);
|
|
|
|
|
|
+ String mp3DirectoryPath = TEMP_FILE_EXP + File.separator + paperId;
|
|
|
|
+ File mp3Directory = new File(mp3DirectoryPath);
|
|
|
|
+ FileUtils.deleteDirectory(mp3Directory);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 保存音频文件到本地
|
|
|
|
- * @param files
|
|
|
|
|
|
+ * 上传音频文件至又拍云
|
|
* @param paperId
|
|
* @param paperId
|
|
- * @param accessUser
|
|
|
|
|
|
+ * @param questionId
|
|
|
|
+ * @param file
|
|
*/
|
|
*/
|
|
- public void saveQuestionAudio(List<MultipartFile> files,String paperId,AccessUser accessUser){
|
|
|
|
- for(MultipartFile file:files){
|
|
|
|
- String questionId = "";
|
|
|
|
- String numbers[] = file.getOriginalFilename().split("_");
|
|
|
|
- //根据试卷id,查询该试卷
|
|
|
|
- Paper paper = paperRepo.findOne(paperId);
|
|
|
|
- paper.setHasAudio(true);
|
|
|
|
- paperRepo.save(paper);
|
|
|
|
- //根据试卷查询所有的小题
|
|
|
|
- List<PaperDetailUnit> paperDetailUnits = paperDetailUnitRepo.findByPaper(paper);
|
|
|
|
- for(PaperDetailUnit paperDetailUnit:paperDetailUnits){
|
|
|
|
- if(paperDetailUnit.getNumber().toString().equals(numbers[0])){
|
|
|
|
- questionId = paperDetailUnit.getQuestion().getId();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- QuestionAudio questionAudio = new QuestionAudio();
|
|
|
|
- questionAudio = questionAudioService.findByQuestionIdAndFileName(questionId, file.getOriginalFilename());
|
|
|
|
- if(questionAudio != null){
|
|
|
|
- questionAudioRepo.delete(questionAudio);
|
|
|
|
- }
|
|
|
|
- String fileUrl = upyunRadioPath + questionId+ "_" + file.getOriginalFilename();
|
|
|
|
- questionAudio = new QuestionAudio(paperId,questionId, file.getOriginalFilename(), fileUrl);
|
|
|
|
- questionAudioService.saveQuestionAudio(questionAudio, accessUser);
|
|
|
|
- }
|
|
|
|
|
|
+ private void uploadAudioFile(String paperId,String questionId,MultipartFile file,AccessUser accessUser){
|
|
|
|
+ try {
|
|
|
|
+ String mp3DirectoryPath = TEMP_FILE_EXP + File.separator + paperId;
|
|
|
|
+ //新建文件夹
|
|
|
|
+ File mp3Directory = new File(mp3DirectoryPath);
|
|
|
|
+ if(!mp3Directory.exists()){
|
|
|
|
+ mp3Directory.mkdirs();
|
|
|
|
+ }
|
|
|
|
+ byte[] bufs = new byte[1024 * 4];
|
|
|
|
+ //使用随机数,防止缓存
|
|
|
|
+ Random random = new Random();
|
|
|
|
+ int randomNumber = random.nextInt(10000);
|
|
|
|
+ String mp3FileNameString = questionId+"_"+randomNumber+"_"+ file.getOriginalFilename();
|
|
|
|
+ File mp3File = new File(mp3DirectoryPath + File.separator +mp3FileNameString);
|
|
|
|
+ FileOutputStream outputStream = new FileOutputStream(mp3File);
|
|
|
|
+ BufferedInputStream bis = new BufferedInputStream(file.getInputStream(),1024*10);
|
|
|
|
+ int read = 0;
|
|
|
|
+ while ((read = bis.read(bufs,0,1024*4)) != -1) {
|
|
|
|
+ outputStream.write(bufs,0,read);
|
|
|
|
+ }
|
|
|
|
+ bis.close();
|
|
|
|
+ outputStream.flush();
|
|
|
|
+ outputStream.close();
|
|
|
|
+ //上传到又拍云
|
|
|
|
+ UpYun upYun = new UpYun(bucketName, userName, password);
|
|
|
|
+ upYun.writeFile(upyunRadioPath+mp3FileNameString, mp3File,true);
|
|
|
|
+ mp3File.delete();
|
|
|
|
+ //保存记录
|
|
|
|
+ questionAudioService.saveQuestionAudio(new QuestionAudio(questionId,file.getOriginalFilename(),upyunRadioPath+mp3FileNameString),accessUser);
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -1078,40 +1068,37 @@ public class PaperService {
|
|
* @param files
|
|
* @param files
|
|
* @param paperId
|
|
* @param paperId
|
|
*/
|
|
*/
|
|
- public void appendAudioTag(List<MultipartFile> files,String paperId){
|
|
|
|
- for(MultipartFile file:files){
|
|
|
|
- QuestionAudio questionAudio = questionAudioService.findByPaperIdAndFileName(paperId, file.getOriginalFilename());
|
|
|
|
- if(questionAudio == null){
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- String numbers[] = file.getOriginalFilename().split("_");
|
|
|
|
- Question question = quesRepo.findOne(questionAudio.getQuestionId());
|
|
|
|
- question.setHasAudio(true);
|
|
|
|
- if(numbers[1].equals("1")){
|
|
|
|
- //Question question = quesRepo.findOne(questionAudio.getQuestionId());
|
|
|
|
- String quesBody = question.getQuesBody();
|
|
|
|
- String quesBodyNew = quesBody.substring(0, quesBody.lastIndexOf("</p>")) + "<a id=\"" +
|
|
|
|
- file.getOriginalFilename() + "\" name=\"" + file.getOriginalFilename() + "\"></a></p>";
|
|
|
|
- question.setQuesBody(quesBodyNew);
|
|
|
|
- quesRepo.save(question);
|
|
|
|
- }else {
|
|
|
|
- //Question question = quesRepo.findOne(questionAudio.getQuestionId());
|
|
|
|
|
|
+ public void appendAudioTag(String fileName,String questionId){
|
|
|
|
+ QuestionAudio questionAudio = questionAudioService.findByQuestionIdAndFileName(questionId,fileName);
|
|
|
|
+ if(questionAudio != null){
|
|
|
|
+ String numbers[] = fileName.split("_");
|
|
|
|
+ Question question = quesRepo.findOne(questionAudio.getQuestionId());
|
|
|
|
+ question.setHasAudio(true);
|
|
|
|
+ if(numbers[1].equals("1")){
|
|
|
|
+ String quesBody = question.getQuesBody();
|
|
|
|
+ if(!quesBody.contains(fileName)){
|
|
|
|
+ String quesBodyNew = quesBody.substring(0, quesBody.lastIndexOf("</p>"))
|
|
|
|
+ + "<a id=\"" + fileName + "\" name=\"" + fileName + "\"></a></p>";
|
|
|
|
+ question.setQuesBody(quesBodyNew);
|
|
|
|
+ }
|
|
|
|
+ }else {
|
|
List<QuesOption> quesOptions = question.getQuesOptions();
|
|
List<QuesOption> quesOptions = question.getQuesOptions();
|
|
List<QuesOption> quesOptionsNew = new ArrayList<QuesOption>();
|
|
List<QuesOption> quesOptionsNew = new ArrayList<QuesOption>();
|
|
for(QuesOption quesOption:quesOptions){
|
|
for(QuesOption quesOption:quesOptions){
|
|
if(quesOption.getNumber().equals(CommonUtils.characterToNumber(numbers[2]).toString())){
|
|
if(quesOption.getNumber().equals(CommonUtils.characterToNumber(numbers[2]).toString())){
|
|
String optionBody = quesOption.getOptionBody();
|
|
String optionBody = quesOption.getOptionBody();
|
|
- String optionBodyNew = optionBody.substring(0, optionBody.lastIndexOf("</p>")) + "<a id=\"" +
|
|
|
|
- file.getOriginalFilename() + "\" name=\"" + file.getOriginalFilename() + "\"></a></p>";
|
|
|
|
- quesOption.setOptionBody(optionBodyNew);
|
|
|
|
|
|
+ if(!optionBody.contains(fileName)){
|
|
|
|
+ String optionBodyNew = optionBody.substring(0, optionBody.lastIndexOf("</p>"))
|
|
|
|
+ + "<a id=\""+fileName+"\" name=\""+fileName+"\"></a></p>";
|
|
|
|
+ quesOption.setOptionBody(optionBodyNew);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
quesOptionsNew.add(quesOption);
|
|
quesOptionsNew.add(quesOption);
|
|
}
|
|
}
|
|
question.setQuesOptions(quesOptionsNew);
|
|
question.setQuesOptions(quesOptionsNew);
|
|
- quesRepo.save(question);
|
|
|
|
}
|
|
}
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
+ quesRepo.save(question);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
}
|
|
}
|