准备
docker运行mysql
$ docker run --name local-mysql -p 3306:3306 -v /Users/lee/Documents/AAA-PLee/C-Code/mysql/conf.d:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 -d mysql
设置MySQL时区
添加修改
/mysql/conf.d/xxx.cnf
文件,并重启MySQL
[mysqld]
default-time_zone = '+8:00'
查看当前MySQL使用的时区
SELECT @@global.time_zone; # +08:00
MySQL建库建表
create database java_app;
create table java_app.file_table
(
id varchar(255) not null primary key, -- 文件ID
name varchar(255) not null, -- 文件真实名称
extension varchar(10) null, -- 文件扩展名
type varchar(255) null, -- 文件类型
size int not null, -- 文件大小(单位:字节)
status int default 1 not null, -- 文件当前状态(0删除,1存在)
createTime datetime default NOW() not null, -- 文件创建时间
updateTime datetime null -- 文件修改时间
);
定义两个实体类
数据表实体类
注意:
规定参数时区
com/example/user/entity/FileTable.java
package com.example.user.entity;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.util.Date;
import java.io.Serializable;
/**
* (FileTable)实体类
*/
public class FileTable implements Serializable {
private String id;
private String name;
private String extension;
private String type;
private Integer size;
private Integer status;
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
private Date updateTime;
public FileTable(String id, String name, String extension, String type, Integer size) {
this.id = id;
this.name = name;
this.extension = extension;
this.type = type;
this.size = size;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getExtension() {
return extension;
}
public void setExtension(String extension) {
this.extension = extension;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Integer getSize() {
return size;
}
public void setSize(Integer size) {
this.size = size;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
}
查询条件实体类
com/example/user/entity/FileTableCondition.java
package com.example.user.entity;
import java.io.Serializable;
import java.util.Date;
/**
* 查询条件
*/
public class FileTableCondition implements Serializable {
private String id;
private String name;
private String extension;
private String type;
private Integer minSize;
private Integer maxSize;
private Integer status;
private Date startCreateTime;
private Date endCreateTime;
private Date startUpdateTime;
private Date endUpdateTime;
public FileTableCondition(
String id, String name, String extension, String type,
Integer status,
Integer minSize, Integer maxSize,
Date startCreateTime, Date endCreateTime,
Date startUpdateTime, Date endUpdateTime
) {
this.id = id;
this.name = name;
this.extension = extension;
this.type = type;
this.status = status;
this.minSize = minSize;
this.maxSize = maxSize;
this.startCreateTime = startCreateTime;
this.endCreateTime = endCreateTime;
this.startUpdateTime = startUpdateTime;
this.endUpdateTime = endUpdateTime;
}
}
工具类
com/example/user/utils/FileUtil.java
package com.example.user.utils;
import com.example.user.entity.FileTable;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
/**
* 文件工具类
*/
public class FileUtil {
// 文件保存路径
private static final String FILE_PATH = "/Users/lee/Documents/AAA-PLee/C-Code/java-app/UploadFiles/";
/**
* 保存文件
*
* @param fileId 文件ID
* @param multipartFile 文件
* @return 文件信息
* @throws IOException 错误信息
*/
public static FileTable saveFile(String fileId, MultipartFile multipartFile) throws IOException {
// 文件完整名称
String originalFilename = multipartFile.getOriginalFilename();
// 断言文件名不为空
assert originalFilename != null;
// 随机ID作为文件名
String id = null == fileId || fileId.isEmpty() ? UUID.randomUUID().toString() : fileId;
// 文件名
String name = originalFilename.substring(0, originalFilename.lastIndexOf("."));
// 文件扩展名
String extension = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
// 文件类型
String type = multipartFile.getContentType();
// 文件大小(单位:字节)
int size = (int) multipartFile.getSize();
// 目标存储位置
String path = FILE_PATH + id;
// 把上传的文件转存到目标目录
multipartFile.transferTo(new File(path));
// 实例化文件信息
return new FileTable(id, name, extension, type, size);
}
/**
* 下载文件
*
* @param fileTable 实例对象
* @return 文件流
*/
public static ResponseEntity<FileSystemResource> downloadFile(FileTable fileTable) {
File file = new File(FILE_PATH + fileTable.getId());
FileSystemResource fileSystemResource = new FileSystemResource(file);
String fileName = fileTable.getName() + "." + fileTable.getExtension();
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attachment; filename=" + fileName);
headers.add("Pragma", "no-cache");
headers.add("Cache-Control", "no-cache");
headers.add("Expires", "0");
return ResponseEntity
.ok()
.headers(headers)
.contentLength(file.length())
.contentType(MediaType.parseMediaType(fileTable.getType()))
.body(fileSystemResource);
}
}
用到的SQL语句
mapper user/src/main/resources/mapper/FileTableDao.xml
注意:
Integer
和int
区别
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.user.dao.FileTableDao">
<resultMap type="com.example.user.entity.FileTable" id="FileTableMap">
<result property="id" column="id" jdbcType="VARCHAR"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
<result property="extension" column="extension" jdbcType="VARCHAR"/>
<result property="type" column="type" jdbcType="VARCHAR"/>
<result property="size" column="size" jdbcType="INTEGER"/>
<result property="status" column="status" jdbcType="INTEGER"/>
<result property="createTime" column="createTime" jdbcType="TIMESTAMP"/>
<result property="updateTime" column="updateTime" jdbcType="TIMESTAMP"/>
</resultMap>
<!--新增一条数据-->
<insert id="insert" keyProperty="" useGeneratedKeys="true">
insert into file_table(id, name, extension, type, size)
values (#{id}, #{name}, #{extension}, #{type}, #{size})
</insert>
<!--新增多条数据-->
<insert id="insertList" parameterType="java.util.List">
insert into file_table(id, name, extension, type, size)
values
<foreach collection="list" item="item" index="index" separator=",">
(#{item.id}, #{item.name}, #{item.extension}, #{item.type}, #{item.size})
</foreach>
</insert>
<!--通过文件ID删除-->
<delete id="deleteById">
update file_table set status = 0, updateTime = NOW() where id = #{id}
</delete>
<!--通过文件ID修改数据-->
<update id="update">
update file_table
<set>
updateTime = NOW(),
<if test="name != null and name != ''">
name = #{name},
</if>
<if test="extension != null and extension != ''">
extension = #{extension},
</if>
<if test="type != null and type != ''">
type = #{type},
</if>
<if test="size != null and size != ''">
size = #{size},
</if>
<if test="status != null and status != ''">
status = #{status}
</if>
</set>
where id = #{id}
</update>
<!--根据ID查询数据-->
<select id="queryById" resultMap="FileTableMap">
select * from file_table
<where>
id = #{id}
</where>
</select>
<!--根据ID查询数据-->
<select id="queryByIds" resultMap="FileTableMap">
select * from file_table
<where>
<foreach collection="list" item="id" index="index">
<if test="id != null and id != ''">
or id = #{id}
</if>
</foreach>
</where>
</select>
<!--条件查询数据-->
<select id="queryByCondition" resultMap="FileTableMap">
select * from file_table
<where>
<if test="id != null and id != ''">
and id = #{id}
</if>
<if test="name != null and name != ''">
and name = #{name}
</if>
<if test="extension != null and extension != ''">
and extension = #{extension}
</if>
<if test="type != null and type != ''">
and type = #{type}
</if>
<if test="status != null">
and status = #{status}
</if>
<if test="minSize != null">
and size <![CDATA[ >= ]]> #{minSize}
</if>
<if test="maxSize != null">
and size <![CDATA[ <= ]]> #{maxSize}
</if>
<if test="startCreateTime != null">
and DATE_FORMAT(createTime, '%Y-%m-%d') <![CDATA[ >= ]]> DATE_FORMAT(#{startCreateTime}, '%Y-%m-%d')
</if>
<if test="endCreateTime != null">
and DATE_FORMAT(createTime, '%Y-%m-%d') <![CDATA[ <= ]]> DATE_FORMAT(#{endCreateTime}, '%Y-%m-%d')
</if>
<if test="startUpdateTime != null">
and DATE_FORMAT(updateTime, '%Y-%m-%d') <![CDATA[ >= ]]> DATE_FORMAT(#{startUpdateTime}, '%Y-%m-%d')
</if>
<if test="endUpdateTime != null">
and DATE_FORMAT(updateTime, '%Y-%m-%d') <![CDATA[ <= ]]> DATE_FORMAT(#{endUpdateTime}, '%Y-%m-%d')
</if>
</where>
</select>
</mapper>
增/改(上传)
controller com/example/user/controller/FileTableController.java
package com.example.user.controller;
import com.example.user.entity.FileTable;
import com.example.user.entity.FileTableCondition;
import com.example.user.service.FileTableService;
import org.springframework.core.io.FileSystemResource;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;
/**
* (FileTable)表控制层
*/
@RestController
@RequestMapping("fileTable")
public class FileTableController {
/**
* 服务对象
*/
@Resource
private FileTableService fileTableService;
/**
* 单/多文件上传 (新增/编辑)
*
* @param id 文件ID
* @param multipartFiles 文件
* @return 文件信息
*/
@PostMapping("/upload")
public ResponseEntity<Object> upload(String id, @RequestParam("file") MultipartFile[] multipartFiles) throws IOException {
if (null != id && !id.isEmpty() && multipartFiles.length > 0) {
return ResponseEntity.ok(this.fileTableService.upload(id, multipartFiles[0]));
} else {
if (multipartFiles.length > 1) {
return ResponseEntity.ok(this.fileTableService.upload("", multipartFiles));
} else {
return ResponseEntity.ok(this.fileTableService.upload("", multipartFiles[0]));
}
}
}
}
service com/example/user/service/impl/FileTableServiceImpl.java
package com.example.user.service.impl;
import com.example.user.dao.FileTableDao;
import com.example.user.entity.FileTable;
import com.example.user.entity.FileTableCondition;
import com.example.user.service.FileTableService;
import com.example.user.utils.FileUtil;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
/**
* (FileTable)表服务实现类
*/
@Service("filesService")
public class FileTableServiceImpl implements FileTableService {
@Resource
private FileTableDao fileTableDao;
/**
* 单文件上传
*
* @param id 文件ID
* @param multipartFile 文件
* @return 文件信息
*/
@Override
public FileTable upload(String id, MultipartFile multipartFile) throws IOException {
if (null == id || id.isEmpty()) {
FileTable fileTable = FileUtil.saveFile("", multipartFile);
this.fileTableDao.insert(fileTable);
return this.fileTableDao.queryById(fileTable.getId());
} else {
FileTable fileTable = FileUtil.saveFile(id, multipartFile);
this.fileTableDao.update(fileTable);
return this.fileTableDao.queryById(fileTable.getId());
}
}
/**
* 多文件上传
*
* @param id 文件ID
* @param multipartFiles 文件
* @return 文件信息
*/
@Override
public List<FileTable> upload(String id, MultipartFile[] multipartFiles) throws IOException {
List<FileTable> fileList = new ArrayList<>();
for (MultipartFile multipartFile : multipartFiles) {
fileList.add(FileUtil.saveFile("", multipartFile));
}
this.fileTableDao.insertList(fileList);
List<String> ids = fileList.stream().map(FileTable::getId).collect(Collectors.toList());
return this.fileTableDao.queryByIds(ids);
}
}
删
controller com/example/user/controller/FileTableController.java
package com.example.user.controller;
import com.example.user.entity.FileTable;
import com.example.user.entity.FileTableCondition;
import com.example.user.service.FileTableService;
import org.springframework.core.io.FileSystemResource;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;
/**
* (FileTable)表控制层
*/
@RestController
@RequestMapping("fileTable")
public class FileTableController {
/**
* 服务对象
*/
@Resource
private FileTableService fileTableService;
/**
* 删除数据
*
* @param id 文件ID
* @return 删除是否成功
*/
@DeleteMapping("{id}")
public ResponseEntity<Boolean> deleteById(@PathVariable("id") String id) {
return ResponseEntity.ok(this.fileTableService.deleteById(id));
}
}
service com/example/user/service/impl/FileTableServiceImpl.java
package com.example.user.service.impl;
import com.example.user.dao.FileTableDao;
import com.example.user.entity.FileTable;
import com.example.user.entity.FileTableCondition;
import com.example.user.service.FileTableService;
import com.example.user.utils.FileUtil;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
/**
* (FileTable)表服务实现类
*/
@Service("filesService")
public class FileTableServiceImpl implements FileTableService {
@Resource
private FileTableDao fileTableDao;
/**
* 通过文件ID删除数据
*
* @param id 文件ID
* @return 是否成功
*/
@Override
public boolean deleteById(String id) {
return this.fileTableDao.deleteById(id) > 0;
}
}
查
controller com/example/user/controller/FileTableController.java
注意:时间格式
package com.example.user.controller;
import com.example.user.entity.FileTable;
import com.example.user.entity.FileTableCondition;
import com.example.user.service.FileTableService;
import org.springframework.core.io.FileSystemResource;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;
/**
* (FileTable)表控制层
*/
@RestController
@RequestMapping("fileTable")
public class FileTableController {
/**
* 服务对象
*/
@Resource
private FileTableService fileTableService;
/**
* 条件查询
*
* @param id 文件ID
* @param name 文件真实名称
* @param extension 文件扩展名
* @param type 文件类型
* @param minSize 最小文件大小(单位:字节)
* @param maxSize 最大文件大小(单位:字节)
* @param status 文件当前状态(0删除,1存在)
* @param startCreateTime 文件创建开始时间
* @param endCreateTime 文件创建结束时间
* @param startUpdateTime 文件修改开始时间
* @param endUpdateTime 文件修改结束时间
* @return 文件信息
*/
@GetMapping
public ResponseEntity<List<FileTable>> queryByCondition(
@RequestParam(required = false) String id,
@RequestParam(required = false) String name,
@RequestParam(required = false) String extension,
@RequestParam(required = false) String type,
@RequestParam(required = false) Integer status,
@RequestParam(required = false) Integer minSize,
@RequestParam(required = false) Integer maxSize,
@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startCreateTime,
@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endCreateTime,
@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startUpdateTime,
@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endUpdateTime
) {
FileTableCondition fileTableCondition = new FileTableCondition(
id, name, extension, type,
status,
minSize, maxSize,
startCreateTime, endCreateTime,
startUpdateTime, endUpdateTime
);
return ResponseEntity.ok(this.fileTableService.queryByCondition(fileTableCondition));
}
}
service com/example/user/service/impl/FileTableServiceImpl.java
package com.example.user.service.impl;
import com.example.user.dao.FileTableDao;
import com.example.user.entity.FileTable;
import com.example.user.entity.FileTableCondition;
import com.example.user.service.FileTableService;
import com.example.user.utils.FileUtil;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
/**
* (FileTable)表服务实现类
*/
@Service("filesService")
public class FileTableServiceImpl implements FileTableService {
@Resource
private FileTableDao fileTableDao;
/**
* 条件查询
*
* @param fileTableCondition 实例对象
* @return 文件信息
*/
@Override
public List<FileTable> queryByCondition(FileTableCondition fileTableCondition) {
return this.fileTableDao.queryByCondition(fileTableCondition);
}
}
下载
controller com/example/user/controller/FileTableController.java
package com.example.user.controller;
import com.example.user.entity.FileTable;
import com.example.user.entity.FileTableCondition;
import com.example.user.service.FileTableService;
import org.springframework.core.io.FileSystemResource;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;
/**
* (FileTable)表控制层
*/
@RestController
@RequestMapping("fileTable")
public class FileTableController {
/**
* 服务对象
*/
@Resource
private FileTableService fileTableService;
@GetMapping("/download/{id}")
public ResponseEntity<FileSystemResource> download(@PathVariable("id") String id){
return this.fileTableService.downloadById(id);
}
}
service com/example/user/service/impl/FileTableServiceImpl.java
package com.example.user.service.impl;
import com.example.user.dao.FileTableDao;
import com.example.user.entity.FileTable;
import com.example.user.entity.FileTableCondition;
import com.example.user.service.FileTableService;
import com.example.user.utils.FileUtil;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
/**
* (FileTable)表服务实现类
*/
@Service("filesService")
public class FileTableServiceImpl implements FileTableService {
@Resource
private FileTableDao fileTableDao;
/**
* 文件下载
*
* @param id 文件ID
* @return 文件流
*/
@Override
public ResponseEntity<FileSystemResource> downloadById(String id) {
FileTable fileTable = this.fileTableDao.queryById(id);
return FileUtil.downloadFile(fileTable);
}
}
完整源码
目录结构
源码
user/src/main/resources/application.yml
server:
port: 8000
spring:
application:
name: user-service
datasource:
url: jdbc:mysql://127.0.0.1:3306/java_app
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:**/mapper/*.xml
com/example/user/UserApplication.java
package com.example.user;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan(basePackages = "com.example.user.dao")
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}
com/example/user/controller/FileTableController.java
package com.example.user.controller;
import com.example.user.entity.FileTable;
import com.example.user.entity.FileTableCondition;
import com.example.user.service.FileTableService;
import org.springframework.core.io.FileSystemResource;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;
/**
* (FileTable)表控制层
*/
@RestController
@RequestMapping("fileTable")
public class FileTableController {
/**
* 服务对象
*/
@Resource
private FileTableService fileTableService;
/**
* 单/多文件上传 (新增/编辑)
*
* @param id 文件ID
* @param multipartFiles 文件
* @return 文件信息
*/
@PostMapping("/upload")
public ResponseEntity<Object> upload(String id, @RequestParam("file") MultipartFile[] multipartFiles) throws IOException {
if (null != id && !id.isEmpty() && multipartFiles.length > 0) {
return ResponseEntity.ok(this.fileTableService.upload(id, multipartFiles[0]));
} else {
if (multipartFiles.length > 1) {
return ResponseEntity.ok(this.fileTableService.upload("", multipartFiles));
} else {
return ResponseEntity.ok(this.fileTableService.upload("", multipartFiles[0]));
}
}
}
/**
* 删除数据
*
* @param id 文件ID
* @return 删除是否成功
*/
@DeleteMapping("{id}")
public ResponseEntity<Boolean> deleteById(@PathVariable("id") String id) {
return ResponseEntity.ok(this.fileTableService.deleteById(id));
}
/**
* 条件查询
*
* @param id 文件ID
* @param name 文件真实名称
* @param extension 文件扩展名
* @param type 文件类型
* @param minSize 最小文件大小(单位:字节)
* @param maxSize 最大文件大小(单位:字节)
* @param status 文件当前状态(0删除,1存在)
* @param startCreateTime 文件创建开始时间
* @param endCreateTime 文件创建结束时间
* @param startUpdateTime 文件修改开始时间
* @param endUpdateTime 文件修改结束时间
* @return 文件信息
*/
@GetMapping
public ResponseEntity<List<FileTable>> queryByCondition(
@RequestParam(required = false) String id,
@RequestParam(required = false) String name,
@RequestParam(required = false) String extension,
@RequestParam(required = false) String type,
@RequestParam(required = false) Integer status,
@RequestParam(required = false) Integer minSize,
@RequestParam(required = false) Integer maxSize,
@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startCreateTime,
@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endCreateTime,
@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startUpdateTime,
@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endUpdateTime
) {
FileTableCondition fileTableCondition = new FileTableCondition(
id, name, extension, type,
status,
minSize, maxSize,
startCreateTime, endCreateTime,
startUpdateTime, endUpdateTime
);
return ResponseEntity.ok(this.fileTableService.queryByCondition(fileTableCondition));
}
@GetMapping("/download/{id}")
public ResponseEntity<FileSystemResource> download(@PathVariable("id") String id){
return this.fileTableService.downloadById(id);
}
}
com/example/user/service/FileTableService.java
package com.example.user.service;
import com.example.user.entity.FileTable;
import com.example.user.entity.FileTableCondition;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
/**
* (FileTable)表服务接口
*/
public interface FileTableService {
/**
* 单文件上传
*
* @param id 文件ID
* @param multipartFile 文件
* @return 文件信息
*/
FileTable upload(String id, MultipartFile multipartFile) throws IOException;
/**
* 多文件上传
*
* @param id 文件ID
* @param multipartFiles 文件
* @return 文件信息
*/
List<FileTable> upload(String id, MultipartFile[] multipartFiles) throws IOException;
/**
* 通过主键删除数据
*
* @param id 文件ID
* @return 是否成功
*/
boolean deleteById(String id);
/**
* 条件查询
*
* @param fileTableCondition 实例对象
* @return 文件信息
*/
List<FileTable> queryByCondition(FileTableCondition fileTableCondition);
/**
* 文件下载
*
* @param id 文件ID
* @return 文件流
*/
ResponseEntity<FileSystemResource> downloadById(String id);
}
com/example/user/service/impl/FileTableServiceImpl.java
文章来源:https://www.toymoban.com/news/detail-486189.html
package com.example.user.service.impl;
import com.example.user.dao.FileTableDao;
import com.example.user.entity.FileTable;
import com.example.user.entity.FileTableCondition;
import com.example.user.service.FileTableService;
import com.example.user.utils.FileUtil;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
/**
* (FileTable)表服务实现类
*/
@Service("filesService")
public class FileTableServiceImpl implements FileTableService {
@Resource
private FileTableDao fileTableDao;
/**
* 单文件上传
*
* @param id 文件ID
* @param multipartFile 文件
* @return 文件信息
*/
@Override
public FileTable upload(String id, MultipartFile multipartFile) throws IOException {
if (null == id || id.isEmpty()) {
FileTable fileTable = FileUtil.saveFile("", multipartFile);
this.fileTableDao.insert(fileTable);
return this.fileTableDao.queryById(fileTable.getId());
} else {
FileTable fileTable = FileUtil.saveFile(id, multipartFile);
this.fileTableDao.update(fileTable);
return this.fileTableDao.queryById(fileTable.getId());
}
}
/**
* 多文件上传
*
* @param id 文件ID
* @param multipartFiles 文件
* @return 文件信息
*/
@Override
public List<FileTable> upload(String id, MultipartFile[] multipartFiles) throws IOException {
List<FileTable> fileList = new ArrayList<>();
for (MultipartFile multipartFile : multipartFiles) {
fileList.add(FileUtil.saveFile("", multipartFile));
}
this.fileTableDao.insertList(fileList);
List<String> ids = fileList.stream().map(FileTable::getId).collect(Collectors.toList());
return this.fileTableDao.queryByIds(ids);
}
/**
* 通过文件ID删除数据
*
* @param id 文件ID
* @return 是否成功
*/
@Override
public boolean deleteById(String id) {
return this.fileTableDao.deleteById(id) > 0;
}
/**
* 条件查询
*
* @param fileTableCondition 实例对象
* @return 文件信息
*/
@Override
public List<FileTable> queryByCondition(FileTableCondition fileTableCondition) {
return this.fileTableDao.queryByCondition(fileTableCondition);
}
/**
* 文件下载
*
* @param id 文件ID
* @return 文件流
*/
@Override
public ResponseEntity<FileSystemResource> downloadById(String id) {
FileTable fileTable = this.fileTableDao.queryById(id);
return FileUtil.downloadFile(fileTable);
}
}
com/example/user/dao/FileTableDao.java
文章来源地址https://www.toymoban.com/news/detail-486189.html
package com.example.user.dao;
import com.example.user.entity.FileTable;
import com.example.user.entity.FileTableCondition;
import java.util.List;
/**
* (FileTable)表数据库访问层
*/
public interface FileTableDao {
/**
* 新增数据
*
* @param files 实例对象
* @return 影响行数
*/
int insert(FileTable files);
/**
* 新增数据
*
* @param filesList 实例对象
* @return 影响行数
*/
int insertList(List<FileTable> filesList);
/**
* 通过主键删除数据
*
* @param id 文件ID
* @return 影响行数
*/
int deleteById(String id);
/**
* 修改数据
*
* @param fileTable 实例对象
* @return 影响行数
*/
int update(FileTable fileTable);
/**
* 按ID查询数据
*
* @param id 文件ID
* @return 对象列表
*/
FileTable queryById(String id);
/**
* 按ID查询数据
*
* @param ids 文件ID
* @return 对象列表
*/
List<FileTable> queryByIds(List<String> ids);
/**
* 按条件查询数据
*
* @param fileTableCondition 查询条件
* @return 对象列表
*/
List<FileTable> queryByCondition(FileTableCondition fileTableCondition);
}
到了这里,关于Java(二):Spring Boot 项目-文件的增删改查下载的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!