|
|
@@ -0,0 +1,93 @@
|
|
|
+package com.uas.mes.mobile.controller.common;
|
|
|
+
|
|
|
+import com.uas.mes.common.entity.Employee;
|
|
|
+import com.uas.mes.common.service.FilePathService;
|
|
|
+import com.uas.mes.common.util.BaseUtil;
|
|
|
+import com.uas.mes.core.data.SpObserver;
|
|
|
+import com.uas.mes.core.util.PathUtil;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Controller;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+import org.springframework.web.multipart.MultipartHttpServletRequest;
|
|
|
+import org.springframework.web.multipart.commons.CommonsMultipartResolver;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.io.*;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+@Controller("addrBookController")
|
|
|
+public class AddrBookController {
|
|
|
+ @Autowired
|
|
|
+ private FilePathService filePathService;
|
|
|
+ /**
|
|
|
+ * 多附件上传
|
|
|
+ *
|
|
|
+ * @throws IOException
|
|
|
+ * @throws IllegalStateException
|
|
|
+ */
|
|
|
+ @RequestMapping("/mobile/uploadAttachs.action")
|
|
|
+ @ResponseBody
|
|
|
+ public Map<String, Object> uploadAttachs(HttpServletRequest request,
|
|
|
+ String master) throws IllegalStateException, IOException {
|
|
|
+ Employee employee = (Employee) request.getSession().getAttribute(
|
|
|
+ "employee");
|
|
|
+ if (employee == null)
|
|
|
+ BaseUtil.showError("会话已断开!");
|
|
|
+ SpObserver.putSp(master);
|
|
|
+ Map<String, Object> modelMap = new HashMap<String, Object>();
|
|
|
+ List<Integer> idArr = new ArrayList<Integer>();
|
|
|
+ CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
|
|
|
+ request.getSession().getServletContext());
|
|
|
+ // 判断 request 是否有文件上传,即多部分请求
|
|
|
+ if (multipartResolver.isMultipart(request)) {
|
|
|
+ // 转换成多部分request
|
|
|
+ MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
|
|
|
+ // 取得request中的所有文件名
|
|
|
+ Iterator<String> iter = multiRequest.getFileNames();
|
|
|
+ while (iter.hasNext()) {
|
|
|
+ // 取得上传文件
|
|
|
+ MultipartFile file = multiRequest.getFile(iter.next());
|
|
|
+ if (file != null) {
|
|
|
+ // 取得当前上传文件的文件名称
|
|
|
+ String myFileName = file.getOriginalFilename();
|
|
|
+ long size = file.getSize();
|
|
|
+ // 如果名称不为"",说明该文件存在,否则说明该文件不存在
|
|
|
+ if (myFileName.trim() != "") {
|
|
|
+ // 定义上传路径
|
|
|
+ String path = getFilePath(myFileName,
|
|
|
+ employee.getEm_code());
|
|
|
+ File localFile = new File(path);
|
|
|
+ file.transferTo(localFile);
|
|
|
+ int id = filePathService.saveFilePath(path, (int) size,
|
|
|
+ myFileName, employee);
|
|
|
+ idArr.add(id);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ modelMap.put("id", idArr.toString());
|
|
|
+ modelMap.put("success", true);
|
|
|
+ return modelMap;
|
|
|
+ }
|
|
|
+ private String getFilePath(String fileName, String em_code) {
|
|
|
+ String uuid = UUID.randomUUID().toString().replaceAll("\\-", "");
|
|
|
+ String suffix = fileName.indexOf(".") != -1 ? fileName.substring(
|
|
|
+ fileName.lastIndexOf("."), fileName.length()) : "";
|
|
|
+ String path = PathUtil.getFilePath() + "postattach";
|
|
|
+ File file = new File(path);
|
|
|
+ if (!file.isDirectory()) {
|
|
|
+ file.mkdir();
|
|
|
+ path = path + File.separator + em_code;
|
|
|
+ new File(path).mkdir();
|
|
|
+ } else {
|
|
|
+ path = path + File.separator + em_code;
|
|
|
+ file = new File(path);
|
|
|
+ if (!file.isDirectory()) {
|
|
|
+ file.mkdir();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return path + File.separator + uuid + suffix;
|
|
|
+ }
|
|
|
+}
|