增加文件前缀和后缀的设置,修缮部分代码规范

This commit is contained in:
2018-04-25 21:09:10 +08:00
parent ea9aa04a20
commit 53989d5502
18 changed files with 251 additions and 184 deletions

View File

@@ -5,7 +5,7 @@
<groupId>com.fjy</groupId>
<artifactId>spring</artifactId>
<version>V2.6.8</version>
<version>V2.7</version>
<packaging>jar</packaging>
<name>spring</name>

View File

@@ -127,6 +127,7 @@ public class DataController {
/**
* 存储密保问题
*
* @param userque
* @return
*/
@@ -139,6 +140,7 @@ public class DataController {
/**
* 判断密保问题是否正确正确返回true错误返回false其余反馈异常对象
*
* @param name
* @param question
* @param answer
@@ -153,10 +155,11 @@ public class DataController {
if (!userque.isPresent()) {
throw new UserException(ResultEnum.EMPTY_QUESTION);
} else if (question.equals(userque.get().getQuestion())) {
if(new BigInteger(CodingUtil.encryptSHA(answer.getBytes())).toString(32).equals(userque.get().getAnswer()))
if (new BigInteger(CodingUtil.encryptSHA(answer.getBytes())).toString(32).equals(userque.get().getAnswer())) {
return true;
else
} else {
return false;
}
} else {
throw new UserException(ResultEnum.QUESTION_ERROR);
}
@@ -195,9 +198,9 @@ public class DataController {
notice.setIssueTime(dateNowStr);
notice.setNoticeContent(content);
return noticeService.addOne(notice) != null;
}else
} else {
return false;
}
}
@PostMapping("/home/admin/addoneversion")

View File

@@ -46,8 +46,9 @@ public class DownLoadController {
@ResponseBody
public List<TbFile> toDownloadAll() {
List<TbFile> files = fileService.findAllFile();//此处做空指针判断并抛出错误
if (files != null)
if (files != null) {
return files;
}
new UserException(ResultEnum.EMPTY_DATA);
return null;
}
@@ -59,8 +60,9 @@ public class DownLoadController {
//log.info(user.toString());
List<TbFile> files = fileService.findByColuserid(user.getColuserid());
//此处做空指针判断并抛出错误
if (files != null)
if (files != null) {
return files;
}
new UserException(ResultEnum.EMPTY_DATA);
return null;
}
@@ -103,14 +105,15 @@ public class DownLoadController {
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null)
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (bos != null)
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
@@ -118,6 +121,7 @@ public class DownLoadController {
e.printStackTrace();
}
}
}
return null;
}
@@ -184,9 +188,15 @@ public class DownLoadController {
} finally {
//关闭流
try {
if (null != bufferStream) bufferStream.close();
if (null != zipStream) zipStream.close();
if (null != zipSource) zipSource.close();
if (null != bufferStream) {
bufferStream.close();
}
if (null != zipStream) {
zipStream.close();
}
if (null != zipSource) {
zipSource.close();
}
} catch (IOException e) {
e.printStackTrace();
}
@@ -209,14 +219,15 @@ public class DownLoadController {
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null)
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (bos != null)
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
@@ -225,6 +236,7 @@ public class DownLoadController {
}
}
}
}
/**
* 获取目录下所有文件的路径

View File

@@ -63,8 +63,9 @@ public class RegisterController {
@ResponseBody
public boolean doCheckStudentNo(@RequestParam(value = "studentno") String studentno){
TbStudentlist studentlist = studentService.findStudentByNo(studentno);
if (studentlist!=null)
if (studentlist!=null) {
return true;
}
return false;
}
@@ -73,8 +74,9 @@ public class RegisterController {
public boolean doCheckStudent(@RequestParam(value = "studentno") String studentno,
@RequestParam(value = "realname") String realname){
TbStudentlist studentlist = studentService.findByColstudentnoAndColrealname(studentno,realname);
if (studentlist!=null&&studentlist.getRegistered()!= RegisteredEnum.REGISTERED.getCode())
if (studentlist!=null&&studentlist.getRegistered()!= RegisteredEnum.REGISTERED.getCode()) {
return true;
}
return false;
}
@@ -87,8 +89,9 @@ public class RegisterController {
@ResponseBody
public boolean doUserName(@RequestParam(value = "name") String name){
Optional<TbUser> user = userService.findByColname(name);
if (user.isPresent())
if (user.isPresent()) {
return false;
}
return true;
}

View File

@@ -1,11 +1,13 @@
package com.fjy.spring.controller;
import com.fjy.spring.constant.GlobalConstant;
import com.fjy.spring.domain.Homework;
import com.fjy.spring.domain.TbFile;
import com.fjy.spring.domain.TbLog;
import com.fjy.spring.domain.TbUser;
import com.fjy.spring.properties.ServerProperties;
import com.fjy.spring.service.FileService;
import com.fjy.spring.service.HomeworkService;
import com.fjy.spring.service.LogService;
import com.fjy.spring.untils.FormatFileSizeUtil;
import com.fjy.spring.untils.GetIPAddrUtil;
@@ -31,11 +33,20 @@ import java.util.Map;
@Slf4j
public class UpLoadController {
/**
* 服务器配置信息
*/
@Autowired
private ServerProperties serverProperties;//服务器配置信息
private ServerProperties serverProperties;
/**
* 文件相关数据库操作
*/
@Autowired
private FileService fileService;
@Autowired
private FileService fileService;//文件相关数据库操作
private HomeworkService homeworkService;
@Autowired
private LogService logService;
@@ -79,20 +90,22 @@ public class UpLoadController {
* 存储文件信息
*/
TbFile file = new TbFile();
file.setColfilesize(new FormatFileSizeUtil().GetFileSize(imageFile.getSize()));
file.setColfilesize(FormatFileSizeUtil.GetFileSize(imageFile.getSize()));
file.setColfilename(filename);
file.setColfilepath(uploadUrl + filename);
file.setColip(request.getRemoteAddr());
file.setColuserid(user.getColuserid());
if (fileService.addFile(file))
if (fileService.addFile(file)) {
log.info("记录写入数据库成功");
}
//System.out.println("记录写入数据库成功");
else
else {
log.error("记录写入数据库失败");
}
//System.out.println("记录写入数据库失败");
log.info("文件上传到: " + uploadUrl + filename);
log.info("文件大小: " + new FormatFileSizeUtil().GetFileSize(imageFile.getSize()));
log.info("文件大小: " + FormatFileSizeUtil.GetFileSize(imageFile.getSize()));
log.info("文件名: " + filename);
File targetFile = new File(uploadUrl + filename);
@@ -130,6 +143,7 @@ public class UpLoadController {
public void moreUpload(HttpServletRequest request,
@RequestParam(value = "courseName", required = false) String courseName,
@RequestParam(value = "folder", required = false) String folder,
@RequestParam(value = "workid") Integer workId,
@RequestParam(value = "rename", required = true) boolean rename) {
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
@@ -158,13 +172,21 @@ public class UpLoadController {
List<String> fileList = new ArrayList<String>();
for (MultipartFile file : files.values()) {
Homework homework = homeworkService.findById(workId);
String filePrefix = homework.getFilePrefix();
String fileSuffix = homework.getFileSuffix();
String filename = file.getOriginalFilename();
String suffix = "." + filename.substring(filename.lastIndexOf(".") + 1);//获取文件后缀
//获取文件后缀
String suffix = "." + filename.substring(filename.lastIndexOf(".") + 1);
TbFile tbFile = new TbFile();
String pathname;
//文件重命名
if (rename) {
pathname = uploadUrl + user.getColstudentno() + user.getColrealname() + suffix;
tbFile.setColfilename(user.getColstudentno() + user.getColrealname() + suffix);
pathname = uploadUrl + filePrefix + user.getColstudentno() + user.getColrealname() + fileSuffix + suffix;
tbFile.setColfilename(filePrefix + user.getColstudentno() + user.getColrealname() + fileSuffix + suffix);
} else {
pathname = uploadUrl + filename;
tbFile.setColfilename(filename);
@@ -183,10 +205,11 @@ public class UpLoadController {
TbFile file1 = fileService.findByFilepath(pathname);
file1.setColfilepath(bakpathname + "." + dateNowStr2 + ".bak");
file1.setColfilename(file1.getColfilename() + "." + dateNowStr2 + ".bak");
if (fileService.addFile(file1))
if (fileService.addFile(file1)) {
log.info("重命名文件数据库更新成功");
else
} else {
log.error("重命名文件数据库更新失败");
}
File mvfile = new File(bakpathname + "." + dateNowStr2 + ".bak");
try {
FileUtils.moveFile(targetFile, mvfile);
@@ -197,14 +220,15 @@ public class UpLoadController {
}
log.info("文件上传到: " + uploadUrl + filename);
log.info("文件大小: " + new FormatFileSizeUtil().GetFileSize(file.getSize()));
log.info("文件大小: " + FormatFileSizeUtil.GetFileSize(file.getSize()));
log.info("文件名: " + filename);
tbFile.setColfilesize(new FormatFileSizeUtil().GetFileSize(file.getSize()));
tbFile.setColfilesize(FormatFileSizeUtil.GetFileSize(file.getSize()));
tbFile.setColtime(dateNowStr);
tbFile.setColrealname(filename);
tbFile.setColfilepath(pathname);//文件自动学号+姓名命名
//文件自动学号+姓名命名
tbFile.setColfilepath(pathname);
tbFile.setColip(request.getRemoteAddr());
tbFile.setColuserid(user.getColuserid());
tbFile.setCourseName(courseName);
@@ -217,10 +241,11 @@ public class UpLoadController {
logs.setColheader(user.getColname() + "上传了'" + filename + "'文件");
logService.addLogRec(logs);
if (fileService.addFile(tbFile))
if (fileService.addFile(tbFile)) {
log.info("记录写入数据库成功");
else
} else {
log.error("记录写入数据库失败");
}
if (!targetFile.exists()) {
try {

View File

@@ -30,4 +30,10 @@ public class Homework {
@Column(name = "workremark")
private String Remark;
@Column(name = "pre")
private String filePrefix;
@Column(name = "suf")
private String fileSuffix;
}

View File

@@ -24,7 +24,8 @@ public class AdminInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
TbUser user = (TbUser)request.getSession().getAttribute(GlobalConstant.USER_SESSION_KEY);
//log.info(user.getColuserid()+"");
if (adminService == null) {//解决service为null无法注入问题
//解决service为null无法注入问题
if (adminService == null) {
BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
adminService = (AdminService) factory.getBean("adminService");
}
@@ -33,8 +34,9 @@ public class AdminInterceptor implements HandlerInterceptor {
if (!admin.isPresent()){
response.sendRedirect("/cms/home");
return false;
}else
}else {
return true;
}
}
}

View File

@@ -15,15 +15,17 @@ public class FileService {
public boolean addFile(TbFile tbFile) {
TbFile file = tbFileRepository.save(tbFile);
if (file != null)
if (file != null) {
return true;
}
return false;
}
public List<TbFile> findFile(TbFile tbFile){
List<TbFile> files = tbFileRepository.findByColfilename(tbFile.getColfilename());
if (files!=null)
if (files!=null) {
return files;
}
return null;
}

View File

@@ -2,13 +2,13 @@ package com.fjy.spring.service;
import com.fjy.spring.domain.Homework;
import com.fjy.spring.domain.VHomework;
import com.fjy.spring.domain.VWorkDetail;
import com.fjy.spring.repository.HomeworkRepository;
import com.fjy.spring.repository.VHomeworkRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class HomeworkService {
@@ -25,4 +25,12 @@ public class HomeworkService {
public List<VHomework> findAllVHomework(){
return vHomeworkRepository.findAll();
}
public Homework findById(Integer id){
Optional<Homework> homework = homeworkRepository.findById(id);
if (homework.isPresent()){
return (Homework)homework.get();
}
return null;
}
}

View File

@@ -81,8 +81,9 @@ public class UserService {
public boolean addUserQue(TbUserque userque) {
TbUserque tbUserque = userqueRepository.save(userque);
if (tbUserque != null)
if (tbUserque != null) {
return true;
}
return false;
}

View File

@@ -1,4 +1,4 @@
#控制配置文件调用
spring:
profiles:
active: prod
active: dev

View File

@@ -25,8 +25,8 @@
<!--滚动策略-->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--路径文件名,文件名包含时间-->
<!--<fileNamePattern>F:\JAVA Workspace\Temp\log\%d\info.%d.log</fileNamePattern>-->
<fileNamePattern>/www/cmsfile/%d/info.%d.log</fileNamePattern>
<fileNamePattern>F:\JAVA Workspace\Temp\log\%d\info.%d.log</fileNamePattern>
<!--<fileNamePattern>/www/cmsfile/%d/info.%d.log</fileNamePattern>-->
</rollingPolicy>
</appender>
@@ -44,8 +44,8 @@
<!--滚动策略-->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--路径文件名,文件名包含时间-->
<!--<fileNamePattern>F:\JAVA Workspace\Temp\log\error.%d.log</fileNamePattern>-->
<fileNamePattern>/www/cmsfile/%d/error.%d.log</fileNamePattern>
<fileNamePattern>F:\JAVA Workspace\Temp\log\error.%d.log</fileNamePattern>
<!--<fileNamePattern>/www/cmsfile/%d/error.%d.log</fileNamePattern>-->
</rollingPolicy>
</appender>

View File

@@ -3,6 +3,7 @@ var month = dt.getMonth() + 1;
var day = dt.getDate();
var year = dt.getFullYear();
var cur = year + '-' + month + '-' + day;
function DateDiff(sDate1, sDate2) { //sDate1和sDate2是2002-12-18格式
var aDate, oDate1, oDate2, iDays
aDate = sDate1.split("-")
@@ -12,9 +13,11 @@ function DateDiff(sDate1, sDate2) { //sDate1和sDate2是2002-12-18格式
iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24) //把相差的毫秒数转换为天数
return iDays
}
function displayStyle(id, type) {
document.getElementById(id).style.display = type;
}
var Main = {
data() {
var checkName = (rule, value, callback) => {
@@ -232,7 +235,7 @@ var Main = {
});
},
uploadURL(row) {
return getRootPath_web()+"/home/moreUpload?courseName=" + row.coursename + "&folder=" + row.workfolder+"&rename=true";
return getRootPath_web() + "/home/moreUpload?courseName=" + row.coursename + "&folder=" + row.workfolder + "&workid=" + row.workid + "&rename=true";
},
limitTime(row) {
@@ -293,7 +296,9 @@ var Main = {
let that = this;
this.$refs.upload.submit();
this.openNotiSuccess("成功", "文件上传成功!");
setTimeout(function () {that.getFileList();},1000);
setTimeout(function () {
that.getFileList();
}, 1000);
},
handleRemove(file, fileList) {
console.log(file, fileList);

View File

@@ -6,7 +6,7 @@ import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/*
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
@@ -22,4 +22,4 @@ public class LoggerTest {
log.error("error...");
}
}
*/

View File

@@ -12,7 +12,7 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import javax.transaction.Transactional;
/*
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@@ -21,11 +21,11 @@ public class DataControllerTest {
@Autowired
private MockMvc mvc;
*/
/**
* 使用此单元测试前要注销拦截器,否则测试不通过
* @throws Exception
*//*
*/
@Test
@Transactional
@@ -37,11 +37,11 @@ public class DataControllerTest {
.andExpect(MockMvcResultMatchers.content().string("true"));
}
*/
/**
* 测试找回密码
* @throws Exception
*//*
*/
@Test
public void findUserQue() throws Exception {
@@ -82,11 +82,11 @@ public class DataControllerTest {
"}"));
}
*/
/**
* 测试忘记密码操作
/* * 测试忘记密码操作
* @throws Exception
*//*
*/
@Test
@Transactional
@@ -98,4 +98,4 @@ public class DataControllerTest {
.param("password", "admin"))
.andExpect(MockMvcResultMatchers.content().string("true"));
}
}*/
}

View File

@@ -12,7 +12,7 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import static org.junit.Assert.*;
/*
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@@ -40,4 +40,4 @@ public class LoginControllerTest {
" \"data\": null\n" +
"}"));
}
}*/
}

View File

@@ -12,7 +12,7 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import static org.junit.Assert.*;
/*
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@@ -74,4 +74,4 @@ public class RegisterControllerTest {
.param("name","root1"))
.andExpect(MockMvcResultMatchers.content().string("true"));
}
}*/
}

View File

@@ -11,7 +11,7 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import static org.junit.Assert.*;
/*
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@@ -39,4 +39,4 @@ public class TestControllerTest {
mvc.perform(MockMvcRequestBuilders.get("/test/id/1"))
.andExpect(MockMvcResultMatchers.status().isOk());
}
}*/
}