package cn.tangtj.clouddisk.web;
import cn.tangtj.clouddisk.entity.UploadFile;
import cn.tangtj.clouddisk.entity.User;
import cn.tangtj.clouddisk.entity.vo.Files;
import cn.tangtj.clouddisk.service.FileService;
import cn.tangtj.clouddisk.service.UserService;
import cn.tangtj.clouddisk.utils.FileUtil;
import cn.tangtj.clouddisk.utils.StringUtil;
import cn.tangtj.clouddisk.utils.UserUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletContext;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
/**
* @author tang
*/
@Controller
@RequestMapping("/file")
public class FileController {
private final String fileSavePath;
private final static String fileSaveDir = "upload";
private final UserService userService;
private final FileService fileService;
@Autowired
public FileController(UserService userService, FileService fileService, ServletContext servletContext) {
this.userService = userService;
this.fileService = fileService;
fileSavePath = servletContext.getRealPath("") + fileSaveDir + File.separator;
File file = new File(fileSavePath);
if (!file.exists()) {
file.mkdir();
}
}
@RequestMapping()
public String index(Model model) {
User user = UserUtil.getPrincipal();
if (user == null) {
return "redirect:/login";
}
List<UploadFile> list = fileService.findByUserId(user.getId());
Files files = new Files(list);
model.addAttribute("user", user);
model.addAttribute("files", files);
return "files";
}
@RequestMapping(value = "/upload")
@ResponseBody
public String fileUpload(MultipartFile file) {
User user = UserUtil.getPrincipal();
if (user != null && file != null && !file.isEmpty()) {
UploadFile f = new UploadFile();
f.setFileName(file.getOriginalFilename());
f.setUserId(user.getId());
f.setFileSize(file.getSize());
f.setUploadDate(new Date());
String md5Name = StringUtil.str2md5(f.getFileName() + "," + f.getUserId() + "," + f.getUploadDate().toString());
Files files = new Files(fileService.findByUserId(user.getId()));
if(files.getFilesSize() + f.getFileSize() > user.getFileMaxSize()){
return "upload.fail";
}
if (files.getFilesCount() + 1 > user.getFileMaxCount()){
return "upload,fail";
}
if (md5Name == null) {
return "upload,fail";
}
f.setMappingName(md5Name);
File localFile = new File(fileSavePath + f.getMappingName());
try {
file.transferTo(localFile);
fileService.save(f);
return "upload,success";
} catch (IOException e) {
e.printStackTrace();
}
}
return "upload,fail";
}
@RequestMapping("/download/{fileId}")
public ResponseEntity<byte[]> fileDownload(@PathVariable("fileId") int fileId) {
UploadFile fileInfo = fileService.findById(fileId);
return FileUtil.createResponseEntityByFileInfo(fileInfo, fileSavePath);
}
@RequestMapping(value = "/shareFile", produces = "text/json; charset=utf-8")
@ResponseBody
public String shareFile(String fileIdStr) {
if (fileIdStr == null) {
return "分享失败";
}
int fileId = Integer.parseInt(fileIdStr);
UploadFile fileInfo = fileService.findById(fileId);
if (fileInfo != null) {
if (fileService.shareFileById(fileInfo.getId()) != null) {
return "分享成功";
}
}
return "分享失败";
}
@RequestMapping(value = "/unShareFile", produces = "text/json; charset=utf-8")
@ResponseBody
public String unShareFile(String fileIdStr) {
User user = (User) SecurityUtils.getSubject().getPrincipal();
if (fileIdStr == null) {
return "取消失败";
}
int fileId = Integer.parseInt(fileIdStr);
List<UploadFile> list = fileService.findByUserId(user.getId());
if (list.stream().anyMatch(it->it.getId() == fileId)) {
fileService.unshareFile(fileId);
return "取消成功";
}
return "取消失败";
}
@RequestMapping("/delete/{fileId}")
public String fileDelete(@PathVariable("fileId") int fileId) {
User user = (User) SecurityUtils.getSubject().getPrincipal();
List<UploadFile> list = fileService.findByUserId(user.getId());
list.stream().filter(it->it.getId()==fileId).findFirst().ifPresent(it->{
fileService.deleteById(fileId);
File file = new File(fileSavePath + it.getMappingName());
if (file.exists()){
file.delete();
}
});
return "redirect:/file";
}
@RequestMapping("/cancel")
public String logout(){
Subject subject = SecurityUtils.getSubject();
subject.logout();
return "redirect:/";
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
(免费分享)基于ssm简易网盘系统-022
共107个文件
xml:55个
java:21个
js:7个
需积分: 0 0 下载量 155 浏览量
2023-12-13
11:15:12
上传
评论
收藏 527KB ZIP 举报
温馨提示
详细说明https://blog.csdn.net/2301_81426796/article/details/134965500
资源推荐
资源详情
资源评论
收起资源包目录
(免费分享)基于ssm简易网盘系统-022 (107个子文件)
bootstrap.css 143KB
bootstrap.min.css 118KB
bootstrap-theme.css 26KB
bootstrap-theme.min.css 23KB
glyphicons-halflings-regular.eot 20KB
.gitignore 117B
CloudDisk.iml 5KB
FileController.java 6KB
FileDao.java 3KB
SignUpController.java 3KB
ShareFileController.java 3KB
StringUtil.java 2KB
SystemRealm.java 2KB
UserDao.java 2KB
UploadFile.java 1KB
FileService.java 1KB
LoginController.java 1KB
FileUtil.java 1KB
User.java 1KB
Files.java 1011B
FormFilterAuthc.java 988B
UserService.java 667B
Guest.java 623B
ShareFile.java 616B
UserUtil.java 443B
SignUpNameCheckResult.java 411B
JdbcUtil.java 394B
MainController.java 324B
jquery-3.3.1.js 265KB
jquery-3.3.1.min.js 85KB
bootstrap.js 68KB
bootstrap.min.js 36KB
signUp.js 2KB
login.js 2KB
npm.js 484B
files.jsp 9KB
signUp.jsp 4KB
shareFileIndex.jsp 2KB
login.jsp 2KB
head.jsp 393B
taglib.jsp 381B
bootstrap.min.css.map 529KB
bootstrap.css.map 380KB
bootstrap-theme.css.map 47KB
bootstrap-theme.min.css.map 25KB
.name 9B
clouddisk.sql 2KB
glyphicons-halflings-regular.svg 106KB
glyphicons-halflings-regular.ttf 44KB
glyphicons-halflings-regular.woff 23KB
glyphicons-halflings-regular.woff2 18KB
workspace.xml 12KB
pom.xml 7KB
CloudDisk_war_exploded.xml 5KB
web.xml 3KB
log4j2.xml 2KB
spring-mvc.xml 2KB
spring-context-shiro.xml 1KB
Maven__org_springframework_spring_expression_5_0_6_RELEASE.xml 658B
compiler.xml 645B
Maven__com_fasterxml_jackson_core_jackson_annotations_2_9_3.xml 644B
Maven__org_springframework_spring_context_5_0_6_RELEASE.xml 637B
Maven__org_springframework_spring_webmvc_5_0_6_RELEASE.xml 630B
Maven__org_apache_taglibs_taglibs_standard_spec_1_2_5.xml 626B
Maven__org_apache_taglibs_taglibs_standard_impl_1_2_5.xml 626B
Maven__com_fasterxml_jackson_core_jackson_databind_2_9_3.xml 623B
Maven__org_springframework_spring_beans_5_0_6_RELEASE.xml 623B
Maven__commons_collections_commons_collections_3_2_2.xml 616B
Maven__org_springframework_spring_core_5_0_6_RELEASE.xml 616B
Maven__org_springframework_spring_aop_5_0_6_RELEASE.xml 609B
Maven__org_springframework_spring_web_5_0_6_RELEASE.xml 609B
Maven__org_springframework_spring_jcl_5_0_6_RELEASE.xml 609B
Maven__commons_fileupload_commons_fileupload_1_3_3.xml 605B
Maven__org_apache_shiro_shiro_crypto_cipher_1_4_0.xml 604B
Maven__com_mchange_mchange_commons_java_0_2_11.xml 598B
Maven__com_fasterxml_jackson_core_jackson_core_2_9_3.xml 595B
Maven__commons_beanutils_commons_beanutils_1_9_3.xml 594B
Maven__org_apache_shiro_shiro_config_core_1_4_0.xml 590B
Maven__org_apache_shiro_shiro_crypto_hash_1_4_0.xml 590B
Maven__org_apache_shiro_shiro_crypto_core_1_4_0.xml 590B
Maven__org_apache_shiro_shiro_config_ogdl_1_4_0.xml 590B
misc.xml 583B
Maven__org_apache_logging_log4j_log4j_core_2_11_0.xml 580B
Maven__org_apache_logging_log4j_log4j_api_2_11_0.xml 573B
Maven__org_apache_logging_log4j_log4j_web_2_11_0.xml 573B
Maven__mysql_mysql_connector_java_6_0_6.xml 567B
Maven__commons_dbutils_commons_dbutils_1_7.xml 558B
Maven__org_apache_shiro_shiro_spring_1_4_0.xml 555B
Maven__org_apache_shiro_shiro_cache_1_4_0.xml 548B
Maven__org_apache_shiro_shiro_event_1_4_0.xml 548B
Maven__org_apache_shiro_shiro_core_1_4_0.xml 541B
Maven__org_apache_shiro_shiro_lang_1_4_0.xml 541B
Maven__org_apache_shiro_shiro_web_1_4_0.xml 534B
Maven__org_hamcrest_hamcrest_core_1_3.xml 532B
Maven__javax_activation_activation_1_1.xml 527B
Maven__com_sun_mail_javax_mail_1_5_0.xml 525B
Maven__org_slf4j_slf4j_api_1_7_21.xml 513B
c3p0-config.xml 509B
Maven__commons_io_commons_io_2_2.xml 503B
Maven__com_mchange_c3p0_0_9_5_2.xml 493B
共 107 条
- 1
- 2
资源评论
IT教程资源_
- 粉丝: 1933
- 资源: 37
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功