springboot文件上传下载实战 —— 登录功能、展示所有文件

spring.application.name=files

server.port=8989

server.servlet.context-path=/files

##配置thymleaf(下面注释的是默认配置, 可以不设置)

#spring.thymeleaf.prefix=classpath:/templates/

#spring.thymeleaf.suffix=.html

#spring.thymeleaf.encoding=UTF-8

#spring.thymeleaf.servlet.content-type=text/html

#本项目使用了热部署, 想让热部署生效必须配置这个

spring.thymeleaf.cache=false

#默认无法直接访问templates下的页面, 需要设置

spring.resources.static-locations=classpath:/templates/, classpath:/static/

##mysql配置

#指定连接池类型

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

#指定驱动

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#指定url

spring.datasource.url=jdbc:mysql://localhost:3306/userfiles?characterEncoding=UTF-8

#指定用户名

spring.datasource.username=root

#指定密码

spring.datasource.password=1234

#指定mapper配置文件位置

mybatis.mapper-locations=classpath:/com/yusael/mapper/*.xml

#指定起别名了的类

mybatis.type-aliases-package=com.yusael.entity

#允许最大上传大小

spring.servlet.multipart.max-file-size=500MB

spring.servlet.multipart.max-request-size=500MB

##日志配置

logging.level.root=info

logging.level.com.yusael.dao=debug

整体架构


搭建出项目的整体架构如下,然后可以开始进行开发各个功能了。

在这里插入图片描述

在启动类中加上 @MapperScan("com.yusael.dao") 来扫描 com.yusae.dao 包。

@SpringBootApplication

@MapperScan(“com.yusael.dao”)

public class MyfilesApplication {

public static void main(String[] args) {

SpringApplication.run(MyfilesApplication.class, args);

}

}

前端页面

=======================================================================

这次的实战主要是为了熟悉文件上传与下载,因此页面十分简易。

登录页面 login.html


用户登录

欢迎访问用户文件管理系统

username:

password:

文件列表页面 showAll.html


用户文件列表页面

欢迎:

文件列表

ID 文件原始名称 文件的新名称 文件后缀 存储路径 文件大小 类型 是否图片 下载次数 上传时间 操作

下载

在线打开

删除


上传列表

页面跳转控制器

==========================================================================

com.yusael.controller 包下创建 IndexController.java

package com.yusael.controller;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.GetMapping;

@Controller

public class IndexController {

@GetMapping(“/index”)

public String index() {

return “login”;

}

}

登录功能

=======================================================================

com.yusael.entity 包下创建数据库映射的实体类 User.java

package com.yusael.entity;

import lombok.AllArgsConstructor;

import lombok.Data;

import lombok.NoArgsConstructor;

import lombok.ToString;

@Data

@AllArgsConstructor

@NoArgsConstructor

@ToString

public class User {

private Integer id;

private String username;

private String password;

}

com.yusael.dao 包下创建 UserDAO.java

package com.yusael.dao;

import com.yusael.entity.User;

import org.apache.ibatis.annotations.Param;

public interface UserDAO {

User login(@Param(“username”) String username, @Param(“password”) String password);

}

resources/com/yusael/mapper 目录下创建接口 UserDAOMapper.xml

select id, username, password from t_user

where username = #{username} and password = #{password}

com.yusael.service 包下创建接口 UserService.java

package com.yusael.service;

import com.yusael.entity.User;

public interface UserService {

User login(String username, String password);

}

com.yusael.service 包下创建接口的实现类 UserServiceImpl.java

package com.yusael.service;

import com.yusael.dao.UserDAO;

import com.yusael.entity.User;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Propagation;

import org.springframework.transaction.annotation.Transactional;

@Service

@Transactional

public class UserServiceImpl implements UserService {

@Autowired

private UserDAO userDAO;

@Override

public User login(String username, String password) {

return userDAO.login(username, password);

}

}

com.yusael.dao 包下创建 UserController.java

package com.yusael.controller;

import com.yusael.entity.User;

import com.yusael.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller

@RequestMapping(“/user”)

public class UserController {

@Autowired

private UserService userService;

@PostMapping(“/login”)

public String login(String username, String password, HttpSession session) {

User login = userService.login(username, password);

if (login != null) {

session.setAttribute(“user”, login); // 用户存在

System.out.println(“登录成功!”);

return “redirect:/file/showAll”;

} else {

return “redirect:/index”; // 没有该用户, 跳转回登录界面

}

}

}

登录功能测试


在数据库的 t_user 表中存储一条数据:

在这里插入图片描述

浏览器输入网址:http://localhost:8989/files/index,来到登陆界面,利用数据库中的数据进行登录。

在这里插入图片描述

由于我们还没有开发好下面的页面,显示这个是正常的。

在这里插入图片描述

我们也可以在 UserController.java 中增加一个输出语句来确保正确登录。

在这里插入图片描述

在这里插入图片描述

展示所有文件

=========================================================================

com.yusael.entity 包下创建数据库映射的实体类 UserFilejava

package com.yusael.entity;

import lombok.AllArgsConstructor;

import lombok.Data;

import lombok.NoArgsConstructor;

import lombok.ToString;

import lombok.experimental.Accessors;

import java.util.Date;

@Data

@AllArgsConstructor

@NoArgsConstructor

@ToString

@Accessors(chain = true)

public class UserFile {

private Integer id;

private String oldFileName;

private String newFileName;

private String ext;

private String path;

private String size;

private String type;

private String isImg;

private Integer downcounts;

private Date uploadTime;

private Integer userId; // 用户外键

}

com.yusael.dao 包下创建 UserFileDAO

package com.yusael.dao;

import com.yusael.entity.UserFile;

import java.util.List;

public interface UserFileDAO {

// 根据登录用户id获取用户的文件目录

List findByUserId(Integer id);

}

com/yusael/mapper 目录下创建 UserFileDAOMapper.xml

select id,oldFileName,newFileName,ext,path,size,type,isImg,downcounts,uploadTime,userId

from t_files

where userId = #{id}

com.yusael.service 包下创建 UserFileService.java

package com.yusael.service;

import com.yusael.entity.User;

public interface UserService {

User login(String username, String password);

}

com.yusael.service 包下创建 UserFileServiceImpl.java

package com.yusael.service;

import com.yusael.dao.UserFileDAO;

import com.yusael.entity.User;

import com.yusael.entity.UserFile;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service

@Transactional

public class UserFileServiceImpl implements UserFileService{

@Autowired

private UserFileDAO userFileDAO;

@Override

public List findById(Integer id) {

return userFileDAO.findByUserId(id);

}

}

com.yusael.controller 包下创建 UserFileController.java

package com.yusael.controller;

import com.yusael.entity.User;

import com.yusael.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller

@RequestMapping(“/user”)

public class UserController {

@Autowired

private UserService userService;

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller

@RequestMapping(“/user”)

public class UserController {

@Autowired

private UserService userService;

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-cc814HvJ-1715076070051)]

[外链图片转存中…(img-HAMjygOu-1715076070052)]

[外链图片转存中…(img-sJRhhqTl-1715076070052)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值