源码获取:博客首页 "资源" 里下载!一、项目简述
功能:用户的邮箱注册、验证码验证以及用户登录。 不需要注册账号,也可以上传满足条件的临时文件,但是 只4小时内有效。 文件的管理,上传、下载、重命名、删除、查看统计数 据、分类管理等。 文件夹的管理,创建、删除、重命名。 文件的分享,支持通过链博口二维码的分享方式等等。
二、项目运行
环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe (IntelliJ IDEA,Eclispe,MyEclispe,Sts 都支持)
【Java毕业设计项目实战篇|Java项目:在线嘿嘿网盘系统设计和实现(java+Springboot+ssm+mysql+maven)】项目技术: JSP +Springboot+ SpringMVC + MyBatis + ThymeLeaf + FTP+ JavaScript + JQuery + Ajax + maven等等
文章图片
文章图片
文章图片
文章图片
管理员控制器:
/**
* @ClassName: AdminController
* @Description: 管理员控制器
**/
@Controller
public class AdminController extends BaseController {
private Logger logger = LogUtils.getInstance(AdminController.class);
/**
* @Description 前往用户管理页面
* @Author xw
* @Date 15:11 2020/3/10
* @Param [map]
* @return java.lang.String
**/
@GetMapping("/manages-users")
public String manageUsers(Map map,Integer cur){
if (loginUser.getRole() == 1){
//用于无访问权限
logger.error("当前登录用户:"+loginUser.getUserName()+"无管理员权限!");
return "redirect:/error401Page";
}
//获取全部的用户
Integer usersCount = userService.getUsersCount();
//获取当前查询的页数,如果为空,默认为0
cur = (cur == null || cur<0)?0:cur;
//获得统计信息
FileStoreStatistics statistics = myFileService.getCountStatistics(loginUser.getFileStoreId());
//分页获得20个用户信息
Page
登录控制器:
/**
* @Description 登录控制器
* @return
**/
@Controller
public class LoginController extends BaseController {private Logger logger = LogUtils.getInstance(LoginController.class);
/**
* @Description 免登陆用户入口,用于本地开发测试,上线运营为了安全请删除此方法
* @Author xw
* @Date 15:17 2020/2/26
* @Param []
* @return java.lang.String
**/
@GetMapping("/admin")
public String adminLogin(){
User user = userService.getUserByOpenId("123456");
logger.info("使用免登陆方式登录成功!"+user);
session.setAttribute("loginUser", user);
return "redirect:/index";
}/**
* 用于注册流程,用户名,密码,邮箱等校验工作由前端来完成
*
* @param map 用于存储提示信息
* @author GGBOY
* @date 2020/1/28
*/
@PostMapping("/register")
public String register(User user, String code, Map map) {
String uCode = (String) session.getAttribute(user.getEmail() + "_code");
if (!code.equals(uCode)) {
map.put("errorMsg", "验证码错误");
return "index";
}
// 用户名去空格
user.setUserName(user.getUserName().trim());
user.setImagePath("https://p.qpic.cn/qqconnect/0/app_101851241_1582451550/100?max-age=2592000&t=0");
user.setRegisterTime(new Date());
user.setRole(1);
if (userService.insert(user)) {
FileStore store = FileStore.builder().userId(user.getUserId()).currentSize(0).build();
fileStoreService.addFileStore(store);
user.setFileStoreId(store.getFileStoreId());
userService.update(user);
logger.info("注册用户成功!当前注册用户" + user);
logger.info("注册仓库成功!当前注册仓库" + store);
} else {
map.put("errorMsg", "服务器发生错误,注册失败");
return "index";
}
session.removeAttribute(user.getEmail() + "_code");
session.setAttribute("loginUser", user);
return "redirect:/index";
}/**
* 用户登录
* @param map 存储提示信息
* @return java.lang.String
* @author 莫提
* @date 2020/1/28
*/
@PostMapping("/login")
public String login(User user, Map map) {
User userByEmail = userService.getUserByEmail(user.getEmail());
if (userByEmail != null && userByEmail.getPassword().equals(user.getPassword())) {
session.setAttribute("loginUser", userByEmail);
logger.info("登录成功!"+userByEmail);
return "redirect:/index";
}else{
User user1 = userService.getUserByEmail(user.getEmail());
String errorMsg = user1 == null ? "该邮箱尚未注册" : "密码错误";
logger.info("登录失败!请确认邮箱和密码是否正确!");
//登录失败,将失败信息返回前端渲染
map.put("errorMsg", errorMsg);
return "index";
}
}/**
* @return void
* @Description 向注册邮箱发送验证码, 并验证邮箱是否已使用
* @Author xw
* @Date 19:32 2020/1/29
* @Param [userName, email, password]
**/
@ResponseBody
@RequestMapping("/sendCode")
public String sendCode(String userName, String email, String password) {
User userByEmail = userService.getUserByEmail(email);
if (userByEmail != null) {
logger.error("发送验证码失败!邮箱已被注册!");
return "exitEmail";
}
logger.info("开始发送邮件.../n" + "获取的到邮件发送对象为:" + mailSender);
mailUtils = new MailUtils(mailSender);
String code = "123456";
session.setAttribute(email + "_code", code);
return "success";
}/**
* @Description 请求QQ登录
* @Author xw
* @Date 18:27 2020/2/25
* @Param []
* @return void
**/
@GetMapping("/loginByQQ")
public void login() {
response.setContentType("text/html;
charset=utf-8");
try {
response.sendRedirect(new Oauth().getAuthorizeURL(request));
logger.info("请求QQ登录,开始跳转...");
} catch (QQConnectException | IOException e) {
e.printStackTrace();
}
}/**
* @Description QQ登录回调地址
* @Author xw
* @Date 18:27 2020/2/25
* @Param []
* @return java.lang.String
**/
@GetMapping("/connection")
public String connection() {
try {
AccessToken accessTokenObj = (new Oauth()).getAccessTokenByRequest(request);
String accessToken = null, openID = null;
long tokenExpireIn = 0L;
if ("".equals(accessTokenObj.getAccessToken())) {
logger.error("登录失败:没有获取到响应参数");
return "accessTokenObj=>" + accessTokenObj + ";
accessToken" + accessTokenObj.getAccessToken();
} else {
accessToken = accessTokenObj.getAccessToken();
tokenExpireIn = accessTokenObj.getExpireIn();
logger.error("accessToken" + accessToken);
request.getSession().setAttribute("demo_access_token", accessToken);
request.getSession().setAttribute("demo_token_expirein", String.valueOf(tokenExpireIn));
// 利用获取到的accessToken 去获取当前用的openid -------- start
OpenID openIDObj = new OpenID(accessToken);
openID = openIDObj.getUserOpenID();
UserInfo qzoneUserInfo = new UserInfo(accessToken, openID);
UserInfoBean userInfoBean = qzoneUserInfo.getUserInfo();
if (userInfoBean.getRet() == 0) {
logger.info("用户的OPEN_ID: " + openID);
logger.info("用户的昵称: " + removeNonBmpUnicode(userInfoBean.getNickname()));
logger.info("用户的头像URI: " + userInfoBean.getAvatar().getAvatarURL100());
//设置用户信息
User user = userService.getUserByOpenId(openID);
if (user == null){
user = User.builder()
.openId(openID).userName(removeNonBmpUnicode(userInfoBean.getNickname()))
.imagePath(userInfoBean.getAvatar().getAvatarURL100()).
registerTime(new Date()).build();
if (userService.insert(user)){
logger.info("注册用户成功!当前注册用户" + user);
FileStore store = FileStore.builder().userId(user.getUserId()).build();
if (fileStoreService.addFileStore(store) == 1){
user.setFileStoreId(store.getFileStoreId());
userService.update(user);
logger.info("注册仓库成功!当前注册仓库" + store);
}
} else {
logger.error("注册用户失败!");
}
}else {
user.setUserName(removeNonBmpUnicode(userInfoBean.getNickname()));
user.setImagePath(userInfoBean.getAvatar().getAvatarURL100());
userService.update(user);
}
logger.info("QQ用户登录成功!"+user);
session.setAttribute("loginUser", user);
return "redirect:/index";
} else {
logger.error("很抱歉,我们没能正确获取到您的信息,原因是: " + userInfoBean.getMsg());
}
}
} catch (QQConnectException e) {
} finally {
logger.error("登录成功!");
}
return "登录失败!请查看日志信息...";
}/**
* @Description 处理掉QQ网名中的特殊表情
* @Author xw
* @Date 18:26 2020/2/25
* @Param [str]
* @return java.lang.String 返回处理之后的网名
**/
public String removeNonBmpUnicode(String str) {
if (str == null) {
return null;
}
str = str.replaceAll("[^\\u0000-\\uFFFF]", "");
if ("".equals(str)) {
str = "($ _ $)";
}
return str;
}/**
* @Description 退出登录,清空session
* @Author xw
* @Date 18:26 2020/2/25
* @Param []
* @return java.lang.String
**/
@GetMapping("/logout")
public String logout() {
logger.info("用户退出登录!");
session.invalidate();
return "redirect:/";
}}
文件仓库控制器:
/**
* @ClassName: FileStoreController
* @Description: 文件仓库控制器
**/
@Controller
public class FileStoreController extends BaseController {
private Logger logger = LogUtils.getInstance(FileStoreController.class);
/**
* @Description 上传临时文件
* @Author xw
* @Date 23:14 2020/3/9
* @Param [files]
* @return void
**/
@PostMapping("/uploadTempFile")
public String uploadTempFile(@RequestParam("file") MultipartFile file,String url) {
session.setAttribute("imgPath","https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2654852821,3851565636&fm=26&gp=0.jpg");
String name = file.getOriginalFilename().replaceAll(" ","");
if (!checkTarget(name)){
logger.error("临时文件上传失败!文件名不符合规范...");
session.setAttribute("msg", "上传失败!文件名不符合规范");
return "redirect:/temp-file";
}
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = format.format(new Date());
String path = "temp/"+dateStr +"/"+UUID.randomUUID();
try {
if (FtpUtil.uploadFile("/"+path, name, file.getInputStream())){
//上传成功
logger.info("临时文件上传成功!"+name);
String size = String.valueOf(file.getSize());
TempFile tempFile = TempFile.builder().fileName(name).filePath(path).size(size).uploadTime(new Date()).build();
if (tempFileService.insert(tempFile)) {
try {
String id = UUID.randomUUID().toString();
String p = request.getSession().getServletContext().getRealPath("/user_img/");
Long t = tempFile.getUploadTime().getTime();
url = url+"/file/share?t="+ UUID.randomUUID().toString().substring(0,10) +"&f="+tempFile.getFileId()+"&p="+size+"&flag=2";
File targetFile = new File(p, "");
if (!targetFile.exists()) {
targetFile.mkdirs();
}
File f = new File(p, id + ".jpg");
if (!f.exists()){
//文件不存在,开始生成二维码并保存文件
OutputStream os = new FileOutputStream(f);
QRCodeUtil.encode(url, "/static/img/logo.png", os, true);
os.close();
}
//异步删除临时文件
tempFileService.deleteById(tempFile.getFileId());
session.setAttribute("imgPath","user_img/"+id+".jpg");
session.setAttribute("url",url);
session.setAttribute("msg","上传成功,扫码/访问链接 即可下载!");
return "redirect:/temp-file";
} catch (Exception e) {
e.printStackTrace();
}
}else {
logger.info("临时文件数据库写入失败!"+name);
session.setAttribute("url","error");
session.setAttribute("msg", "服务器出错了,临时文件上传失败!");
}
}else{
//上传失败
logger.info("临时文件上传失败!"+name);
session.setAttribute("url","error");
session.setAttribute("msg", "服务器出错了,上传失败!");
}
} catch (IOException e) {
e.printStackTrace();
}
return "redirect:/temp-file";
}/**
* @Description 网盘的文件上传
* @Author xw
* @Date 23:10 2020/2/10
* @Param [files]
* @return java.util.Map
**/
@PostMapping("/uploadFile")
@ResponseBody
public Map uploadFile(@RequestParam("file") MultipartFile files) {
Map map = new HashMap<>();
if (fileStoreService.getFileStoreByUserId(loginUser.getUserId()).getPermission() != 0){
logger.error("用户没有上传文件的权限!上传失败...");
map.put("code", 499);
return map;
}
FileStore store = fileStoreService.getFileStoreByUserId(loginUser.getUserId());
Integer folderId = Integer.valueOf(request.getHeader("id"));
String name = files.getOriginalFilename().replaceAll(" ","");
//获取当前目录下的所有文件,用来判断是否已经存在
List myFiles = null;
if (folderId == 0){
//当前目录为根目录
myFiles = myFileService.getRootFilesByFileStoreId(loginUser.getFileStoreId());
}else {
//当前目录为其他目录
myFiles = myFileService.getFilesByParentFolderId(folderId);
}
for (int i = 0;
i < myFiles.size();
i++) {
if ((myFiles.get(i).getMyFileName()+myFiles.get(i).getPostfix()).equals(name)){
logger.error("当前文件已存在!上传失败...");
map.put("code", 501);
return map;
}
}
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = format.format(new Date());
String path = loginUser.getUserId()+"/"+dateStr +"/"+folderId;
if (!checkTarget(name)){
logger.error("上传失败!文件名不符合规范...");
map.put("code", 502);
return map;
}
Integer sizeInt = Math.toIntExact(files.getSize() / 1024);
//是否仓库放不下该文件
if(store.getCurrentSize()+sizeInt > store.getMaxSize()){
logger.error("上传失败!仓库已满。");
map.put("code", 503);
return map;
}
//处理文件大小
String size = String.valueOf(files.getSize()/1024.0);
int indexDot = size.lastIndexOf(".");
size = size.substring(0,indexDot);
int index = name.lastIndexOf(".");
String tempName = name;
String postfix = "";
int type = 4;
if (index!=-1){
tempName = name.substring(index);
name = name.substring(0,index);
//获得文件类型
type = getType(tempName.toLowerCase());
postfix = tempName.toLowerCase();
}
try {
//提交到FTP服务器
boolean b = FtpUtil.uploadFile("/"+path, name + postfix, files.getInputStream());
if (b){
//上传成功
logger.info("文件上传成功!"+files.getOriginalFilename());
//向数据库文件表写入数据
myFileService.addFileByFileStoreId(
MyFile.builder()
.myFileName(name).fileStoreId(loginUser.getFileStoreId()).myFilePath(path)
.downloadTime(0).uploadTime(new Date()).parentFolderId(folderId).
size(Integer.valueOf(size)).type(type).postfix(postfix).build());
//更新仓库表的当前大小
fileStoreService.addSize(store.getFileStoreId(),Integer.valueOf(size));
try {
Thread.sleep(5000);
map.put("code", 200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}else{
logger.error("文件上传失败!"+files.getOriginalFilename());
map.put("code", 504);
}
} catch (IOException e) {
e.printStackTrace();
}
return map;
}/**
* @Description 网盘的文件下载
* @Author xw
* @Date 23:13 2020/2/10
* @Param [fId]
* @return void
**/
@GetMapping("/downloadFile")
public String downloadFile(@RequestParam Integer fId){
if (fileStoreService.getFileStoreByUserId(loginUser.getUserId()).getPermission() == 2){
logger.error("用户没有下载文件的权限!下载失败...");
return "redirect:/error401Page";
}
//获取文件信息
MyFile myFile = myFileService.getFileByFileId(fId);
String remotePath = myFile.getMyFilePath();
String fileName = myFile.getMyFileName()+myFile.getPostfix();
try {
//去FTP上拉取
OutputStream os = new BufferedOutputStream(response.getOutputStream());
response.setCharacterEncoding("utf-8");
// 设置返回类型
response.setContentType("multipart/form-data");
// 文件名转码一下,不然会出现中文乱码
response.setHeader("Content-Disposition", "attachment;
fileName=" + URLEncoder.encode(fileName, "UTF-8"));
boolean flag = FtpUtil.downloadFile("/" + remotePath, fileName, os);
if (flag) {
myFileService.updateFile(
MyFile.builder().myFileId(myFile.getMyFileId()).downloadTime(myFile.getDownloadTime() + 1).build());
os.flush();
os.close();
logger.info("文件下载成功!" + myFile);
}
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}/**
* @Description 删除文件
* @Author xw
* @Date 23:14 2020/2/10
* @Param [fId, folder]
* @return java.lang.String
**/
@GetMapping("/deleteFile")
public String deleteFile(@RequestParam Integer fId,Integer folder){
//获得文件信息
MyFile myFile = myFileService.getFileByFileId(fId);
String remotePath = myFile.getMyFilePath();
String fileName = myFile.getMyFileName()+myFile.getPostfix();
//从FTP文件服务器上删除文件
boolean b = FtpUtil.deleteFile("/"+remotePath, fileName);
if (b){
//删除成功,返回空间
fileStoreService.subSize(myFile.getFileStoreId(),Integer.valueOf(myFile.getSize()));
//删除文件表对应的数据
myFileService.deleteByFileId(fId);
}
logger.info("删除文件成功!"+myFile);
return "redirect:/files?fId="+folder;
}/**
* @Description 删除文件夹并清空文件
* @Author xw
* @Date 15:22 2020/2/12
* @Param [fId]
* @return java.lang.String
**/
@GetMapping("/deleteFolder")
public String deleteFolder(@RequestParam Integer fId){
FileFolder folder = fileFolderService.getFileFolderByFileFolderId(fId);
//强制删除
deleteFolderF(folder);
return folder.getParentFolderId() == 0?"redirect:/files":"redirect:/files?fId="+folder.getParentFolderId();
} /**
* @Description 迭代删除文件夹里面的所有文件和子文件夹
* @Author xw
* @Date 9:17 2020/2/29
* @Param [folder]
* @return void
**/
public void deleteFolderF(FileFolder folder){
//获得当前文件夹下的所有子文件夹
List folders = fileFolderService.getFileFolderByParentFolderId(folder.getFileFolderId());
//删除当前文件夹的所有的文件
List files = myFileService.getFilesByParentFolderId(folder.getFileFolderId());
if (files.size()!=0){
for (int i = 0;
i < files.size();
i++) {
Integer fileId = files.get(i).getMyFileId();
boolean b = FtpUtil.deleteFile("/"+files.get(i).getMyFilePath(), files.get(i).getMyFileName() + files.get(i).getPostfix());
if (b){
myFileService.deleteByFileId(fileId);
fileStoreService.subSize(folder.getFileStoreId(),Integer.valueOf(files.get(i).getSize()));
}
}
}
if (folders.size()!=0){
for (int i = 0;
i < folders.size();
i++) {
deleteFolderF(folders.get(i));
}
}
fileFolderService.deleteFileFolderById(folder.getFileFolderId());
}/**
* @Description 添加文件夹
* @Author xw
* @Date 23:16 2020/2/10
* @Param [folder, map]
* @return java.lang.String
**/
@PostMapping("/addFolder")
public String addFolder(FileFolder folder,Map map) {
//设置文件夹信息
folder.setFileStoreId(loginUser.getFileStoreId());
folder.setTime(new Date());
//获得当前目录下的所有文件夹,检查当前文件夹是否已经存在
List fileFolders = null;
if (folder.getParentFolderId() == 0){
//向用户根目录添加文件夹
fileFolders = fileFolderService.getRootFoldersByFileStoreId(loginUser.getFileStoreId());
}else{
//向用户的其他目录添加文件夹
fileFolders = fileFolderService.getFileFolderByParentFolderId(folder.getParentFolderId());
}
for (int i = 0;
i < fileFolders.size();
i++) {
FileFolder fileFolder = fileFolders.get(i);
if (fileFolder.getFileFolderName().equals(folder.getFileFolderName())){
logger.info("添加文件夹失败!文件夹已存在...");
return "redirect:/files?error=1&fId="+folder.getParentFolderId();
}
}
//向数据库写入数据
Integer integer = fileFolderService.addFileFolder(folder);
logger.info("添加文件夹成功!"+folder);
return "redirect:/files?fId="+folder.getParentFolderId();
}/**
* @Description 重命名文件夹
* @Author xw
* @Date 23:18 2020/2/10
* @Param [folder, map]
* @return java.lang.String
**/
@PostMapping("/updateFolder")
public String updateFolder(FileFolder folder,Map map) {
//获得文件夹的数据库信息
FileFolder fileFolder = fileFolderService.getFileFolderByFileFolderId(folder.getFileFolderId());
fileFolder.setFileFolderName(folder.getFileFolderName());
//获得当前目录下的所有文件夹,用于检查文件夹是否已经存在
List fileFolders = fileFolderService.getFileFolderByParentFolderId(fileFolder.getParentFolderId());
for (int i = 0;
i < fileFolders.size();
i++) {
FileFolder folder1 = fileFolders.get(i);
if (folder1.getFileFolderName().equals(folder.getFileFolderName()) && folder1.getFileFolderId() != folder.getFileFolderId()){
logger.info("重命名文件夹失败!文件夹已存在...");
return "redirect:/files?error=2&fId="+fileFolder.getParentFolderId();
}
}
//向数据库写入数据
Integer integer = fileFolderService.updateFileFolderById(fileFolder);
logger.info("重命名文件夹成功!"+folder);
return "redirect:/files?fId="+fileFolder.getParentFolderId();
}/**
* @Description 重命名文件
* @Author xw
* @Date 12:47 2020/2/12
* @Param [file, map]
* @return java.lang.String
**/
@PostMapping("/updateFileName")
public String updateFileName(MyFile file,Map map) {
MyFile myFile = myFileService.getFileByFileId(file.getMyFileId());
if (myFile != null){
String oldName = myFile.getMyFileName();
String newName = file.getMyFileName();
if (!oldName.equals(newName)){
boolean b = FtpUtil.reNameFile(myFile.getMyFilePath() + "/" + oldName+myFile.getPostfix(), myFile.getMyFilePath() + "/" + newName+myFile.getPostfix());
if (b){
Integer integer = myFileService.updateFile(
MyFile.builder().myFileId(myFile.getMyFileId()).myFileName(newName).build());
if (integer == 1){
logger.info("修改文件名成功!原文件名:"+oldName+"新文件名:"+newName);
}else{
logger.error("修改文件名失败!原文件名:"+oldName+"新文件名:"+newName);
}
}
}
}
return "redirect:/files?fId="+myFile.getParentFolderId();
}/**
* @Description 获得二维码
* @Author xw
* @Date 15:20 2020/2/12
* @Param [id, url]
* @return java.util.Map
**/
@GetMapping("getQrCode")
@ResponseBody
public Map getQrCode(@RequestParam Integer id,@RequestParam String url){
Map map = new HashMap<>();
map.put("imgPath","https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2654852821,3851565636&fm=26&gp=0.jpg");
if (id != null){
MyFile file = myFileService.getFileByFileId(id);
if (file != null){
try {
String path = request.getSession().getServletContext().getRealPath("/user_img/");
url = url+"/file/share?t="+ UUID.randomUUID().toString().substring(0,10) +"&f="+file.getMyFileId()+"&p="+file.getUploadTime().getTime()+""+file.getSize()+"&flag=1";
File targetFile = new File(path, "");
if (!targetFile.exists()) {
targetFile.mkdirs();
}
File f = new File(path, id + ".jpg");
if (!f.exists()){
//文件不存在,开始生成二维码并保存文件
OutputStream os = new FileOutputStream(f);
QRCodeUtil.encode(url, "/static/img/logo.png", os, true);
os.close();
}
map.put("imgPath","user_img/"+id+".jpg");
map.put("url",url);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return map;
}/**
* @Description 分享文件
* @Author xw
* @Date 14:23 2020/2/12
* @Param [fId]
* @return void
**/
@GetMapping("/file/share")
public String shareFile(Integer f,String p,String t,Integer flag){
String fileNameTemp = "";
String remotePath = "";
String fileName = "";
Integer times = 0;
if (flag == null || f == null || p == null || t == null){
logger.info("下载分享文件失败,参数错误");
return "redirect:/error400Page";
}
if(flag == 1){
//获取文件信息
MyFile myFile = myFileService.getFileByFileId(f);
if (myFile == null){
return "redirect:/error404Page";
}
String pwd = myFile.getUploadTime().getTime()+""+myFile.getSize();
if (!pwd.equals(p)){
return "redirect:/error400Page";
}
remotePath = myFile.getMyFilePath();
fileName = myFile.getMyFileName()+myFile.getPostfix();
}else if(flag == 2){
TempFile tempFile = tempFileService.queryById(f);
if (tempFile == null){
return "redirect:/error404Page";
}
Long test = tempFile.getUploadTime().getTime();
String pwd = tempFile.getSize();
if (!pwd.equals(p)){
return "redirect:/error400Page";
}
remotePath = tempFile.getFilePath();
fileName = tempFile.getFileName();
}else {
return "redirect:/error400Page";
}
fileNameTemp = fileName;
try {
//解决下载文件时 中文文件名乱码问题
boolean isMSIE = isMSBrowser(request);
if (isMSIE) {
//IE浏览器的乱码问题解决
fileNameTemp = URLEncoder.encode(fileNameTemp, "UTF-8");
} else {
//万能乱码问题解决
fileNameTemp = new String(fileNameTemp.getBytes("UTF-8"), "ISO-8859-1");
}
//去FTP上拉取
OutputStream os = new BufferedOutputStream(response.getOutputStream());
response.setCharacterEncoding("utf-8");
// 设置返回类型
response.setContentType("multipart/form-data");
// 文件名转码一下,不然会出现中文乱码
response.setHeader("Content-Disposition", "attachment;
fileName=" + fileNameTemp);
if (FtpUtil.downloadFile("/" + remotePath, fileName, os)) {
myFileService.updateFile(
MyFile.builder().myFileId(f).downloadTime(times + 1).build());
os.flush();
os.close();
logger.info("文件下载成功!");
}
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}/**
* @Description 根据文件的后缀名获得对应的类型
* @Author xw
* @Date 23:20 2020/2/10
* @Param [type]
* @return int 1:文本类型2:图像类型3:视频类型4:音乐类型5:其他类型
**/
public int getType(String type){
if (".chm".equals(type)||".txt".equals(type)||".xmind".equals(type)||".xlsx".equals(type)||".md".equals(type)
||".doc".equals(type)||".docx".equals(type)||".pptx".equals(type)
||".wps".equals(type)||".word".equals(type)||".html".equals(type)||".pdf".equals(type)){
return1;
}else if (".bmp".equals(type)||".gif".equals(type)||".jpg".equals(type)||".ico".equals(type)||".vsd".equals(type)
||".pic".equals(type)||".png".equals(type)||".jepg".equals(type)||".jpeg".equals(type)||".webp".equals(type)
||".svg".equals(type)){
return 2;
} else if (".avi".equals(type)||".mov".equals(type)||".qt".equals(type)
||".asf".equals(type)||".rm".equals(type)||".navi".equals(type)||".wav".equals(type)
||".mp4".equals(type)||".mkv".equals(type)||".webm".equals(type)){
return 3;
} else if (".mp3".equals(type)||".wma".equals(type)){
return 4;
} else {
return 5;
}
}/**
* @Description 正则验证文件名是否合法 [汉字,字符,数字,下划线,英文句号,横线]
* @Author xw
* @Date 23:22 2020/2/10
* @Param [target]
* @return boolean
**/
public boolean checkTarget(String target) {
final String format = "[^\\u4E00-\\u9FA5\\uF900-\\uFA2D\\w-_.]";
Pattern pattern = Pattern.compile(format);
Matcher matcher = pattern.matcher(target);
return !matcher.find();
}/**
* @Description 判断当前浏览器是否为ie
* @Author xw
* @Date 22:39 2020/3/5
* @Param [request]
* @return boolean
**/
public static boolean isMSBrowser(HttpServletRequest request) {
String[] IEBrowserSignals = {"MSIE", "Trident", "Edge"};
String userAgent = request.getHeader("User-Agent");
for (String signal : IEBrowserSignals) {
if (userAgent.contains(signal)){
return true;
}
}
return false;
}}
源码获取:博客首页 "资源" 里下载!
推荐阅读
- Java|Java基础——数组
- 人工智能|干货!人体姿态估计与运动预测
- java简介|Java是什么(Java能用来干什么?)
- Java|规范的打印日志
- Linux|109 个实用 shell 脚本
- mysql|InnoDB数据页结构
- 程序员|【高级Java架构师系统学习】毕业一年萌新的Java大厂面经,最新整理
- javaweb|基于Servlet+jsp+mysql开发javaWeb学生成绩管理系统
- Spring注解驱动第十讲--@Autowired使用
- =======j2ee|spring用注解实现注入的@resource,@autowired,@inject区别