Java项⽬:⽹盘系统设计和实现(java+ssm+jpa)源码获取:博客⾸页 "资源" ⾥下载!
很多同学都有⾃⼰的⽹盘,⽅便存储⼀些java学习教程。该毕业设计实现了⼀个简易的⽹盘,包含⽂件上传和⽂件分享等功能。
后端技术采⽤了spring,spring mvc,JPA,前端采⽤了thymeleaf模板语⾔。整体业务功能相对不是太复杂,亮点是⽂件分享功能。
分享操作业务:
@Controller
public class ShareController {
private Logger logger = Class());
@Autowired
private LinkSecretServiceImpl linkSecretService;
@Autowired
private FileServiceImpl fileService;
private SaveServiceImpl saveService;
/**
* 安卓上查看分享记录的接⼝
*
* @return
*/
@RequestMapping(value = "/shareRecord", produces = "application/json; charset=utf-8")
@ResponseBody
public List<ShareMessage> shareRecord(HttpServletRequest request) {
User user = (User) Session().getAttribute("user");
String userName = UserName();
List<ShareMessage> shareMessageList = new ArrayList<>();
List<LinkSecret> linkSecretList = linkSecretService.findLinkSecretsByUserName(userName);
for (int i = 0; i < linkSecretList.size(); i++) {
LinkSecret linkSecret = (i);
ShareMessage shareMessage = new ShareMessage();
shareMessage.DownloadNum());
shareMessage.ExpireDate());
shareMessage.LocalLink().LocalLink().lastIndexOf("/")));
shareMessage.setDownloadName(null);
shareMessageList.add(shareMessage);
}
return shareMessageList;
}
//保存到⽹盘-----link是加密的链接 downloadLink解密后的链接/data/share/zc2/Fuck.java, path:保存路径--是⽤户名后⾯的路径 @RequestMapping(value = "/shareToMyPan")
@ResponseBody
public ResponseMsg shareToMyPan(HttpServletRequest request, String path, String link) {
String downloadLink = "";
if (ains("/data/share")) {
downloadLink = link;
} else {
downloadLink = fileService.fileShareCodeDecode(link);
}
logger.warn("downloadLink:" + downloadLink);
boolean b = false;
ResponseMsg responseMsg = new ResponseMsg();
User user = (User) Session().getAttribute("user");
if (user == null) {
responseMsg.setSuccess(false);
responseMsg.setMsg("未登录");
} else {
String userName = UserName();
if (path == null) {
path = "/";
}
logger.warn(userName + " " + downloadLink + " " + path);
b = pyFileToMyPan(userName, downloadLink, path);
responseMsg.setSuccess(b);
if (b == false) {
responseMsg.setMsg("保存失败");
} else {
responseMsg.setMsg("保存成功");
}
}
return responseMsg;
}
/**
* 下载客户端的apk
* 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 = Header("user-agent");
String fileName4 = "";
if (ains("Firefox")) {
fileName4 = new Bytes(), "iso-8859-1");
} else {
//空格,(,),;,@,#,&,逗号在⾕歌浏览器中出现乱码
fileName4 = de(filename, "UTF-8").replaceAll("\\+", "%20").
replaceAll("%28", "\\(").
replaceAll("%29", "\\)").
replaceAll("%3B", ";").
replaceAll("%40", "@").
replaceAll("%23", "\\#").
replaceAll("%26", "\\&").
replaceAll("%2C", "\\,");
}
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName4 + "\"");
// 循环取出流中的数据
byte[] b = new byte[100];
int len;
try {
while ((len = ad(b)) > 0) {
}
inStream.close();
responseMsg.setSuccess(true);
} catch (IOException e) {
<("Exception:", e);
responseMsg.setSuccess(false);
}
return responseMsg;
}
}
登录业务控制器:
/**
* 登录
* 管理员admin 密码123 权限0(最⾼)
*
*/
@Controller
public class LoginController {
private Logger logger = Class());
@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 = Parameter("userName");
String password = Parameter("password");
String encryptedPwd = "";
try {
//加密的⽤户密码
//加密的⽤户密码
encryptedPwd = EncryptedPwd(password);
logger.warn("encryptedPwd:" + encryptedPwd);
} catch (NoSuchAlgorithmException e) {
<("Exception:", e);
} catch (UnsupportedEncodingException e) {
<("Exception:", e);
}
User dataBaseUser = userService.queryUserByUsername(userName);
if (dataBaseUser != null && encryptedPwd.PassWord())) {
User user = new User(userName, encryptedPwd, LevelType(), Email(), Phone(), Alias());
logger.info("⽤户登录成功!");
map.put("result", "1");
map.put("userName", userName);
} else if (dataBaseUser != null && !encryptedPwd.PassWord())) {
logger.info("密码错误!");
map.put("result", "2");
} else {
logger.info("⽤户不存在!");
map.put("result", "0");
}
return map;
}
// 退出登录
@RequestMapping(value = "/quit", method = RequestMethod.GET)
public String loginOut(HttpServletRequest request) {
if (!SystemUtil.isWindows()) {
// ⾮windows环境下要删除⽤户⽂件
FileUtil.deleteDir(fileRootPath + UserNameByRequest(request));
}
// 清除session
logger.info("退出登录成功!");
return "login";
}
// 注册
@RequestMapping(value = "/signin", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> signin(@RequestParam(required = false) String alias,
@RequestParam(required = true) String userName, @RequestParam(required = true) String password,
@RequestParam(required = false) String regcode, @RequestParam(required = false) String email,
@RequestParam(required = false) String phone, HttpServletRequest request, HttpServletResponse response) { Map<String, Object> map = new HashMap<>();
String encryptedPwd = "";
try {
//加密的⽤户密码
encryptedPwd = EncryptedPwd(password);
} catch (NoSuchAlgorithmException e) {
<("NoSuchAlgorithmException:", e);
} catch (UnsupportedEncodingException e) {
<("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);
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;
}
}
@RequestMapping(value = "/username", produces = "application/json; charset=utf-8")
@ResponseBody
public ResponseMsg fileRename(HttpServletRequest request) {
ResponseMsg j = new ResponseMsg();
// 获取⽤户名
User user = (User) Session().getAttribute("user");
String userName = UserName();
if (userName == null) {
userName = "null";
}
j.setMsg(userName);
j.setSuccess(true);
replaceall()return j;
}
@RequestMapping(value = "/getUserByUserName")
@ResponseBody
public User getUserByUserName(HttpServletRequest request) {
User user = (User) Session().getAttribute("user");
return user;
}
//更新当前⽤户信息
@RequestMapping(value = "/updateUserByUserName")
@ResponseBody
public ResponseMsg updateUserByUserName(@RequestParam(required = false) String username, @RequestParam(required = false) String alias, @RequestParam(required = false) String password, @RequestParam(required = false) String email, @RequestParam(required = false) String phone, HttpServletRequest request) {
ResponseMsg j = new ResponseMsg();
User user = (User) Session().getAttribute("user");
if (username != null) {
user.setUserName(username);
}
if (alias != null) {
user.setAlias(alias);
}
if (password != null) {
try {
String encryptedPwd = EncryptedPwd(password);
user.setPassWord(encryptedPwd);
} catch (NoSuchAlgorithmException e) {
<("NoSuchAlgorithmException:", e);
} catch (UnsupportedEncodingException e) {
<("UnsupportedEncodingException:", e);
}
}
if (email != null) {
user.setEmail(email);
}
if (phone != null) {
user.setPhone(phone);
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论