上传图片代码 测试用例
							parent
							
								
									cbebfd7150
								
							
						
					
					
						commit
						eba6660198
					
				|  | @ -2,6 +2,7 @@ package com.zcloud.controller.accident; | ||||||
| 
 | 
 | ||||||
| import cn.hutool.core.bean.BeanUtil; | import cn.hutool.core.bean.BeanUtil; | ||||||
| import cn.hutool.core.convert.Convert; | import cn.hutool.core.convert.Convert; | ||||||
|  | import cn.hutool.core.util.StrUtil; | ||||||
| import com.zcloud.controller.base.BaseController; | import com.zcloud.controller.base.BaseController; | ||||||
| import com.zcloud.entity.Page; | import com.zcloud.entity.Page; | ||||||
| import com.zcloud.entity.PageData; | import com.zcloud.entity.PageData; | ||||||
|  | @ -90,4 +91,23 @@ public class AccidentRecordsController extends BaseController { | ||||||
|         } |         } | ||||||
|         accidentRecordsService.importExcel(file); |         accidentRecordsService.importExcel(file); | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     @RequestMapping("/import/photos") | ||||||
|  |     public Map<String, Object> importPhotos(@RequestParam("file") MultipartFile file) { | ||||||
|  |         if (file == null || file.isEmpty()) { | ||||||
|  |             throw new RuntimeException("文件不能为空"); | ||||||
|  |         } | ||||||
|  |         Map<String, Object> result = new HashMap<>(); | ||||||
|  |         result.put("result", "success"); // 路径
 | ||||||
|  |         result.put("path", accidentRecordsService.importPhotos(file)); | ||||||
|  |         return result; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @RequestMapping("/view/photos/{path}") | ||||||
|  |     public void viewPhotos(@PathVariable("path") String path, HttpServletResponse response) { | ||||||
|  |         if (StrUtil.isEmpty(path)) { | ||||||
|  |             throw new RuntimeException("路径不能为空"); | ||||||
|  |         } | ||||||
|  |         accidentRecordsService.viewPhotos(path, response); | ||||||
|  |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -10,17 +10,71 @@ import java.util.List; | ||||||
| 
 | 
 | ||||||
| public interface AccidentRecordsService { | public interface AccidentRecordsService { | ||||||
| 
 | 
 | ||||||
|  |     /** | ||||||
|  |      * 分页查询 | ||||||
|  |      * | ||||||
|  |      * @param page 条件 | ||||||
|  |      * @return 查询结果 | ||||||
|  |      */ | ||||||
|     List<PageData> queryPage(Page page); |     List<PageData> queryPage(Page page); | ||||||
| 
 | 
 | ||||||
|  |     /** | ||||||
|  |      * 根据主键查询 | ||||||
|  |      * | ||||||
|  |      * @param id 主键 | ||||||
|  |      * @return 查询结果 | ||||||
|  |      */ | ||||||
|     PageData getById(Long id); |     PageData getById(Long id); | ||||||
| 
 | 
 | ||||||
|  |     /** | ||||||
|  |      * 新增 | ||||||
|  |      * | ||||||
|  |      * @param accidentRecords 要保存的实体 | ||||||
|  |      */ | ||||||
|     void save(AccidentRecords accidentRecords); |     void save(AccidentRecords accidentRecords); | ||||||
| 
 | 
 | ||||||
|  |     /** | ||||||
|  |      * 修改 | ||||||
|  |      * | ||||||
|  |      * @param bean 要修改的实体 | ||||||
|  |      */ | ||||||
|     void update(AccidentRecords bean); |     void update(AccidentRecords bean); | ||||||
| 
 | 
 | ||||||
|  |     /** | ||||||
|  |      * 删除 | ||||||
|  |      * | ||||||
|  |      * @param ids 主键数组 | ||||||
|  |      */ | ||||||
|     void delete(Long[] ids); |     void delete(Long[] ids); | ||||||
| 
 | 
 | ||||||
|  |     /** | ||||||
|  |      * 导出Excel | ||||||
|  |      * | ||||||
|  |      * @param pd       查询条件 | ||||||
|  |      * @param response 响应 | ||||||
|  |      */ | ||||||
|     void exportExcel(PageData pd, HttpServletResponse response); |     void exportExcel(PageData pd, HttpServletResponse response); | ||||||
| 
 | 
 | ||||||
|  |     /** | ||||||
|  |      * 导入Excel | ||||||
|  |      * | ||||||
|  |      * @param file 文件 | ||||||
|  |      */ | ||||||
|     void importExcel(MultipartFile file); |     void importExcel(MultipartFile file); | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * 导入图片 | ||||||
|  |      * | ||||||
|  |      * @param file 文件 | ||||||
|  |      * @return 导入结果 | ||||||
|  |      */ | ||||||
|  |     String importPhotos(MultipartFile file); | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * 查看图片 | ||||||
|  |      * | ||||||
|  |      * @param path     路径 | ||||||
|  |      * @param response 响应 | ||||||
|  |      */ | ||||||
|  |     void viewPhotos(String path, HttpServletResponse response); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1,10 +1,9 @@ | ||||||
| package com.zcloud.service.accident.impl; | package com.zcloud.service.accident.impl; | ||||||
| 
 | 
 | ||||||
|  | import cn.hutool.core.io.FileUtil; | ||||||
| import cn.hutool.core.lang.Assert; | import cn.hutool.core.lang.Assert; | ||||||
| import cn.hutool.core.util.CharsetUtil; | import cn.hutool.core.util.*; | ||||||
| import cn.hutool.core.util.IdUtil; | import cn.hutool.extra.servlet.ServletUtil; | ||||||
| import cn.hutool.core.util.ReflectUtil; |  | ||||||
| import cn.hutool.core.util.URLUtil; |  | ||||||
| import cn.hutool.poi.excel.ExcelReader; | import cn.hutool.poi.excel.ExcelReader; | ||||||
| import cn.hutool.poi.excel.ExcelUtil; | import cn.hutool.poi.excel.ExcelUtil; | ||||||
| import cn.hutool.poi.excel.ExcelWriter; | import cn.hutool.poi.excel.ExcelWriter; | ||||||
|  | @ -19,6 +18,8 @@ import com.zcloud.service.system.DictionariesService; | ||||||
| import com.zcloud.util.Jurisdiction; | import com.zcloud.util.Jurisdiction; | ||||||
| import lombok.RequiredArgsConstructor; | import lombok.RequiredArgsConstructor; | ||||||
| import lombok.extern.slf4j.Slf4j; | import lombok.extern.slf4j.Slf4j; | ||||||
|  | import org.apache.commons.io.FileUtils; | ||||||
|  | import org.apache.commons.io.FilenameUtils; | ||||||
| import org.apache.ibatis.session.ExecutorType; | import org.apache.ibatis.session.ExecutorType; | ||||||
| import org.apache.ibatis.session.SqlSession; | import org.apache.ibatis.session.SqlSession; | ||||||
| import org.apache.ibatis.session.SqlSessionFactory; | import org.apache.ibatis.session.SqlSessionFactory; | ||||||
|  | @ -28,6 +29,7 @@ import org.springframework.web.multipart.MultipartFile; | ||||||
| 
 | 
 | ||||||
| import javax.servlet.http.HttpServletResponse; | import javax.servlet.http.HttpServletResponse; | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
|  | import java.io.InputStream; | ||||||
| import java.lang.reflect.Field; | import java.lang.reflect.Field; | ||||||
| import java.util.Arrays; | import java.util.Arrays; | ||||||
| import java.util.Date; | import java.util.Date; | ||||||
|  | @ -137,4 +139,25 @@ public class AccidentRecordsServiceImpl implements AccidentRecordsService { | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public String importPhotos(MultipartFile file) { | ||||||
|  |         String fileName = StrUtil.format("{}.{}", IdUtil.getSnowflake(1, 1).nextId(), | ||||||
|  |                 FilenameUtils.getExtension(file.getOriginalFilename())); | ||||||
|  |         try (InputStream inputStream = file.getInputStream()) { | ||||||
|  |             FileUtils.copyInputStreamToFile(inputStream, FileUtil.touch("./image/" + fileName)); | ||||||
|  |             return "./image/" + fileName; | ||||||
|  |         } catch (IOException e) { | ||||||
|  |             log.error("导入图片失败", e); | ||||||
|  |             throw new RuntimeException("导入图片失败"); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void viewPhotos(String path, HttpServletResponse response) { | ||||||
|  |         if (!FileUtil.exist(path)) { | ||||||
|  |             throw new RuntimeException("图片不存在"); | ||||||
|  |         } | ||||||
|  |         ServletUtil.write(response, FileUtil.touch(path)); | ||||||
|  |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -0,0 +1,136 @@ | ||||||
|  | package com.zcloud.service.accident.service; | ||||||
|  | 
 | ||||||
|  | import cn.hutool.core.util.IdUtil; | ||||||
|  | import com.zcloud.entity.Page; | ||||||
|  | import com.zcloud.entity.PageData; | ||||||
|  | import com.zcloud.entity.accident.AccidentRecords; | ||||||
|  | import com.zcloud.entity.accident.dto.AccidentRecordsExcel; | ||||||
|  | import com.zcloud.mapper.datasource.accident.AccidentRecordsMapper; | ||||||
|  | import com.zcloud.service.accident.impl.AccidentRecordsServiceImpl; | ||||||
|  | import com.zcloud.util.Jurisdiction; | ||||||
|  | import org.apache.commons.io.FilenameUtils; | ||||||
|  | import org.junit.Before; | ||||||
|  | import org.junit.Test; | ||||||
|  | import org.junit.runner.RunWith; | ||||||
|  | import org.mockito.InjectMocks; | ||||||
|  | import org.mockito.MockitoAnnotations; | ||||||
|  | import org.springframework.beans.factory.annotation.Autowired; | ||||||
|  | import org.springframework.boot.test.context.SpringBootTest; | ||||||
|  | import org.springframework.boot.test.mock.mockito.MockBean; | ||||||
|  | import org.springframework.test.context.junit4.SpringRunner; | ||||||
|  | 
 | ||||||
|  | import javax.servlet.http.HttpServletResponse; | ||||||
|  | import java.util.ArrayList; | ||||||
|  | import java.util.Date; | ||||||
|  | import java.util.List; | ||||||
|  | 
 | ||||||
|  | import static org.junit.Assert.assertEquals; | ||||||
|  | import static org.mockito.Mockito.*; | ||||||
|  | 
 | ||||||
|  | @SpringBootTest | ||||||
|  | @RunWith(SpringRunner.class) | ||||||
|  | public class AccidentRecordsServiceTest { | ||||||
|  | 
 | ||||||
|  |     @Autowired | ||||||
|  |     @InjectMocks | ||||||
|  |     private AccidentRecordsServiceImpl accidentRecordsService; | ||||||
|  | 
 | ||||||
|  |     @MockBean | ||||||
|  |     private AccidentRecordsMapper accidentRecordsMapper; | ||||||
|  | 
 | ||||||
|  | //    @MockBean
 | ||||||
|  | //    private DictionariesService dictionariesService;
 | ||||||
|  | //
 | ||||||
|  | //    @MockBean
 | ||||||
|  | //    private SqlSessionFactory sqlSessionFactory;
 | ||||||
|  | 
 | ||||||
|  |     @Before | ||||||
|  |     public void setUp() { | ||||||
|  |         MockitoAnnotations.initMocks(this); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void testQueryPage() { | ||||||
|  |         Page page = new Page(); | ||||||
|  |         List<PageData> expectedList = new ArrayList<>(); | ||||||
|  |         when(accidentRecordsMapper.listPage(page)).thenReturn(expectedList); | ||||||
|  | 
 | ||||||
|  |         List<PageData> resultList = accidentRecordsService.queryPage(page); | ||||||
|  | 
 | ||||||
|  |         assertEquals(expectedList, resultList); | ||||||
|  |         verify(accidentRecordsMapper, times(1)).listPage(page); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void testGetById() { | ||||||
|  |         Long id = 1L; | ||||||
|  |         PageData expectedPageData = new PageData(); | ||||||
|  |         when(accidentRecordsMapper.getById(id)).thenReturn(expectedPageData); | ||||||
|  | 
 | ||||||
|  |         PageData resultPageData = accidentRecordsService.getById(id); | ||||||
|  | 
 | ||||||
|  |         assertEquals(expectedPageData, resultPageData); | ||||||
|  |         verify(accidentRecordsMapper, times(1)).getById(id); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void testSave() { | ||||||
|  |         AccidentRecords accidentRecords = new AccidentRecords(); | ||||||
|  |         accidentRecords.setId(IdUtil.getSnowflake(1, 1).nextId()); | ||||||
|  |         accidentRecords.setCreatedBy("测试"); | ||||||
|  |         accidentRecords.setCreatedTime(new Date()); | ||||||
|  |         accidentRecords.setIsDeleted(0); | ||||||
|  | 
 | ||||||
|  |         accidentRecordsService.save(accidentRecords); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void testUpdate() { | ||||||
|  |         AccidentRecords accidentRecords = new AccidentRecords(); | ||||||
|  |         accidentRecords.setUpdatedBy(Jurisdiction.getUsername()); | ||||||
|  |         accidentRecords.setUpdatedTime(new Date()); | ||||||
|  | 
 | ||||||
|  |         accidentRecordsService.update(accidentRecords); | ||||||
|  | 
 | ||||||
|  |         verify(accidentRecordsMapper, times(1)).updateById(accidentRecords); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void testDelete() { | ||||||
|  |         Long[] ids = {1L, 2L}; | ||||||
|  | 
 | ||||||
|  |         accidentRecordsService.delete(ids); | ||||||
|  | 
 | ||||||
|  |         verify(accidentRecordsMapper, times(1)).delete(ids); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void testExportExcel() throws Exception { | ||||||
|  |         PageData pd = new PageData(); | ||||||
|  |         List<AccidentRecordsExcel> expectedList = new ArrayList<>(); | ||||||
|  |         when(accidentRecordsMapper.listExcel(pd)).thenReturn(expectedList); | ||||||
|  | 
 | ||||||
|  |         HttpServletResponse response = mock(HttpServletResponse.class); | ||||||
|  |         accidentRecordsService.exportExcel(pd, response); | ||||||
|  | 
 | ||||||
|  |         verify(response, times(1)).setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); | ||||||
|  |         verify(response, times(1)).setCharacterEncoding("utf-8"); | ||||||
|  |         verify(response, times(1)).setHeader("Content-Disposition", "attachment;filename*=utf-8''事故调查表.xlsx"); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | //    @Test
 | ||||||
|  | //    public void testImportExcel() throws Exception {
 | ||||||
|  | //        InputStream inputStream = new ByteArrayInputStream(ExcelUtil.write(new ArrayList<>(), true).getBytes());
 | ||||||
|  | //        MultipartFile file = mock(MultipartFile.class);
 | ||||||
|  | //        when(file.getInputStream()).thenReturn(inputStream);
 | ||||||
|  | //
 | ||||||
|  | //        accidentRecordsService.importExcel(file);
 | ||||||
|  | //
 | ||||||
|  | //        verify(sqlSessionFactory, times(1)).openSession(ExecutorType.BATCH, false);
 | ||||||
|  | //    }
 | ||||||
|  | 
 | ||||||
|  |     public static void main(String[] args) { | ||||||
|  |         String extension = FilenameUtils.getExtension("akjsfhahjf.apk"); | ||||||
|  |         System.out.println(extension); | ||||||
|  |     } | ||||||
|  | } | ||||||
		Loading…
	
		Reference in New Issue