ES操作测试通过

This commit is contained in:
2018-06-22 19:34:56 +08:00
commit 8a59fcaf8d
31 changed files with 1092 additions and 0 deletions

View 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);
}
}

View 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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,6 @@
package top.fjy8018.fileupload.enums;
public interface CodeEnum<T> {
T getCode();
T getMsg();
}

View 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;
}
}

View File

@@ -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();
}
}

View File

@@ -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());
}
}

View File

@@ -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> {
}

View File

@@ -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 {
}

View File

@@ -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> {
}

View File

@@ -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);
}

View 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);
}
}

View 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;
}
}

View 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);
}
}