【springboot学习第一天】整合Mybatis实现文件上传

【springboot学习第一天】整合Mybatis实现文件上传

背景

我想做个网盘项目,嗯,就是拿来练手的,之前用servlet写过一个
,不过自从接触了springboot我真的想去重构下,所以干脆上来就先去看看文件上传怎么做。

相关文章

IDEA创建Springboot项目

hello,springboot

1、创建数据表

# Host: LOCALHOST  (Version: 5.5.15)
# Date: 2018-06-24 22:45:58
# Generator: MySQL-Front 5.3  (Build 4.269)

/*!40101 SET NAMES utf8 */;

#
# Structure for table "file"
#

DROP TABLE IF EXISTS `file`;
CREATE TABLE `file` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `filename` varchar(255) DEFAULT NULL,
  `day` date DEFAULT '0000-00-00',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

2、创建项目

IDEA创建Springboot项目

3、添加依赖

<!--servlet-->
 		<dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
<!--Tomcat-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <version>8.5.31</version>
        </dependency>
<!--mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.5</version>
        </dependency>
		        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.0</version>
        </dependency>
<!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.44</version>
        </dependency>
<!--pageHelper-->
        <dependency>
            <groupId>pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>3.4.2-fix</version>
        </dependency>
<!--shiro-->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-web</artifactId>
            <version>1.3.2</version>
        </dependency>
<!--session-->
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-bom</artifactId>
            <version>Apple-SR3</version>
        </dependency>

为啥要添加这么多的依赖,emmm其实我想写个稍微成熟点的小项目而已,先从第一步开始吧。

4、建包

建包

建包

就这么多包了,有些还没用过。

5、配置propertis文件

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
spring.session.store-type=none
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/uolpad?characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.password=admin
spring.datasource.username=root

有些配置还没写,因为暂时用不到,比如Shiro的配置等。

6、编码

放一些核心代码,写的不好,(反正我脸皮厚/逃)
ShowFileController ---文件展示功能

    @RequestMapping("/showFile")
    public String showFile(Model model, @RequestParam(value = "start", defaultValue = "1") int start, @RequestParam(value = "size", defaultValue = "10") int size){
        PageHelper.startPage(start,size,"id desc");
        List<FilePojo> filePojos = fileMapper.getAll();
        PageInfo<FilePojo> pageInfo = new PageInfo<>(filePojos);
        model.addAttribute("page", pageInfo);
        return "showFile";
    }
UploadController  ---上传功能

``` java
    @RequestMapping("/upload")
    public String upload(){
        return "upload";
    }
    @RequestMapping(value = "/upload" ,method = RequestMethod.POST)
    public String upload(Model model, HttpServletRequest request, @RequestParam("file")MultipartFile file){
        try {
            String fileName = file.getOriginalFilename();
            String finalfilename = fileName.substring(fileName.lastIndexOf("."));
            String ext = System.currentTimeMillis()+finalfilename;

            String destFileName = request.getServletContext().getRealPath("") + "upload" + File.separator+ext;
            File destFile = new File(destFileName);
            destFile.getParentFile().mkdir();
            file.transferTo(destFile);
            //保存文件名到数据库
            FilePojo filePojo = new FilePojo();
            long date = System.currentTimeMillis();
            Date time = new Date(date);
            String name = System.currentTimeMillis() + "\\" + finalfilename;
            filePojo.setFilename(ext);
            filePojo.setDate(time);
            fileMapper.insert(filePojo);
        } catch (Exception e) {
            e.printStackTrace();
            return "上传失败" + e.getMessage();
        }
        return "redirect:showFile";
    }

showFile.jsp ---文件展示页面,暂时没做成百度云盘那样的效果,太晚了,要睡觉了

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>文件上传成功</title>
</head>
<body>
    <a href="/upload">上传文件</a>
    <div align="center">
        <c:forEach items="${page.list}" var="c" varStatus="st">
            <img src="/upload/${c.filename}"> <strong><a href="/rename?id=${c.id}">重命名</a> | <a href="/delete?id=${c.id}">删除</a> </strong>  <br>
        </c:forEach>
        <a href="?start=1">首页</a>
        <a href="?start=${filename.pageNum -1}">上一页</a>
        <a href="?start=${filename.pageNum +1}">下一页</a>
        <a href="?start=${filename.pages}">尾页</a>
    </div>
</body>
</html>

upload.jsp ---上传页面,反正比较丑就是了

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>上传</title>
</head>
<body>
<div align="center">

    <form action="/upload" method="post" enctype="multipart/form-data">
        选择图片:<input type="file" name="file" accept="*/*">
        <input type="submit" value="上传">
    </form>
</div>
</body>
</html>

OK,这样我们的文件上传功能就开发完了,当然有些地方还是可以优化的,不过,,,明天再来战吧。
这个项目里面用到的都是今天学的东西,感觉受益良多,决定慢慢把它完善。顺便后面还得写下载,权限控制等等。留坑,等后面来填

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Copyright bcfou.com.Some Rights Reserved.