开发工具:IDEA,mysql5.7
Tomcat8.0,jdk1.8
package cn.tangtj.clouddisk.web;
import cn.tangtj.clouddisk.entity.UploadFile;
import cn.tangtj.clouddisk.entity.User;
import cn.tangtj.clouddisk.entity.vo.Guest;
import cn.tangtj.clouddisk.entity.vo.ShareFile;
import cn.tangtj.clouddisk.service.FileService;
import cn.tangtj.clouddisk.service.UserService;
import cn.tangtj.clouddisk.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.ServletContext;
import java.io.File;
@Controller
@RequestMapping("/shareFile")
@SessionAttributes({"guest"})
public class ShareFileController {
private final String fileSavePath;
private final static String fileSaveDir = "upload";
private final FileService fileService;
private final UserService userService;
@Autowired
public ShareFileController(FileService fileService, UserService userService,ServletContext servletContext) {
this.fileService = fileService;
this.userService = userService;
fileSavePath = servletContext.getRealPath("") + fileSaveDir + File.separator;
}
@RequestMapping(method = RequestMethod.GET)
public String shareUi(Model model) {
// Guest guest = new Guest();
// guest.setDownloadTime(0);
// model.addAttribute("guest", guest);
return "shareFileIndex";
}
@RequestMapping(value = "/{shareCode}", method = RequestMethod.POST)
@ResponseBody
public ShareFile shareFile(@SessionAttribute Guest guest, @PathVariable("shareCode") String shareCode) {
if (guest == null) {
return null;
}
UploadFile fileInfo = fileService.findByShareCode(shareCode);
User user = userService.findById(fileInfo.getUserId());
ShareFile shareFileInfo = new ShareFile();
shareFileInfo.setFileName(fileInfo.getFileName());
shareFileInfo.setOwnName(user.getUsername());
shareFileInfo.setShareCode(shareCode);
return shareFileInfo;
}
@RequestMapping(value = "/{shareCode}/download")
public ResponseEntity<byte[]> shareDownload(@SessionAttribute Guest guest, @PathVariable("shareCode") String shareCode) {
// if (guest == null) {
// return null;
// }
// if (guest.getDownloadTime() >= 10) {
// return null;
// }
// guest.setDownloadTime(guest.getDownloadTime() + 1);
UploadFile fileInfo = fileService.findByShareCode(shareCode);
return FileUtil.createResponseEntityByFileInfo(fileInfo, fileSavePath);
}
}
package cn.tangtj.clouddisk.web;
import cn.tangtj.clouddisk.entity.User;
import cn.tangtj.clouddisk.entity.vo.SignUpNameCheckResult;
import cn.tangtj.clouddisk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping(“/signUp”)
class SignUpController {
private final UserService userService;
@Autowired
public SignUpController(UserService userService) {
this.userService = userService;
}
@RequestMapping(method = RequestMethod.GET)
public String singUp(){
return "signUp";
}
@RequestMapping(value = "/checkUserName", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
@ResponseBody
public String checkUserName(String userName){
SignUpNameCheckResult result = new SignUpNameCheckResult();
if (check(userName)) {
User user = userService.findByName(userName);
if (user == null) {
result.setUsable(true);
result.setMsg("用户名可用");
} else {
result.setUsable(false);
result.setMsg("当前用户名不可用");
}
} else {
result.setUsable(false);
result.setMsg("用户名不符合规范");
}
return "{\"usable\":\"" + result.isUsable() +"\",\"msg\":\""+result.getMsg()+"\"}";
}
@RequestMapping(method = RequestMethod.POST)
public String regUser(String username,String password,String reuserpwd,Model model){
if (!check(username)){
model.addAttribute("errorNameMsg", "用户名不符合规范");
return "signUp";
}
if (!check(password)){
model.addAttribute("errorPwdMsg", "密码不符合规范");
return "signUp";
}
if (!password.equals(reuserpwd)){
model.addAttribute("signUpError", "两次密码不一致");
return "signUp";
}
User user = userService.findByName(username);
if (user == null){
User insert = new User();
insert.setUsername(username);
insert.setPassword(password);
User result = userService.save(insert);
if (result != null){
model.addAttribute("signUpMsg","注册成功");
}else{
model.addAttribute("signUpError","注册失败,请重新注册");
}
return "signUp";
}else {
model.addAttribute("signUpError", "注册失败,用户名已被使用");
}
return "signUp";
}
public boolean check(String str){
return str.matches("[0-9a-zA-Z]{5,10}");
}
}
源码获取:底部gongzhonghao,022领取下载链接