实现对文件是否重命名的控制,目前设定为管理员上传的文件不会重命名,完善日志输出存储
This commit is contained in:
@@ -4,6 +4,7 @@ import com.fjy.spring.domain.TbFile;
|
||||
import com.fjy.spring.enums.ResultEnum;
|
||||
import com.fjy.spring.exception.UserException;
|
||||
import com.fjy.spring.service.FileService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
@@ -14,6 +15,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import java.io.File;
|
||||
|
||||
@Controller
|
||||
@Slf4j
|
||||
public class DeleteController {
|
||||
@Autowired
|
||||
private FileService fileService;//文件相关数据库操作
|
||||
@@ -37,7 +39,7 @@ public class DeleteController {
|
||||
TbFile resfile = fileService.findFileById(tbFile);
|
||||
File filepath = new File(resfile.getColfilepath());
|
||||
if (!filepath.exists()) {
|
||||
System.out.println("删除文件失败:" + resfile.getColfilename() + "不存在!");
|
||||
log.error("删除文件失败:" + resfile.getColfilename() + "不存在!");
|
||||
} else {
|
||||
if (filepath.isFile()){
|
||||
deleteFile(resfile.getColfilepath(),resfile.getColfileid());
|
||||
@@ -60,14 +62,14 @@ public class DeleteController {
|
||||
if (file.exists() && file.isFile()) {
|
||||
if (file.delete()) {
|
||||
fileService.deleteFileById(tbFile);
|
||||
System.out.println("删除单个文件" + fileName + "成功!");
|
||||
log.info("删除单个文件" + fileName + "成功!");
|
||||
return true;
|
||||
} else {
|
||||
System.out.println("删除单个文件" + fileName + "失败!");
|
||||
log.info("删除单个文件" + fileName + "失败!");
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
System.out.println("删除单个文件失败:" + fileName + "不存在!");
|
||||
log.info("删除单个文件失败:" + fileName + "不存在!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.fjy.spring.enums.ResultEnum;
|
||||
import com.fjy.spring.exception.UserException;
|
||||
import com.fjy.spring.properties.ServerProperties;
|
||||
import com.fjy.spring.service.FileService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
@@ -55,7 +56,7 @@ public class DownLoadController {
|
||||
@ResponseBody
|
||||
public List<TbFile> toDownloadOne(){
|
||||
TbUser user = (TbUser)request.getSession().getAttribute(USER_SESSION_KEY);
|
||||
System.out.println(user.toString());
|
||||
//log.info(user.toString());
|
||||
List<TbFile> files = fileService.findByColuserid(user.getColuserid());
|
||||
//此处做空指针判断并抛出错误
|
||||
if (files!=null)
|
||||
@@ -81,12 +82,12 @@ public class DownLoadController {
|
||||
TbFile tbFile = fileService.findFileById(file);
|
||||
//TbFile tbFile = fileService.findFile(file);
|
||||
|
||||
System.out.println(tbFile.getColfilepath());
|
||||
//System.out.println(tbFile.getColfilepath());
|
||||
|
||||
String ctxPath = tbFile.getColfilepath();
|
||||
String downLoadPath = ctxPath;
|
||||
//String downLoadPath = ctxPath + tbFile.getColfilename();
|
||||
System.out.println(downLoadPath);
|
||||
//System.out.println(downLoadPath);
|
||||
try {
|
||||
long fileLength = new File(downLoadPath).length();
|
||||
response.setContentType("application/x-msdownload;");
|
||||
@@ -146,7 +147,7 @@ public class DownLoadController {
|
||||
// 要生成的压缩文件地址和文件名称
|
||||
String zipFileName=courseName+folder+".zip";
|
||||
String desPath = zipPath+"\\"+zipFileName;
|
||||
System.out.println("打包文件存储地址:"+desPath);
|
||||
//System.out.println("打包文件存储地址:"+desPath);
|
||||
|
||||
File zipFile = new File(desPath);
|
||||
ZipOutputStream zipStream = null;
|
||||
@@ -244,13 +245,13 @@ public class DownLoadController {
|
||||
if (f.isFile()) {
|
||||
fileList.add(f);
|
||||
} else if (f.isDirectory()) {
|
||||
System.out.println(f.getAbsolutePath());
|
||||
//log.info(f.getAbsolutePath());
|
||||
path.add(f.getAbsolutePath());
|
||||
getFileString(f.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
for (File f1 : fileList) {
|
||||
System.out.println(f1.getName());
|
||||
//System.out.println(f1.getName());
|
||||
path.add(f1.getAbsolutePath());
|
||||
/*name[j]=f1.getName();j++;*/
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.fjy.spring.domain.TbUser;
|
||||
import com.fjy.spring.properties.ServerProperties;
|
||||
import com.fjy.spring.service.FeedBackService;
|
||||
import com.fjy.spring.untils.RedirectUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
@@ -19,6 +20,7 @@ import java.util.Date;
|
||||
import static com.fjy.spring.constant.GlobalConstant.USER_SESSION_KEY;
|
||||
|
||||
@RestController
|
||||
@Slf4j
|
||||
public class FeedBackController {
|
||||
|
||||
@Autowired
|
||||
@@ -42,7 +44,7 @@ public class FeedBackController {
|
||||
feedBack.setUserid(user.getColuserid());
|
||||
RedirectUtil red = new RedirectUtil();
|
||||
if (feedBackService.addContent(feedBack)){
|
||||
System.out.println("写入数据库成功");
|
||||
log.info("反馈信息写入数据库成功");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import com.fjy.spring.properties.ServerProperties;
|
||||
import com.fjy.spring.service.FileService;
|
||||
import com.fjy.spring.service.LogService;
|
||||
import com.fjy.spring.untils.FormatFileSizeUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@@ -25,6 +26,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Controller
|
||||
@Slf4j
|
||||
public class UpLoadController {
|
||||
|
||||
@Autowired
|
||||
@@ -91,13 +93,15 @@ public class UpLoadController {
|
||||
file.setColip(request.getRemoteAddr());
|
||||
file.setColuserid(user.getColuserid());
|
||||
if (fileService.addFile(file))
|
||||
System.out.println("记录写入数据库成功");
|
||||
log.info("记录写入数据库成功");
|
||||
//System.out.println("记录写入数据库成功");
|
||||
else
|
||||
System.out.println("记录写入数据库失败");
|
||||
log.error("记录写入数据库失败");
|
||||
//System.out.println("记录写入数据库失败");
|
||||
|
||||
System.out.println("文件上传到: " + uploadUrl + filename);
|
||||
System.out.println("文件大小: " + new FormatFileSizeUtil().GetFileSize(imageFile.getSize()));
|
||||
System.out.println("文件名: " + filename);
|
||||
log.info("文件上传到: " + uploadUrl + filename);
|
||||
log.info("文件大小: " + new FormatFileSizeUtil().GetFileSize(imageFile.getSize()));
|
||||
log.info("文件名: " + filename);
|
||||
|
||||
File targetFile = new File(uploadUrl + filename);
|
||||
if (!targetFile.exists()) {
|
||||
@@ -133,7 +137,8 @@ public class UpLoadController {
|
||||
@RequestMapping("/moreUpload")
|
||||
public void moreUpload(HttpServletRequest request,
|
||||
@RequestParam(value = "courseName",required = false) String courseName,
|
||||
@RequestParam(value = "folder",required = false) String folder) {
|
||||
@RequestParam(value = "folder",required = false) String folder,
|
||||
@RequestParam(value = "rename",required = true) boolean rename) {
|
||||
|
||||
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
|
||||
Map<String, MultipartFile> files = multipartHttpServletRequest.getFileMap();
|
||||
@@ -142,8 +147,14 @@ public class UpLoadController {
|
||||
Date date = new Date();
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
String dateNowStr = sdf.format(date);
|
||||
String uploadUrl;
|
||||
//String uploadUrl = request.getSession().getServletContext().getRealPath("/") + "upload/";
|
||||
String uploadUrl = serverProperties.getFilePath()+ "upload/"+courseName+"/"+folder+"/";
|
||||
if (rename){
|
||||
uploadUrl = serverProperties.getFilePath()+ "upload/"+courseName+"/"+folder+"/";
|
||||
}else {
|
||||
uploadUrl = serverProperties.getFilePath()+ "upload/";
|
||||
}
|
||||
|
||||
|
||||
File dir = new File(uploadUrl);
|
||||
if (!dir.exists()) {
|
||||
@@ -155,35 +166,43 @@ public class UpLoadController {
|
||||
for (MultipartFile file : files.values()) {
|
||||
String filename = file.getOriginalFilename();
|
||||
String suffix = "."+filename.substring(filename.lastIndexOf(".") + 1);//获取文件后缀
|
||||
|
||||
File targetFile = new File(uploadUrl + user.getColstudentno()+user.getColrealname()+suffix);
|
||||
|
||||
System.out.println("文件上传到: " + uploadUrl + filename);
|
||||
System.out.println("文件大小: " + new FormatFileSizeUtil().GetFileSize(file.getSize()));
|
||||
System.out.println("文件名: " + filename);
|
||||
|
||||
TbFile tbFile = new TbFile();
|
||||
String pathname;
|
||||
if (rename){
|
||||
pathname = uploadUrl + user.getColstudentno()+user.getColrealname()+suffix;
|
||||
tbFile.setColfilename(user.getColstudentno()+user.getColrealname()+suffix);
|
||||
}else {
|
||||
pathname = uploadUrl + filename;
|
||||
tbFile.setColfilename(filename);
|
||||
}
|
||||
File targetFile = new File(pathname);
|
||||
|
||||
log.info("文件上传到: " + uploadUrl + filename);
|
||||
log.info("文件大小: " + new FormatFileSizeUtil().GetFileSize(file.getSize()));
|
||||
log.info("文件名: " + filename);
|
||||
|
||||
|
||||
tbFile.setColfilesize(new FormatFileSizeUtil().GetFileSize(file.getSize()));
|
||||
tbFile.setColfilename(user.getColstudentno()+user.getColrealname()+suffix);
|
||||
|
||||
tbFile.setColtime(dateNowStr);
|
||||
tbFile.setColrealname(filename);
|
||||
tbFile.setColfilepath(uploadUrl + user.getColstudentno()+user.getColrealname()+suffix);//文件自动学号+姓名命名
|
||||
tbFile.setColfilepath(pathname);//文件自动学号+姓名命名
|
||||
tbFile.setColip(request.getRemoteAddr());
|
||||
tbFile.setColuserid(user.getColuserid());
|
||||
tbFile.setCourseName(courseName);
|
||||
tbFile.setWorkFolder(folder);
|
||||
|
||||
TbLog log = new TbLog();
|
||||
log.setUserid(user.getColuserid());
|
||||
log.setColtime(dateNowStr);
|
||||
log.setColip(httpServletRequest.getRemoteAddr());
|
||||
log.setColheader(user.getColname()+"上传了"+filename+"文件");
|
||||
logService.addLogRec(log);
|
||||
TbLog logs = new TbLog();
|
||||
logs.setUserid(user.getColuserid());
|
||||
logs.setColtime(dateNowStr);
|
||||
logs.setColip(httpServletRequest.getRemoteAddr());
|
||||
logs.setColheader(user.getColname()+"上传了'"+filename+"'文件");
|
||||
logService.addLogRec(logs);
|
||||
|
||||
if (fileService.addFile(tbFile))
|
||||
System.out.println("记录写入数据库成功");
|
||||
log.info("记录写入数据库成功");
|
||||
else
|
||||
System.out.println("记录写入数据库失败");
|
||||
log.error("记录写入数据库失败");
|
||||
|
||||
if (!targetFile.exists()) {
|
||||
try {
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.fjy.spring.exception.UserException;
|
||||
import com.fjy.spring.properties.ServerProperties;
|
||||
import com.fjy.spring.service.UserService;
|
||||
import com.fjy.spring.untils.CodingUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.validation.BindingResult;
|
||||
@@ -16,6 +17,7 @@ import javax.servlet.http.HttpServletRequest;
|
||||
import java.math.BigInteger;
|
||||
|
||||
@Controller
|
||||
@Slf4j
|
||||
public class UpdateController {
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
@@ -36,7 +38,7 @@ public class UpdateController {
|
||||
tbUser.setColpassword(new BigInteger(CodingUtil.encryptSHA(tbUser.getColpassword().getBytes())).toString(32));
|
||||
}
|
||||
if (userService.doRegisterService(tbUser)){
|
||||
System.out.println(tbUser.getColname()+" 信息更新成功");
|
||||
log.info(tbUser.getColname()+" 信息更新成功");
|
||||
return "redirect:" + request.getScheme() + "://" + request.getServerName() + ":"
|
||||
+ serverProperties.getPortNum() + request.getContextPath() + "/home/user";
|
||||
// return "login";
|
||||
|
||||
Reference in New Issue
Block a user