ES service测试完成,上传Controller编写完成
This commit is contained in:
@@ -2,15 +2,21 @@ package top.fjy8018.fileupload.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
||||
import top.fjy8018.fileupload.config.ServerPropertiesConfig;
|
||||
import top.fjy8018.fileupload.constant.GlobalConstant;
|
||||
import top.fjy8018.fileupload.dataobject.User;
|
||||
import top.fjy8018.fileupload.dataobject.es.EsFileInfo;
|
||||
import top.fjy8018.fileupload.service.FileService;
|
||||
import top.fjy8018.fileupload.util.FormatFileSizeUtil;
|
||||
import top.fjy8018.fileupload.util.TimeUtil;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -18,6 +24,7 @@ import java.util.Map;
|
||||
* @date 2018-06-23 09:16
|
||||
*/
|
||||
@Controller
|
||||
@RequestMapping("/upload")
|
||||
public class UploadController {
|
||||
|
||||
@Autowired
|
||||
@@ -26,17 +33,34 @@ public class UploadController {
|
||||
@Autowired
|
||||
private ServerPropertiesConfig serverPropertiesConfig;
|
||||
|
||||
@PostMapping("/add")
|
||||
public String moreUpload(HttpServletRequest request,
|
||||
@RequestParam(value = "fileName") String fileName) {
|
||||
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
|
||||
Map<String, MultipartFile> files = multipartHttpServletRequest.getFileMap();
|
||||
|
||||
EsFileInfo fileInfo = new EsFileInfo();
|
||||
fileInfo.setFileName(fileName);
|
||||
fileInfo.setFilePath(serverPropertiesConfig.getFilePath());
|
||||
/*fileInfo.setFileSize();
|
||||
fileInfo.setUserId();*/
|
||||
User user = (User)request.getSession().getAttribute(GlobalConstant.USER_SESSION_KEY);
|
||||
|
||||
EsFileInfo fileInfo = new EsFileInfo();
|
||||
|
||||
for (MultipartFile file : files.values()) {
|
||||
String pathname = serverPropertiesConfig.getFilePath()+fileName;
|
||||
File targetFile = new File(pathname);
|
||||
|
||||
fileInfo.setFileName(fileName);
|
||||
fileInfo.setFilePath(pathname);
|
||||
fileInfo.setFileSize(FormatFileSizeUtil.GetFileSize(file.getSize()));
|
||||
fileInfo.setUserId(user.getId());
|
||||
|
||||
// 写入ES
|
||||
fileService.saveFile(fileInfo);
|
||||
|
||||
try {
|
||||
file.transferTo(targetFile);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ import java.util.concurrent.TimeUnit;
|
||||
* @date 2018-06-22 11:34
|
||||
*/
|
||||
@Data
|
||||
@Document(indexName = "fileInfo",type = "file")
|
||||
@Document(indexName = "fileinfo",type = "file",shards = 3,replicas = 1)
|
||||
public class EsFileInfo implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 3216398036847369019L;
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package top.fjy8018.fileupload.util;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
|
||||
public class FormatFileSizeUtil {
|
||||
public static String GetFileSize(long sizes){
|
||||
String size = "";
|
||||
if(sizes!=0){
|
||||
long fileS = sizes;
|
||||
DecimalFormat df = new DecimalFormat("#.00");
|
||||
if (fileS < 1024) {
|
||||
size = df.format((double) fileS) + "BT";
|
||||
} else if (fileS < 1048576) {
|
||||
size = df.format((double) fileS / 1024) + "KB";
|
||||
} else if (fileS < 1073741824) {
|
||||
size = df.format((double) fileS / 1048576) + "MB";
|
||||
} else {
|
||||
size = df.format((double) fileS / 1073741824) +"GB";
|
||||
}
|
||||
}else{
|
||||
size = "非法!";
|
||||
}
|
||||
return size;
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,7 @@ public class EsFileInfoRepositoryTest extends EstestApplicationTests {
|
||||
/**
|
||||
* 测试数据
|
||||
*/
|
||||
@Test
|
||||
public void initRpositoryData(){
|
||||
// 清除所有数据
|
||||
repository.deleteAll();
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package top.fjy8018.fileupload.service.impl;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.stereotype.Component;
|
||||
import top.fjy8018.fileupload.EstestApplicationTests;
|
||||
import top.fjy8018.fileupload.dataobject.es.EsFileInfo;
|
||||
import top.fjy8018.fileupload.service.FileService;
|
||||
import top.fjy8018.fileupload.util.KeyUtil;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class FileServiceImplTest extends EstestApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private FileService fileService;
|
||||
|
||||
private PageRequest pageRequest = PageRequest.of(0,2);
|
||||
|
||||
private static final String USER_ID = "1529723184401294213";
|
||||
|
||||
@Test
|
||||
public void findByFileName() {
|
||||
String fileName = "文件";
|
||||
Page<EsFileInfo> res = fileService.findByFileName(pageRequest,fileName);
|
||||
|
||||
log.info("【查询结果】{},数量{}",res.getContent(),res.getTotalElements());
|
||||
Assert.assertNotEquals(0,res.getTotalElements());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findDistinctByUserId() {
|
||||
Page<EsFileInfo> res = fileService.findDistinctByUserId(pageRequest,USER_ID);
|
||||
|
||||
log.info("【查询结果】{},数量{}",res.getContent(),res.getTotalElements());
|
||||
Assert.assertNotEquals(0,res.getTotalElements());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void saveFile() {
|
||||
String fileName = "测试Service虚拟文件3";
|
||||
String fileSize = "31kb";
|
||||
String filePath = "/www/Service";
|
||||
EsFileInfo fileInfo = new EsFileInfo(USER_ID,fileName,fileSize,filePath);
|
||||
log.info("【fileInfo】{}",fileInfo.toString());
|
||||
|
||||
EsFileInfo res = fileService.saveFile(fileInfo);
|
||||
Assert.assertNotNull(res);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
public void deleteFile() {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteById() {
|
||||
//fileService.deleteById();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteAll() {
|
||||
fileService.deleteAll();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user