很多同学都有自己的网盘,方便存储一些java学习教程。该毕业设计实现了一个简易的网盘,包含文件上传和文件分享等功能。
后端技术采用了spring,spring mvc,JPA,前端采用了thymeleaf模板语言。整体业务功能相对不是太复杂,亮点是文件分享功能。
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
* 登录
* 管理员admin 密码123 权限0(最高)
*
*/
@Controller
public class LoginController {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private IUserService userService;
@Autowired
private IVerifyCodeService iVerifyCodeService;
// 登录
@RequestMapping(value = "/login", method = RequestMethod.POST)
@ResponseBody
}
if (!"null".equals(b)) {
j.setSuccess(true);
j.setMsg(b);
} else {
j.setMsg("提取码生成失败!");
}
return j;
}
// 文件、文件夹 移动 文件夹移动时fileName=@dir@
@RequestMapping(value = "/filemove", produces = "application/json; charset=utf-8")
@ResponseBody
public ResponseMsg fileMove(String fileName, String oldPath, String newPath, HttpServletRequest request) {
if (fileName == null) {
}
}
登录业务控制器:
/**
* 登录
* 管理员admin 密码123 权限0(最高)
*
*/
@Controller
public class LoginController {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private IUserService userService;
@Autowired
private IVerifyCodeService iVerifyCodeService;
// 登录
@RequestMapping(value = "/login", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> login(HttpServletRequest request, HttpServletResponse response) {
Map<String, Object> map = new HashMap<>();
String userName = request.getParameter("userName");
String password = request.getParameter("password");
String encryptedPwd = "";
try {
//加密的用户密码
try {
//加密的用户密码
encryptedPwd = Md5SaltTool.getEncryptedPwd(password);
} catch (NoSuchAlgorithmException e) {
logger.error("NoSuchAlgorithmException:", e);
} catch (UnsupportedEncodingException e) {
logger.error("UnsupportedEncodingException:", e);
}
User dataBaseUser = userService.queryUserByUsername(encryptedPwd);
//regcode的重新写入
if (!iVerifyCodeService.isValid(regcode)) {
logger.info("注册失败,激活码失效或不正确!");
map.put("result", "2");
return map;
} else {
if (dataBaseUser == null) {
User user = new User(userName, encryptedPwd, "0", email, phone, alias);
userService.add(user);
logger.info("账号注册成功!");
map.put("result", "1");
} else {
logger.info("用户已经存在,请登录或换一个用户名!");
map.put("result", "0");
}
return map;
}
logger.warn("downloadLink:" + downloadLink);
boolean b = false;
ResponseMsg responseMsg = new ResponseMsg();
User user = (User) request.getSession().getAttribute("user");
if (user == null) {
responseMsg.setSuccess(false);
responseMsg.setMsg("未登录");
} else {
String userName = user.getUserName();
if (path == null) {
path = "/";
}
logger.warn(userName + " " + downloadLink + " " + path);
b = fileService.copyFileToMyPan(userName, downloadLink, path);
responseMsg.setSuccess(b);
if (b == false) {
responseMsg.setMsg("保存失败");
} else {
responseMsg.setMsg("保存成功");
}
}
return responseMsg;
}
/**
* 下载客户端的apk
path = "/";
}
ResponseMsgAdd j = new ResponseMsgAdd();
if (fileName.isEmpty()) {
j.setMsg("文件名字为空!");
return j;
}
// 获取用户名
String userName = WebUtil.getUserNameByRequest(request);
// String userName ="zc";
// 下载文件,获取下载路径,这个是 个映射的路径
String link = fileService.download(fileName, userName, path);
try {
//这里校验要填真实的路经
String newLink = link.replace("/data/", fileRootPath);
String[] md5Array = FileSplit.splitBySizeSubSection(newLink, size,
fileRootPath + "/tempMd5/" + userName + "/");
j.setObj(md5Array);
} catch (Exception e) {
logger.error("Exception:", e);
j.setObj("");
}
if (!link.isEmpty()) {
j.setSuccess(true);
if (path == null) {
path = "/";
}
ResponseMsg j = new ResponseMsg();
if (dirName.isEmpty() || path.isEmpty()) {
j.setMsg("文件夹名字为空!");
return j;
}
// 获取用户名
String userName = WebUtil.getUserNameByRequest(request);
// path = /pan/userName/当前path
if (!SystemUtil.isWindows()) {
path = "/pan/" + userName + path;
} else {
path = fileRootPath + userName + path;
}
User user = (User) request.getSession().getAttribute("user");
String userName = user.getUserName();
List<ShareMessage> shareMessageList = new ArrayList<>();
List<LinkSecret> linkSecretList = linkSecretService.findLinkSecretsByUserName(userName);
for (int i = 0; i < linkSecretList.size(); i++) {
LinkSecret linkSecret = linkSecretList.get(i);
ShareMessage shareMessage = new ShareMessage();
shareMessage.setDownloadNum(linkSecret.getDownloadNum());
shareMessage.setExpireDate(linkSecret.getExpireDate());
shareMessage.setFileName(linkSecret.getLocalLink().substring(linkSecret.getLocalLink().lastIndexOf("/")));
shareMessage.setDownloadName(null);
shareMessageList.add(shareMessage);
}
return shareMessageList;
public ResponseMsgAdd download(@RequestParam String fileName, String path, HttpServletRequest request) {
if (path == null) {
path = "/";
}
ResponseMsgAdd j = new ResponseMsgAdd();
if (fileName.isEmpty()) {
j.setMsg("文件名字为空!");
return j;
}
// 获取用户名
String userName = WebUtil.getUserNameByRequest(request);
// String userName ="zc";
// 下载文件,获取下载路径,这个是 个映射的路径
String link = fileService.download(fileName, userName, path);
try {
//这里校验要填真实的路经
String newLink = link.replace("/data/", fileRootPath);
String[] md5Array = FileSplit.splitBySizeSubSection(newLink, size,
fileRootPath + "/tempMd5/" + userName + "/");
j.setObj(md5Array);
} catch (Exception e) {
logger.error("Exception:", e);
* filename:下载apk的名字
* downloadPath:下载的文件夹,放在/root/pan/share目录中
*/
@RequestMapping(value = "/downloadApk", produces = {"application/json; charset=UTF-8"})
@ResponseBody
public ResponseMsg shareToMyPan(HttpServletRequest request, HttpServletResponse response, String filename,
String downloadPath) throws FileNotFoundException, UnsupportedEncodingException {
// 读到流中
// String filePath="F:/"+downloadPath+"/"+filename;//window上测试的路径
ResponseMsg responseMsg = new ResponseMsg();
String filePath = fileRootPath + downloadPath + "/" + filename;
InputStream inStream = new FileInputStream(filePath);
if (!new File(filePath).exists()) {
responseMsg.setMsg("找不到文件");
}
// 设置输出的格式
String agent = request.getHeader("user-agent");
String fileName4 = "";
if (agent.contains("Firefox")) {
fileName4 = new String(filename.getBytes(), "iso-8859-1");
} else {