ES操作测试通过
This commit is contained in:
12
src/main/java/top/fjy8018/fileupload/EstestApplication.java
Normal file
12
src/main/java/top/fjy8018/fileupload/EstestApplication.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package top.fjy8018.fileupload;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class EstestApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(EstestApplication.class, args);
|
||||
}
|
||||
}
|
||||
25
src/main/java/top/fjy8018/fileupload/VO/ResultVO.java
Normal file
25
src/main/java/top/fjy8018/fileupload/VO/ResultVO.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package top.fjy8018.fileupload.VO;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* http请求返回的最外层对象 ViewObject
|
||||
* @author F嘉阳
|
||||
* @date 2018-06-22 09:34
|
||||
*/
|
||||
@Data
|
||||
public class ResultVO<T> implements Serializable{
|
||||
|
||||
private static final long serialVersionUID = 2015767657112436515L;
|
||||
|
||||
/** 错误码. **/
|
||||
private Integer code;
|
||||
|
||||
/** 提示信息. **/
|
||||
private String msg;
|
||||
|
||||
/** 返回的内容. **/
|
||||
private T data;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package top.fjy8018.fileupload.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author F嘉阳
|
||||
* @date 2018-06-22 09:42
|
||||
*/
|
||||
@Data
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "serverproperties")
|
||||
public class ServerPropertiesConfig {
|
||||
|
||||
private String portNum;
|
||||
|
||||
private String filePath;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package top.fjy8018.fileupload.dataobject;
|
||||
|
||||
import lombok.Data;
|
||||
import org.hibernate.annotations.DynamicUpdate;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
/**
|
||||
* @author F嘉阳
|
||||
* @date 2018-06-22 10:49
|
||||
*/
|
||||
@Data
|
||||
@Entity
|
||||
@DynamicUpdate
|
||||
public class AdminInfo {
|
||||
@Id
|
||||
private String adminId;
|
||||
|
||||
private String userId;
|
||||
|
||||
private String createTime;
|
||||
|
||||
private String updateTime;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package top.fjy8018.fileupload.dataobject;
|
||||
|
||||
import lombok.Data;
|
||||
import org.hibernate.annotations.DynamicUpdate;
|
||||
|
||||
import javax.persistence.Id;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 文件表
|
||||
* @author F嘉阳
|
||||
* @date 2018-06-22 10:43
|
||||
*/
|
||||
@Data
|
||||
/*@Entity*/
|
||||
@DynamicUpdate
|
||||
public class FileInfo {
|
||||
|
||||
@Id
|
||||
private String fileId;
|
||||
|
||||
private String userId;
|
||||
|
||||
private Date createTime;
|
||||
|
||||
private String fileName;
|
||||
|
||||
private String fileSize;
|
||||
|
||||
private String filePath;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package top.fjy8018.fileupload.dataobject;
|
||||
|
||||
import lombok.Data;
|
||||
import org.hibernate.annotations.DynamicUpdate;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 系统A用户表
|
||||
* @author F嘉阳
|
||||
* @date 2018-06-22 10:46
|
||||
*/
|
||||
@Data
|
||||
@Entity
|
||||
@DynamicUpdate
|
||||
public class UserInfo {
|
||||
|
||||
@Id
|
||||
private String userId;
|
||||
|
||||
private String username;
|
||||
|
||||
private String password;
|
||||
|
||||
private Date createTime;
|
||||
|
||||
private Date updateTime;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package top.fjy8018.fileupload.dataobject.es;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.elasticsearch.annotations.Document;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author F嘉阳
|
||||
* @date 2018-06-22 11:34
|
||||
*/
|
||||
@Data
|
||||
@Document(indexName = "file",type = "file")
|
||||
public class EsFileInfo implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 3216398036847369019L;
|
||||
|
||||
@Id
|
||||
private String fileId;
|
||||
|
||||
private String userId;
|
||||
|
||||
private String createTime = currentTimeString();
|
||||
|
||||
private String fileName;
|
||||
|
||||
private String fileSize;
|
||||
|
||||
private String filePath;
|
||||
|
||||
protected EsFileInfo() {
|
||||
}
|
||||
|
||||
public EsFileInfo(String userId, String fileName, String fileSize, String filePath) {
|
||||
this.userId = userId;
|
||||
this.fileName = fileName;
|
||||
this.fileSize = fileSize;
|
||||
this.filePath = filePath;
|
||||
}
|
||||
|
||||
public String currentTimeString(){
|
||||
Date date = new Date();
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
return sdf.format(date);
|
||||
}
|
||||
}
|
||||
6
src/main/java/top/fjy8018/fileupload/enums/CodeEnum.java
Normal file
6
src/main/java/top/fjy8018/fileupload/enums/CodeEnum.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package top.fjy8018.fileupload.enums;
|
||||
|
||||
public interface CodeEnum<T> {
|
||||
T getCode();
|
||||
T getMsg();
|
||||
}
|
||||
38
src/main/java/top/fjy8018/fileupload/enums/ResultVOEnum.java
Normal file
38
src/main/java/top/fjy8018/fileupload/enums/ResultVOEnum.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package top.fjy8018.fileupload.enums;
|
||||
|
||||
/**
|
||||
* 异常枚举类
|
||||
* @author F嘉阳
|
||||
* @date 2018-06-22 09:28
|
||||
*/
|
||||
public enum ResultVOEnum implements CodeEnum {
|
||||
UPLOAD_SUCCESS(0,"上传成功"),
|
||||
UPLOAD_FAIL(-1,"上传失败"),
|
||||
;
|
||||
|
||||
private Integer code;
|
||||
private String msg;
|
||||
|
||||
ResultVOEnum(Integer code, String msg) {
|
||||
this.code = code;
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(Integer code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMsg() {
|
||||
return msg;
|
||||
}
|
||||
|
||||
public void setMsg(String msg) {
|
||||
this.msg = msg;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package top.fjy8018.fileupload.exception;
|
||||
|
||||
import lombok.Getter;
|
||||
import top.fjy8018.fileupload.enums.ResultVOEnum;
|
||||
|
||||
/**
|
||||
* 文件上传异常类
|
||||
* @author F嘉阳
|
||||
* @date 2018-06-22 09:27
|
||||
*/
|
||||
@Getter
|
||||
public class FileUploadException extends RuntimeException {
|
||||
|
||||
private Integer code;
|
||||
|
||||
/**
|
||||
* Constructs a new runtime exception with {@code null} as its
|
||||
* detail message. The cause is not initialized, and may subsequently be
|
||||
* initialized by a call to {@link #initCause}.
|
||||
*/
|
||||
public FileUploadException(ResultVOEnum resultVOEnum) {
|
||||
super(resultVOEnum.getMsg());
|
||||
this.code = resultVOEnum.getCode();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package top.fjy8018.fileupload.handler;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import top.fjy8018.fileupload.VO.ResultVO;
|
||||
import top.fjy8018.fileupload.exception.FileUploadException;
|
||||
import top.fjy8018.fileupload.util.ResultVOUtil;
|
||||
|
||||
/**
|
||||
* 异常处理
|
||||
* @author F嘉阳
|
||||
* @date 2018-06-22 09:32
|
||||
*/
|
||||
@Slf4j
|
||||
@ControllerAdvice
|
||||
public class ExceptionHandle {
|
||||
|
||||
@ResponseBody
|
||||
@ExceptionHandler(value = FileUploadException.class)
|
||||
public ResultVO handle(FileUploadException e){
|
||||
return ResultVOUtil.error(e.getCode(),e.getMessage());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package top.fjy8018.fileupload.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import top.fjy8018.fileupload.dataobject.AdminInfo;
|
||||
|
||||
public interface AdminInfoRepository extends JpaRepository<AdminInfo,String> {
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
|
||||
package top.fjy8018.fileupload.repository;
|
||||
/*
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import top.fjy8018.fileupload.dataobject.FileInfo;
|
||||
|
||||
public interface FileInfoRepository extends JpaRepository<FileInfo,String> {
|
||||
}
|
||||
*/
|
||||
public interface FileInfoRepository {
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package top.fjy8018.fileupload.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import top.fjy8018.fileupload.dataobject.UserInfo;
|
||||
|
||||
public interface UserInfoRepository extends JpaRepository<UserInfo,String> {
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package top.fjy8018.fileupload.repository.es;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
|
||||
import top.fjy8018.fileupload.dataobject.es.EsFileInfo;
|
||||
|
||||
public interface EsFileInfoRepository extends ElasticsearchRepository<EsFileInfo,String> {
|
||||
/**
|
||||
* 通过文件名查找,并去除重复
|
||||
* fileName包含即可,非精确匹配
|
||||
* @param pageable
|
||||
* @param fileName
|
||||
* @return
|
||||
*/
|
||||
Page<EsFileInfo> findDistinctByFileNameContains(Pageable pageable, String fileName);
|
||||
|
||||
/**
|
||||
* 通过用户号查询,并去除重复
|
||||
* @param pageable
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
Page<EsFileInfo> findDistinctByUserId(Pageable pageable, String userId);
|
||||
|
||||
Page<EsFileInfo> findByFileName(Pageable pageable, String fileName);
|
||||
}
|
||||
27
src/main/java/top/fjy8018/fileupload/util/KeyUtil.java
Normal file
27
src/main/java/top/fjy8018/fileupload/util/KeyUtil.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package top.fjy8018.fileupload.util;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* 自动生成键(主键、外键等)
|
||||
*
|
||||
* @author F嘉阳
|
||||
* @date 2018-05-27 19:32
|
||||
*/
|
||||
public class KeyUtil {
|
||||
|
||||
/**
|
||||
* 生成唯一主键
|
||||
* 格式:时间+随机数
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static synchronized String genUniqueKey() {
|
||||
Random random = new Random();
|
||||
|
||||
// 生成6位随机数
|
||||
Integer number = random.nextInt(900000) + 100000;
|
||||
|
||||
return System.currentTimeMillis()+String.valueOf(number);
|
||||
}
|
||||
}
|
||||
45
src/main/java/top/fjy8018/fileupload/util/ResultVOUtil.java
Normal file
45
src/main/java/top/fjy8018/fileupload/util/ResultVOUtil.java
Normal file
@@ -0,0 +1,45 @@
|
||||
package top.fjy8018.fileupload.util;
|
||||
|
||||
import top.fjy8018.fileupload.VO.ResultVO;
|
||||
import top.fjy8018.fileupload.enums.ResultVOEnum;
|
||||
|
||||
/**
|
||||
* @author F嘉阳
|
||||
* @date 2018-06-22 09:37
|
||||
*/
|
||||
public class ResultVOUtil {
|
||||
|
||||
public static ResultVO success(Object object){
|
||||
ResultVO resultVO = new ResultVO();
|
||||
|
||||
resultVO.setCode(ResultVOEnum.UPLOAD_SUCCESS.getCode());
|
||||
resultVO.setMsg(ResultVOEnum.UPLOAD_SUCCESS.getMsg());
|
||||
resultVO.setData(object);
|
||||
return resultVO;
|
||||
}
|
||||
|
||||
public static ResultVO success(){
|
||||
ResultVO resultVO = new ResultVO();
|
||||
|
||||
resultVO.setCode(ResultVOEnum.UPLOAD_SUCCESS.getCode());
|
||||
resultVO.setMsg(ResultVOEnum.UPLOAD_SUCCESS.getMsg());
|
||||
resultVO.setData(null);
|
||||
return resultVO;
|
||||
}
|
||||
|
||||
public static ResultVO error(String msg){
|
||||
ResultVO resultVO = new ResultVO();
|
||||
|
||||
resultVO.setCode(ResultVOEnum.UPLOAD_FAIL.getCode());
|
||||
resultVO.setMsg(msg);
|
||||
return resultVO;
|
||||
}
|
||||
|
||||
public static ResultVO error(Integer code,String msg){
|
||||
ResultVO resultVO = new ResultVO();
|
||||
|
||||
resultVO.setCode(code);
|
||||
resultVO.setMsg(msg);
|
||||
return resultVO;
|
||||
}
|
||||
}
|
||||
17
src/main/java/top/fjy8018/fileupload/util/TimeUtil.java
Normal file
17
src/main/java/top/fjy8018/fileupload/util/TimeUtil.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package top.fjy8018.fileupload.util;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author F嘉阳
|
||||
* @date 2018-06-22 18:19
|
||||
*/
|
||||
public class TimeUtil {
|
||||
|
||||
public static String currentTimeString(){
|
||||
Date date = new Date();
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
return sdf.format(date);
|
||||
}
|
||||
}
|
||||
36
src/main/resources/application-dev.yml
Normal file
36
src/main/resources/application-dev.yml
Normal file
@@ -0,0 +1,36 @@
|
||||
#开发环境配置文件
|
||||
server:
|
||||
servlet:
|
||||
context-path: /fileupload
|
||||
port: 8080
|
||||
serverproperties:
|
||||
portNum: 8080
|
||||
filePath: F:\JAVA Workspace\Temp\
|
||||
spring:
|
||||
thymeleaf:
|
||||
prefix: classpath:/templates/
|
||||
cache: false
|
||||
check-template: true
|
||||
|
||||
servlet:
|
||||
multipart:
|
||||
max-file-size: 100Mb
|
||||
max-request-size: 100Mb
|
||||
datasource:
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
url: jdbc:mysql://localhost:3306/db_sysdemo1?useUnicode=true&characterEncoding=utf-8&useSSL=true
|
||||
username: trs
|
||||
password: fileupload
|
||||
jpa:
|
||||
hibernate:
|
||||
ddl-auto: update
|
||||
show-sql: true
|
||||
data:
|
||||
elasticsearch:
|
||||
cluster-name: Fjiayang
|
||||
cluster-nodes: 127.0.0.1:9300
|
||||
|
||||
|
||||
#resources:
|
||||
# static-locations: classpath:/templates/
|
||||
debug: true
|
||||
3
src/main/resources/application.yml
Normal file
3
src/main/resources/application.yml
Normal file
@@ -0,0 +1,3 @@
|
||||
spring:
|
||||
profiles:
|
||||
active: dev
|
||||
Reference in New Issue
Block a user