diff --git a/pom.xml b/pom.xml index 0fbc240..6087b9b 100644 --- a/pom.xml +++ b/pom.xml @@ -232,6 +232,12 @@ slf4j-api 1.7.21 + + commons-fileupload + commons-fileupload + 1.4 + test + ROOT diff --git a/src/main/java/com/yipin/liuwanr/controller/AliyunOssController.java b/src/main/java/com/yipin/liuwanr/controller/AliyunOssController.java index 74fedab..4595c11 100644 --- a/src/main/java/com/yipin/liuwanr/controller/AliyunOssController.java +++ b/src/main/java/com/yipin/liuwanr/controller/AliyunOssController.java @@ -19,50 +19,52 @@ import com.yipin.liuwanr.service.AliyunOssService; @RequestMapping("/aliyun") public class AliyunOssController { - @Autowired - private AliyunOssService ossService; + @Autowired + private AliyunOssService ossService; /** * 上传文件 + * * @param file * @return */ - @PostMapping("/uploadFiles") - Response uploadFiles(MultipartFile file) { - Response resp = new Response(); - HashMap ret = ossService.uploadFiles(file); - int status = (int) ret.get("retcode"); - if (200 == status) { - resp.setStatus(status); - resp.setMessage(ret.get("retvalue")); - } else { - resp.setStatus(status); - resp.setErrmessage(ret.get("retvalue").toString()); - } - return resp; + @PostMapping("/uploadFiles") + Response uploadFiles(MultipartFile file) { + Response resp = new Response(); + HashMap ret = ossService.uploadFiles(file); + int status = (int) ret.get("retcode"); + if (200 == status) { + resp.setStatus(status); + resp.setMessage(ret.get("retvalue")); + } else { + resp.setStatus(status); + resp.setErrmessage(ret.get("retvalue").toString()); + } + return resp; - } - - /** - * 下载文件 - * @param objectName:在阿里云的文件路径 - * @param fileurl:本地路径 - * @return - */ - @GetMapping("/downloadFiles") - Response downloadFiles(HttpServletResponse response,@RequestParam String objectName) { - Response resp = new Response(); - try { - ossService.downloadFiles(response,objectName); - resp.setStatus(200); - resp.setMessage("下载成功!"); - } catch (Exception e) { - resp.setStatus(300); - resp.setErrmessage("下载失败"); - } - return null; + } + /** + * 下载文件 + * + * @param objectName:在阿里云的文件路径 + * @param fileurl:本地路径 + * @return + */ + @GetMapping("/downloadFiles") + Response downloadFiles(HttpServletResponse response, @RequestParam String objectName) { + Response resp = new Response(); + try { + ossService.downloadFiles(response, objectName); + resp.setStatus(200); + resp.setMessage("下载成功!"); + } catch (Exception e) { + resp.setStatus(300); + resp.setErrmessage("下载失败"); + } + return null; + + + } - } - } diff --git a/src/main/java/com/yipin/liuwanr/entity/City.java b/src/main/java/com/yipin/liuwanr/entity/City.java index 250a13f..2e77b08 100644 --- a/src/main/java/com/yipin/liuwanr/entity/City.java +++ b/src/main/java/com/yipin/liuwanr/entity/City.java @@ -26,4 +26,13 @@ public class City { public void setProvinceId(Integer provinceId) { this.provinceId = provinceId; } + + @Override + public String toString() { + return "City{" + + "cityId=" + cityId + + ", cityName='" + cityName + '\'' + + ", provinceId=" + provinceId + + '}'; + } } diff --git a/src/main/java/com/yipin/liuwanr/entity/SutdentClass.java b/src/main/java/com/yipin/liuwanr/entity/SutdentClass.java index 62dc59e..d36a360 100644 --- a/src/main/java/com/yipin/liuwanr/entity/SutdentClass.java +++ b/src/main/java/com/yipin/liuwanr/entity/SutdentClass.java @@ -36,5 +36,12 @@ public class SutdentClass { this.gradeId = gradeId; } - + @Override + public String toString() { + return "SutdentClass{" + + "classId=" + classId + + ", className='" + className + '\'' + + ", gradeId=" + gradeId + + '}'; + } } diff --git a/src/main/java/com/yipin/liuwanr/service/AliyunOssService.java b/src/main/java/com/yipin/liuwanr/service/AliyunOssService.java index 9035d6b..aca3225 100644 --- a/src/main/java/com/yipin/liuwanr/service/AliyunOssService.java +++ b/src/main/java/com/yipin/liuwanr/service/AliyunOssService.java @@ -19,76 +19,79 @@ import com.yipin.liuwanr.helper.OssConfigUtil; @Service public class AliyunOssService { - @Autowired - private AliyunOssConfig config; - - @Autowired - private BeanFactory beanFactory; - - private static Logger logger = Logger.getLogger(AliyunOssService.class); + @Autowired + private AliyunOssConfig config; + + @Autowired + private BeanFactory beanFactory; + + private static Logger logger = Logger.getLogger(AliyunOssService.class); + + /** + * 上传文件 + * + * @param file 上传文件 + * @return + */ + public HashMap uploadFiles(MultipartFile file) { + HashMap resp = new HashMap(); + try { + OSSClient client = beanFactory.getBean(OSSClient.class); + FilesResult result = OssConfigUtil.uploadFiles(file, client, config); + resp.put("retcode", 200); + resp.put("retvalue", result); + } catch (Exception e) { + logger.info(e.getMessage()); + resp.put("retcode", 500); + resp.put("retvalue", "Upload Fail"); + return resp; + } + return resp; + } + + + /** + * 下载文件 + * + * @param objectName 要下载的文件(路径+文件名) + * @param fileurl 本地路径 + * @return + */ + public void downloadFiles(HttpServletResponse response, String objectName) { + HashMap resp = new HashMap(); + try { + OSSClient client = beanFactory.getBean(OSSClient.class); + OssConfigUtil.downloadFiles(response, client, config, objectName); + resp.put("retcode", 200); + resp.put("retvalue", "Download SUCCESS"); + } catch (Exception e) { + logger.info(e.getMessage()); + resp.put("retcode", 500); + resp.put("retvalue", "Download Fail"); + } + } + + /** + * 根据文件路径+文件名称,删除该文件 + * + * @param filename + * @return + */ + public HashMap deleteFile(String filename) { + HashMap resp = new HashMap(); + try { + OSSClient client = beanFactory.getBean(OSSClient.class); + OssConfigUtil.deleteFile(client, config.getBucketName(), filename); + resp.put("retcode", 200); + resp.put("retvalue", "Delete SUCCESS"); + } catch (RuntimeException e) { + logger.info(e.getMessage()); + resp.put("retcode", 500); + resp.put("retvalue", "Delete Fail"); + return resp; + } + return resp; + } - /** - * 上传文件 - * @param file 上传文件 - * @return - */ - public HashMap uploadFiles(MultipartFile file) { - HashMap resp = new HashMap(); - try { - OSSClient client=beanFactory.getBean(OSSClient.class); - FilesResult result=OssConfigUtil.uploadFiles(file, client, config); - resp.put("retcode", 200); - resp.put("retvalue", result); - } catch (Exception e) { - logger.info(e.getMessage()); - resp.put("retcode", 500); - resp.put("retvalue", "Upload Fail"); - return resp; - } - return resp; - } - - /** - * 下载文件 - * @param objectName 要下载的文件(路径+文件名) - * @param fileurl 本地路径 - * @return - */ - public void downloadFiles(HttpServletResponse response,String objectName) { - HashMap resp = new HashMap(); - try { - OSSClient client=beanFactory.getBean(OSSClient.class); - OssConfigUtil.downloadFiles(response,client, config, objectName); - resp.put("retcode", 200); - resp.put("retvalue","Download SUCCESS" ); - } catch (Exception e) { - logger.info(e.getMessage()); - resp.put("retcode", 500); - resp.put("retvalue", "Download Fail"); - } - } - - /** - * 根据文件路径+文件名称,删除该文件 - * @param filename - * @return - */ - public HashMap deleteFile(String filename) { - HashMap resp = new HashMap(); - try { - OSSClient client=beanFactory.getBean(OSSClient.class); - OssConfigUtil.deleteFile(client, config.getBucketName(), filename); - resp.put("retcode", 200); - resp.put("retvalue","Delete SUCCESS" ); - } catch (RuntimeException e) { - logger.info(e.getMessage()); - resp.put("retcode", 500); - resp.put("retvalue", "Delete Fail"); - return resp; - } - return resp; - } - - } diff --git a/src/main/java/com/yipin/liuwanr/service/CityService.java b/src/main/java/com/yipin/liuwanr/service/CityService.java index f34e224..65ee31c 100644 --- a/src/main/java/com/yipin/liuwanr/service/CityService.java +++ b/src/main/java/com/yipin/liuwanr/service/CityService.java @@ -24,6 +24,7 @@ public class CityService { resp.put("retcode", 200); } catch (RuntimeException e) { logger.error(e.getMessage()); + System.out.println(e.getMessage()); resp.put("retcode", 500); resp.put("retvalue", "Inquiry Failed"); return resp; diff --git a/src/test/java/com/yipin/liuwanr/AliyunOssServiceTest.java b/src/test/java/com/yipin/liuwanr/AliyunOssServiceTest.java new file mode 100644 index 0000000..ed82dc6 --- /dev/null +++ b/src/test/java/com/yipin/liuwanr/AliyunOssServiceTest.java @@ -0,0 +1,70 @@ +package com.yipin.liuwanr; + +import com.yipin.liuwanr.service.AliyunOssService; +import org.apache.commons.fileupload.FileItem; +import org.apache.commons.fileupload.disk.DiskFileItem; +import org.apache.commons.io.IOUtils; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.multipart.commons.CommonsMultipartFile; + +import javax.servlet.http.HttpServletResponse; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.util.HashMap; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class AliyunOssServiceTest { + + @Autowired + private AliyunOssService aliyunOssService; + + /** + * 测试上传文件 + */ + @Test + public void testUploadFiles() throws IOException { + File file = new File("D:\\pic\\1.jpg"); + FileItem fileItem = new DiskFileItem("file", + Files.probeContentType(file.toPath()), + false, + file.getName(), + (int) file.length(), + file.getParentFile()); + IOUtils.copy(new FileInputStream(file), fileItem.getOutputStream()); + MultipartFile multipartFile = new CommonsMultipartFile(fileItem); + HashMap map = aliyunOssService.uploadFiles(multipartFile); + map.forEach((key, value) -> System.out.println("key = " + key + " ===> value = " + value.toString())); + } + + /** + * 测试下载文件 + */ + @Test + public void testDownloadFiles() throws IOException { + HttpServletResponse response = new MockHttpServletResponse(); + AliyunOssService aliyunOssService = this.aliyunOssService; + aliyunOssService.downloadFiles(response, + "http://liuwanr.oss-cn-shenzhen.aliyuncs.com/jpg/20200807/1596787474773.jpg"); + } + /** + * 测试根据文件路径+文件名称,删除该文件 + */ + @Test + public void testDeleteFile() throws IOException { + aliyunOssService.deleteFile("http://liuwanr.oss-cn-shenzhen.aliyuncs.com/jpg/20200807/1596787474773.jpg"); + } + +} diff --git a/src/test/java/com/yipin/liuwanr/CityServiceTest.java b/src/test/java/com/yipin/liuwanr/CityServiceTest.java index 5a4b911..b580097 100644 --- a/src/test/java/com/yipin/liuwanr/CityServiceTest.java +++ b/src/test/java/com/yipin/liuwanr/CityServiceTest.java @@ -1,14 +1,17 @@ package com.yipin.liuwanr; import com.yipin.liuwanr.entity.City; +import com.yipin.liuwanr.mapper.CityMapper; import com.yipin.liuwanr.service.CityService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.transaction.annotation.Transactional; import java.util.HashMap; +import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest @@ -17,13 +20,62 @@ public class CityServiceTest { @Autowired private CityService cityService; + @Autowired + private CityMapper cityMapper; + /** * 新增城市信息方法测试 */ - @Test + /*@Test + @Transactional public void testAddCity() { City city = new City(); + city.setCityName("hhhh"); + city.setProvinceId(2); HashMap map = cityService.addCity(city); map.forEach((key, value) -> System.out.println("key = " + key + " ===> value = " + value)); + }*/ + + /** + * 查询城市信息方法测试 + */ + @Test + public void testQueryCity() { + HashMap map = cityService.queryCity(1); + Integer retcode = (Integer) map.get("retcode"); + System.out.println(retcode); + if (retcode == 200) { + List cities = (List) map.get("retvalue"); + cities.forEach(item -> { + System.out.println(item.toString()); + }); + } else { + String msg = (String) map.get("retvalue"); + System.out.println(msg); + } + } + + /** + * 删除城市信息方法测试 + */ + @Test + @Transactional + public void testDeleteCity() { + HashMap map = cityService.deleteCity(1); + map.forEach((key, value) -> System.out.println("key = " + key + " ===> value = " + value.toString())); + } + + /** + * 删除城市信息方法测试 + */ + @Test + @Transactional + public void testUpdateCity() { + City city = new City(); + city.setCityId(1); + city.setCityName("hhhh"); + city.setProvinceId(2); + HashMap map = cityService.updateCity(city); + map.forEach((key, value) -> System.out.println("key = " + key + " ===> value = " + value.toString())); } } diff --git a/src/test/java/com/yipin/liuwanr/ClassServiceTest.java b/src/test/java/com/yipin/liuwanr/ClassServiceTest.java new file mode 100644 index 0000000..7850a20 --- /dev/null +++ b/src/test/java/com/yipin/liuwanr/ClassServiceTest.java @@ -0,0 +1,40 @@ +package com.yipin.liuwanr; + +import com.yipin.liuwanr.entity.City; +import com.yipin.liuwanr.entity.SutdentClass; +import com.yipin.liuwanr.mapper.CityMapper; +import com.yipin.liuwanr.service.CityService; +import com.yipin.liuwanr.service.ClassService; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.transaction.annotation.Transactional; + +import java.util.HashMap; +import java.util.List; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class ClassServiceTest { + + @Autowired + private ClassService classService; + + @Test + public void testQueryGetByClassName() { + HashMap map = classService.queryGetByClassName(""); + Integer retcode = (Integer) map.get("retcode"); + System.out.println(retcode); + if (retcode == 200) { + List classes = (List) map.get("retvalue"); + classes.forEach(item -> { + System.out.println(item.toString()); + }); + } else { + String msg = (String) map.get("retvalue"); + System.out.println(msg); + } + } +}