|
@@ -1,153 +0,0 @@
|
|
-package cn.com.qmth.importpaper;
|
|
|
|
-
|
|
|
|
-import java.io.File;
|
|
|
|
-import java.util.ArrayList;
|
|
|
|
-import java.util.Date;
|
|
|
|
-import java.util.HashSet;
|
|
|
|
-import java.util.List;
|
|
|
|
-import java.util.Map;
|
|
|
|
-import java.util.Set;
|
|
|
|
-
|
|
|
|
-import org.apache.commons.io.IOUtils;
|
|
|
|
-import org.apache.logging.log4j.LogManager;
|
|
|
|
-import org.apache.logging.log4j.Logger;
|
|
|
|
-import org.apache.poi.xssf.usermodel.XSSFRow;
|
|
|
|
-import org.apache.poi.xssf.usermodel.XSSFSheet;
|
|
|
|
-import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
-
|
|
|
|
-import com.google.common.collect.Lists;
|
|
|
|
-import com.google.common.collect.Maps;
|
|
|
|
-
|
|
|
|
-import okhttp3.Response;
|
|
|
|
-
|
|
|
|
-public class ImportPaperByAllQues {
|
|
|
|
-
|
|
|
|
- private static Logger logger = LogManager.getLogger(ImportPaperByAllQues.class);
|
|
|
|
-
|
|
|
|
- private static String paperSuff=null;
|
|
|
|
-
|
|
|
|
- private static String sourceDir = "d:\\2020ddimport\\ques\\excelDir";
|
|
|
|
-
|
|
|
|
- private static String sourceQuesDir = sourceDir + "\\quesDir\\";
|
|
|
|
-
|
|
|
|
- private final static String url = "http://ecs-test.qmth.com.cn:8008/api/ecs_ques/importDdCollegePaper";
|
|
|
|
-
|
|
|
|
- private final static String rootOrgId = "0";
|
|
|
|
-
|
|
|
|
- private final static String courseCode="CCC";
|
|
|
|
-
|
|
|
|
- private final static String key = "U_C_0_8";
|
|
|
|
-
|
|
|
|
- private final static String token = "5c595b0d450a4ab0b81e9f6aa0ec0d44";
|
|
|
|
-
|
|
|
|
- public static void main(String[] args) {
|
|
|
|
- logger.debug("**********************导入开始");
|
|
|
|
- Date start = new Date();
|
|
|
|
- int errnum = 0;
|
|
|
|
- StringBuilder sb = new StringBuilder();
|
|
|
|
- int num = 0;
|
|
|
|
- String fname = null;
|
|
|
|
- File dir = new File(sourceDir);
|
|
|
|
- for (File f : dir.listFiles()) {
|
|
|
|
- fname = f.getName();
|
|
|
|
- if (!f.isDirectory() && fname.endsWith(".xlsx")) {
|
|
|
|
- num++;
|
|
|
|
- Date n = new Date();
|
|
|
|
- try {
|
|
|
|
- importCoursePaper(f);
|
|
|
|
- Date et = new Date();
|
|
|
|
- logger.debug("导入成功(" + num + "):" + fname + " 耗时:" + (et.getTime() - n.getTime()));
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- errnum++;
|
|
|
|
- sb.append(fname).append(",");
|
|
|
|
- Date et = new Date();
|
|
|
|
- logger.error(
|
|
|
|
- "导入失败(" + num + "):" + fname + " 耗时:" + (et.getTime() - n.getTime()) + " " + e.getMessage(),
|
|
|
|
- e);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-// try {
|
|
|
|
-// importCoursePaper(new File("d:\\2020ddimport\\excelDir\\AAAAA.11.15.xlsx"));
|
|
|
|
-// } catch (Exception e) {
|
|
|
|
-// e.printStackTrace();
|
|
|
|
-// }
|
|
|
|
-
|
|
|
|
- Date end = new Date();
|
|
|
|
- logger.debug("**********************导入结束 耗时:" + (end.getTime() - start.getTime()));
|
|
|
|
-
|
|
|
|
- logger.debug("**********************导入出错数:" + errnum);
|
|
|
|
- logger.debug("**********************导入出错课程:" + sb.toString());
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private static void importCoursePaper(File file) throws Exception {
|
|
|
|
- File zfile = new File(sourceDir + "\\" + (file.getName().substring(0, file.getName().lastIndexOf(".")))+ ".zip");
|
|
|
|
- try {
|
|
|
|
- if (zfile.exists()) {
|
|
|
|
- zfile.delete();
|
|
|
|
- }
|
|
|
|
- createZip(file, zfile);
|
|
|
|
- submitZip(zfile);
|
|
|
|
- } finally {
|
|
|
|
- if (zfile.exists()) {
|
|
|
|
- zfile.delete();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private static void createZip(File file, File zfile) throws Exception {
|
|
|
|
-
|
|
|
|
- XSSFWorkbook wb = null;
|
|
|
|
- try {
|
|
|
|
- wb = new XSSFWorkbook(file);
|
|
|
|
- int num = wb.getNumberOfSheets();
|
|
|
|
- if (num == 1) {
|
|
|
|
- throw new NoPaperException("1000","没有试卷信息:" + file.getName());
|
|
|
|
- }
|
|
|
|
- Set<String> ques = new HashSet<String>();
|
|
|
|
- for (int i = 1; i < num; i++) {
|
|
|
|
- XSSFSheet sheet = wb.getSheetAt(i);
|
|
|
|
- int rows = sheet.getLastRowNum();
|
|
|
|
- for (int j = 1; j <= rows; j++) {
|
|
|
|
- XSSFRow row = sheet.getRow(j);
|
|
|
|
- ques.add(row.getCell(0).getStringCellValue());
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- List<File> files = new ArrayList<File>();
|
|
|
|
- for (String s : ques) {
|
|
|
|
- File f = new File(sourceQuesDir + s + ".txt");
|
|
|
|
- files.add(f);
|
|
|
|
- }
|
|
|
|
- files.add(file);
|
|
|
|
- FileUtil.doZip(files, zfile);
|
|
|
|
- } finally {
|
|
|
|
- if (wb != null) {
|
|
|
|
- wb.close();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private static void submitZip(File zfile) throws Exception {
|
|
|
|
- Map<String, String> params = Maps.newHashMap();
|
|
|
|
- params.put("rootOrgId", rootOrgId);
|
|
|
|
- params.put("courseCode", courseCode);
|
|
|
|
- if(paperSuff!=null) {
|
|
|
|
- params.put("suff", paperSuff);
|
|
|
|
- }
|
|
|
|
- Map<String, String> headers = Maps.newHashMap();
|
|
|
|
- headers.put("key", key);
|
|
|
|
- headers.put("token", token);
|
|
|
|
- List<FormFilePart> fileList = Lists.newArrayList();
|
|
|
|
- fileList.add(new FormFilePart("dataFile", zfile.getName(), zfile));
|
|
|
|
- Response resp = null;
|
|
|
|
- try {
|
|
|
|
- resp = OKHttpUtil.call(HttpMethod.POST, url, headers, params, fileList);
|
|
|
|
- if (resp.code() != 200) {
|
|
|
|
- throw new Exception(zfile.getName() + ":body:" + resp.body().string());
|
|
|
|
- }
|
|
|
|
- } finally {
|
|
|
|
- IOUtils.closeQuietly(resp);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-}
|
|
|