上传与下载
package com. zkw. controller ;
import cn. hutool. core. io. FileUtil ;
import cn. hutool. core. util. IdUtil ;
import cn. hutool. core. util. StrUtil ;
import cn. hutool. crypto. SecureUtil ;
import com. baomidou. mybatisplus. core. conditions. query. QueryWrapper ;
import com. zkw. Mapper. FileMapper ;
import com. zkw. entity. FileEntity ;
import org. springframework. beans. factory. annotation. Value ;
import org. springframework. web. bind. annotation. * ;
import org. springframework. web. multipart. MultipartFile ;
import javax. annotation. Resource ;
import javax. servlet. ServletOutputStream ;
import javax. servlet. http. HttpServletRequest ;
import javax. servlet. http. HttpServletResponse ;
import java. io. File ;
import java. io. IOException ;
import java. net. URLEncoder ;
import java. util. List ;
@RestController
@RequestMapping ( "/file" )
public class TestFileController {
@Value ( "${file.upload.path}" )
private String fileUploadPath;
@Resource
private FileMapper fileMapper;
@PostMapping ( "/upload" )
public String upload ( @RequestParam MultipartFile file, HttpServletRequest request) {
String filename = file. getOriginalFilename ( ) ;
String type = FileUtil . extName ( filename) ;
long size = file. getSize ( ) ;
File uploadFile = new File ( fileUploadPath) ;
if ( ! uploadFile. exists ( ) ) {
uploadFile. mkdirs ( ) ;
}
String uuid = IdUtil . fastSimpleUUID ( ) ;
String fileUUID = uuid + StrUtil . DOT + type;
File uploadFilePath = new File ( fileUploadPath + fileUUID) ;
try {
file. transferTo ( uploadFilePath) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
String md5 = SecureUtil . md5 ( uploadFilePath) ;
String url = null ;
QueryWrapper < FileEntity > wrapper = new QueryWrapper < > ( ) ;
wrapper. eq ( "md5" , md5) ;
List < FileEntity > list = fileMapper. selectList ( wrapper) ;
FileEntity fileEntity = list. size ( ) == 0 ? null : list. get ( 0 ) ;
StringBuffer oldUrl = request. getRequestURL ( ) ;
StringBuffer newUrl = oldUrl. replace ( oldUrl. indexOf ( "upload" ) , oldUrl. length ( ) , fileUUID) ;
if ( fileEntity == null ) {
url = newUrl + "" ;
} else {
uploadFilePath. delete ( ) ;
url = fileEntity. getUrl ( ) ;
}
FileEntity saveFiles = new FileEntity ( ) ;
saveFiles. setName ( filename) ;
saveFiles. setType ( type) ;
saveFiles. setSize ( size / 1024 ) ;
saveFiles. setUrl ( url) ;
saveFiles. setMd5 ( md5) ;
fileMapper. insert ( saveFiles) ;
return saveFiles. getUrl ( ) ;
}
@GetMapping ( "/{fileUUID}" )
public void download ( @PathVariable String fileUUID, HttpServletResponse response) {
File uploadFile = new File ( fileUploadPath + fileUUID) ;
ServletOutputStream os = null ;
try {
os = response. getOutputStream ( ) ;
response. addHeader ( "Content-Disposition" , "attachment;filename" + URLEncoder . encode ( fileUUID, "UTF-8" ) ) ;
response. setContentType ( "application/octet-stream" ) ;
os. write ( FileUtil . readBytes ( uploadFile) ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
} finally {
try {
os. flush ( ) ;
os. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
}
yaml 文件配置
spring :
datasource :
url : jdbc: mysql: //localhost: 3306/test? useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username : root
password : 123456
driver-class-name : com.mysql.cj.jdbc.Driver
file :
upload :
path : D: \files\
mybatis-plus 配置
import com. baomidou. mybatisplus. annotation. DbType ;
import com. baomidou. mybatisplus. extension. plugins. MybatisPlusInterceptor ;
import com. baomidou. mybatisplus. extension. plugins. inner. PaginationInnerInterceptor ;
import org. mybatis. spring. annotation. MapperScan ;
import org. springframework. context. annotation. Bean ;
import org. springframework. context. annotation. Configuration ;
@Configuration
@MapperScan ( "com.zkw.mapper" )
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor ( ) {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor ( ) ;
interceptor. addInnerInterceptor ( new PaginationInnerInterceptor ( DbType . MYSQL) ) ;
return interceptor;
}
}
FileMapper.java 的写法
import com. baomidou. mybatisplus. core. mapper. BaseMapper ;
import com. zkw. entity. FileEntity ;
public interface FileMapper extends BaseMapper < FileEntity > {
}
所使用到的依赖
< dependency>
< groupId> org.mybatis.spring.boot</ groupId>
< artifactId> mybatis-spring-boot-starter</ artifactId>
< version> 2.3.1</ version>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< version> 8.0.22</ version>
</ dependency>
< dependency>
< groupId> cn.hutool</ groupId>
< artifactId> hutool-all</ artifactId>
< version> 5.7.20</ version>
</ dependency>
< dependency>
< groupId> org.apache.poi</ groupId>
< artifactId> poi-ooxml</ artifactId>
< version> 4.1.2</ version>
</ dependency>
< dependency>
< groupId> com.baomidou</ groupId>
< artifactId> mybatis-plus-boot-starter</ artifactId>
< version> 3.5.1</ version>
</ dependency>
sql 文件
SET FOREIGN_KEY_CHECKS= 0 ;
DROP TABLE IF EXISTS ` t_file` ;
CREATE TABLE ` t_file` (
` id` int NOT NULL AUTO_INCREMENT ,
` name` varchar ( 255 ) DEFAULT NULL ,
` type` varchar ( 255 ) DEFAULT NULL ,
` size` bigint DEFAULT NULL ,
` url` varchar ( 255 ) DEFAULT NULL ,
` md5` varchar ( 255 ) DEFAULT NULL ,
PRIMARY KEY ( ` id` )
) ENGINE = InnoDB AUTO_INCREMENT = 17 DEFAULT CHARSET = utf8;