wangwei 7 vuotta sitten
vanhempi
commit
77f1ca6f97

+ 45 - 5
examcloud-exchange-outer-api-provider/src/main/java/cn/com/qmth/examcloud/exchange/outer/api/provider/FaceppController.java

@@ -2,6 +2,7 @@ package cn.com.qmth.examcloud.exchange.outer.api.provider;
 
 import java.io.File;
 import java.util.List;
+import java.util.Map;
 
 import javax.servlet.http.HttpServletRequest;
 
@@ -14,8 +15,10 @@ import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.multipart.commons.CommonsMultipartFile;
 
 import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
 
 import cn.com.qmth.examcloud.commons.base.exception.StatusException;
+import cn.com.qmth.examcloud.commons.base.util.ZipUtil;
 import cn.com.qmth.examcloud.commons.web.security.bean.User;
 import cn.com.qmth.examcloud.commons.web.support.ControllerSupport;
 import cn.com.qmth.examcloud.exchange.outer.service.FaceppService;
@@ -90,7 +93,7 @@ public class FaceppController extends ControllerSupport {
 	 * @throws Exception
 	 */
 	@PostMapping("/import")
-	public List<String> batchAll(@RequestParam CommonsMultipartFile file,
+	public List<Map<String, String>> batchAll(@RequestParam CommonsMultipartFile file,
 			HttpServletRequest request) throws Exception {
 		User accessUser = getAccessUser();
 
@@ -98,8 +101,6 @@ public class FaceppController extends ControllerSupport {
 		File storeLocation = item.getStoreLocation();
 		String filename = file.getOriginalFilename();
 
-		String userToken = accessUser.getToken();
-
 		if (!filename.endsWith(".zip")) {
 			throw new StatusException("EX-620001", "文件格式不正确,必须是zip格式的压缩包");
 		}
@@ -108,9 +109,48 @@ public class FaceppController extends ControllerSupport {
 			throw new StatusException("EX-620002", "文件大小超过50M,请分批导入");
 		}
 
-		List<String> errDatas = Lists.newArrayList();
+		List<Map<String, String>> ret = Lists.newArrayList();
+
+		String tempDirPath = storeLocation.getCanonicalPath() + File.separator
+				+ System.currentTimeMillis();
+		File tempDir = new File(tempDirPath);
+		try {
+			ZipUtil.unzip(storeLocation, new File(tempDirPath));
+		} catch (Exception e) {
+			throw new StatusException("EX-620003", "zip文件损坏");
+		}
+
+		File[] files = tempDir.listFiles();
+
+		if (null == files) {
+			throw new StatusException("EX-620004", "zip文件为空");
+		}
+
+		for (File f : files) {
+			Map<String, String> map = Maps.newHashMap();
+
+			if (f.length() > MAX_SIZE * 1024) {
+				map.put("statusCode", "EX-620005");
+				map.put("statusDesc", "文件大小超过500KB");
+				ret.add(map);
+				continue;
+			}
+			try {
+				map.put("file", f.getName());
+				faceppService.uploadPhoto(accessUser, f.getName(), f);
+				map.put("statusCode", "200");
+				map.put("statusDesc", "成功");
+			} catch (StatusException e) {
+				map.put("statusCode", e.getCode());
+				map.put("statusDesc", e.getDesc());
+			} catch (Exception e) {
+				map.put("statusCode", "系统异常");
+				map.put("statusDesc", e.getCause().getMessage());
+			}
+			ret.add(map);
+		}
 
-		return errDatas;
+		return ret;
 	}
 
 }