|
@@ -1,58 +1,65 @@
|
|
package cn.com.qmth.examcloud.core.examwork.base.util;
|
|
package cn.com.qmth.examcloud.core.examwork.base.util;
|
|
|
|
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.FileOutputStream;
|
|
|
|
+import java.io.OutputStream;
|
|
|
|
+
|
|
import org.apache.commons.io.IOUtils;
|
|
import org.apache.commons.io.IOUtils;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
-import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
|
|
|
|
|
|
|
-import java.io.File;
|
|
|
|
-import java.io.FileOutputStream;
|
|
|
|
-import java.io.OutputStream;
|
|
|
|
|
|
+import cn.com.qmth.examcloud.commons.base.exception.ExamCloudRuntimeException;
|
|
|
|
|
|
/**
|
|
/**
|
|
* Created by songyue on 17/7/14.
|
|
* Created by songyue on 17/7/14.
|
|
*/
|
|
*/
|
|
public final class ImportUtils {
|
|
public final class ImportUtils {
|
|
|
|
|
|
- protected static final Logger logger = LoggerFactory.getLogger(ImportUtils.class);
|
|
|
|
-
|
|
|
|
- public static final String TEMP_FILE_IMP = "fileImport/";
|
|
|
|
-
|
|
|
|
- public static final String TEMP_FILE_EXP = "fileExport/";
|
|
|
|
-
|
|
|
|
- static {
|
|
|
|
- init();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 初始化文件夹
|
|
|
|
- */
|
|
|
|
- public static void init() {
|
|
|
|
- File temp_file_exp = new File(TEMP_FILE_EXP);
|
|
|
|
- File temp_file_imp = new File(TEMP_FILE_IMP);
|
|
|
|
- // 如果输出目标文件夹不存在,则创建
|
|
|
|
- if (!temp_file_imp.exists()) {
|
|
|
|
- temp_file_imp.mkdirs();
|
|
|
|
- }
|
|
|
|
- if (!temp_file_exp.exists()) {
|
|
|
|
- temp_file_exp.mkdirs();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 获取上传文件
|
|
|
|
- *
|
|
|
|
- * @param file
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public static File getUploadFile(MultipartFile file) throws Exception {
|
|
|
|
- String fileName = file.getOriginalFilename();
|
|
|
|
- File tempFile = new File(TEMP_FILE_IMP + fileName);
|
|
|
|
- OutputStream os = new FileOutputStream(tempFile);
|
|
|
|
- IOUtils.copyLarge(file.getInputStream(), os);
|
|
|
|
- IOUtils.closeQuietly(os);
|
|
|
|
- return tempFile;
|
|
|
|
- }
|
|
|
|
|
|
+ protected static final Logger logger = LoggerFactory.getLogger(ImportUtils.class);
|
|
|
|
+
|
|
|
|
+ public static final String TEMP_FILE_IMP = "fileImport/";
|
|
|
|
+
|
|
|
|
+ public static final String TEMP_FILE_EXP = "fileExport/";
|
|
|
|
+
|
|
|
|
+ static {
|
|
|
|
+ init();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 初始化文件夹
|
|
|
|
+ */
|
|
|
|
+ public static void init() {
|
|
|
|
+ File temp_file_exp = new File(TEMP_FILE_EXP);
|
|
|
|
+ File temp_file_imp = new File(TEMP_FILE_IMP);
|
|
|
|
+ // 如果输出目标文件夹不存在,则创建
|
|
|
|
+ if (!temp_file_imp.exists()) {
|
|
|
|
+ temp_file_imp.mkdirs();
|
|
|
|
+ }
|
|
|
|
+ if (!temp_file_exp.exists()) {
|
|
|
|
+ temp_file_exp.mkdirs();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取上传文件
|
|
|
|
+ *
|
|
|
|
+ * @param file
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static File getUploadFile(MultipartFile file) {
|
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
|
+ File tempFile = new File(TEMP_FILE_IMP + fileName);
|
|
|
|
+ OutputStream os = null;
|
|
|
|
+ try {
|
|
|
|
+ os = new FileOutputStream(tempFile);
|
|
|
|
+ IOUtils.copyLarge(file.getInputStream(), os);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new ExamCloudRuntimeException(e);
|
|
|
|
+ } finally {
|
|
|
|
+ IOUtils.closeQuietly(os);
|
|
|
|
+ }
|
|
|
|
+ return tempFile;
|
|
|
|
+ }
|
|
|
|
|
|
}
|
|
}
|