写点什么

springboot 文件上传下载实战 —— 登录功能,「高并发秒杀

发布于: 2 小时前


GitHub:[https://github.com/szluyu99/springboot_files](


)


后续内容:[springboot 文件上传下载实战 ——文件上传、下载、在线打开、删除](


)


SpringBoot 知识点目录: [SpringBoot 核心知识点整理!](


)


[](


)创建项目


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


通过 Spring Initializr 或者 直接创建一个 Maven 项目来构建一个 springboot 项目,我们通过 Spring Initializr 来创建:



输入项目信息:



选择 Sprig Wbe 依赖,其他依赖可以看我后面的 pom.xml。



删除一些用不到的文件和文件夹(比如 test 下的文件),基础的 spring boot 项目结构如下:



[](


)pom.xml




这些是本项目中要用到依赖,直接复制我的 pom.xml 即可。



<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--继承springboot父项目-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.7.RELEASE</version>
</parent>
<groupId>com.yusael</groupId>
<artifactId>myfiles</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>myfiles</name>
<description>文件的上传与下载实战</description>


<properties>
<java.version>1.8</java.version>
</properties>


<dependencies>
<!--引入springboot的web支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.43</version>
<scope>runtime</scope>
</dependency>
<!--druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--commons-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
</dependencies>


<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>
复制代码


[](


)数据库建表与环境准备


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


需求分析:


  • 要做的是文件管理系统,自然要存储文件的信息,需要文件信息表(t_files)

  • 我们要做用户的登录功能,需要用户表(t_user)(此处为了简略,省略注册,若想看注册,可以看 [基于 springboot+thymeleaf+mybatis 的员工管理系统 —— 登录与注册](


))


[](


)建表 SQL




用户表 t_user 的 SQL:



CREATE TABLE `t_user` (
`id` int(8) NOT NULL,
`username` varchar(80) DEFAULT NULL,
`password` varchar(80) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
复制代码


文件信息表 t_files 的 SQL:



CREATE TABLE `t_files` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`oldFileName` varchar(200) DEFAULT NULL,
`newFileName` varchar(300) DEFAULT NULL,
`ext` varchar(20) DEFAULT NULL,
`path` varchar(300) DEFAULT NULL,
`size` varchar(200) DEFAULT NULL,
`type` varchar(120) DEFAULT NULL,
`isImg` varchar(8) DEFAULT NULL,
`downcounts` int(6) DEFAULT NULL,
`uploadTime` datetime DEFAULT NULL,
`userId` int(8) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `userId` (`userId`),
CONSTRAINT `userId` FOREIGN KEY (`userId`) REFERENCES `t_user` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=46 DEFAULT CHARSET=utf8;
复制代码


数据库建完表以后,还需要在 application.properties 中配置。


[](


)配置文件 application.properties





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





<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>用户登录</title>
</head>
<body>
<h1>欢迎访问用户文件管理系统</h1>
<form th:action="@{/user/login}" method="post">
username: <input type="text" name="username"/> <br>
password: <input type="password" name="password"/> <br>
<input type="submit" value="登录">
</form>
</body>
</html>
复制代码


[](


)文件列表页面 showAll.html





<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>用户文件列表页面</title>
</head>
<body>


<h1>欢迎: <span th:if="${session.user != null}" th:text="%{session.user.username}"/></h1>
<h2>文件列表</h2>
<table border="1px">
<tr>
<th>ID</th>
<th>文件原始名称</th>
<th>文件的新名称</th>
<th>文件后缀</th>
<th>存储路径</th>
<th>文件大小</th>
<th>类型</th>
<th>是否图片</th>
<th>下载次数</th>
<th>上传时间</th>
<th>操作</th>
</tr>
<tr th:each="file : ${files}">
<td><span th:text="${file.id}"/></td>
<td><span th:text="${file.id}"/></td>
<td><span th:text="${file.oldFileName}"/></td>
<td><span th:text="${file.newFileName}"/></td>
<td><span th:text="${file.ext}"/></td>
<td><span th:text="${file.path}"/></td>
<td><span th:text="${file.size}"/></td>
<td><span th:text="${file.type}"/></td>
<td>
<span th:text="${file.isImg}"/>
<img th:if="${file.isImg}=='是'" style="height: 40px;height: 100px" th:src="${#servletContext.contextPath} + ${file.path} + '/' + ${file.newFileName}" alt="">
</td>
<td th:id="${file.id}"><span th:text="${file.downcounts}"/></td>
<td><span th:text="${#dates.format(file.uploadTime, 'yyyy-MM-dd hh:mm:ss')}"/></td>
<td>
<a th:href="@{/file/download(id=${file.id})}">下载</a>
<a th:href="@{/file/download(id=${file.id},openStyle='inline')}">在线打开</a>
<a th:href="@{/file/delete(id=${file.id})}">删除</a>
</td>
</tr>
</table>
<hr>
<h3>上传列表</h3>
<form th:action="@{/file/upload}" method="post" enctype="multipart/form-data">
<input type="file" name="aaa">
<input type="submit" value="上传文件">
</form>
</body>
</html>
复制代码


[](


)页面跳转控制器


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


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);
}
复制代码

总结

面试难免让人焦虑不安。经历过的人都懂的。但是如果你提前预测面试官要问你的问题并想出得体的回答方式,就会容易很多。


此外,都说“面试造火箭,工作拧螺丝”,那对于准备面试的朋友,你只需懂一个字:刷!


给我刷刷刷刷,使劲儿刷刷刷刷刷!今天既是来谈面试的,那就必须得来整点面试真题,这不花了我整 28 天,做了份“Java 一线大厂高岗面试题解析合集:JAVA 基础-中级-高级面试+SSM 框架+分布式+性能调优+微服务+并发编程+网络+设计模式+数据结构与算法等”



CodeChina开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频】


且除了单纯的刷题,也得需准备一本【JAVA 进阶核心知识手册】:JVM、JAVA 集合、JAVA 多线程并发、JAVA 基础、Spring 原理、微服务、Netty 与 RPC、网络、日志、Zookeeper、Kafka、RabbitMQ、Hbase、MongoDB、Cassandra、设计模式、负载均衡、数据库、一致性算法、JAVA 算法、数据结构、加密算法、分布式缓存、Hadoop、Spark、Storm、YARN、机器学习、云计算,用来查漏补缺最好不过。



用户头像

VX:vip204888 领取资料 2021.07.29 加入

还未添加个人简介

评论

发布
暂无评论
springboot文件上传下载实战 —— 登录功能,「高并发秒杀