|
|
@@ -0,0 +1,118 @@
|
|
|
+package com.uas.kanban.controller;
|
|
|
+
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+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.RequestMethod;
|
|
|
+import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import com.uas.kanban.base.BaseController;
|
|
|
+import com.uas.kanban.dao.LogoDao;
|
|
|
+import com.uas.kanban.model.Logo;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import javax.servlet.ServletOutputStream;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 文件上传
|
|
|
+ *
|
|
|
+ * @author sunyj
|
|
|
+ * @since 2017年9月1日 下午4:42:45
|
|
|
+ */
|
|
|
+@Controller
|
|
|
+@RequestMapping("/logo")
|
|
|
+public class LogoController extends BaseController<Logo> {
|
|
|
+
|
|
|
+ private static final String UPLOAD_DIR = System.getProperty("java.io.tmpdir");
|
|
|
+ private static final String NO_LOGO_PATH = System.getProperty("user.dir") + "/src/main/webapp/resources/images/nologo.png";
|
|
|
+
|
|
|
+ private Logger logger = LoggerFactory.getLogger(LogoController.class);
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private LogoDao logoDao;
|
|
|
+
|
|
|
+ @RequestMapping(value = "/upload")
|
|
|
+ @ResponseBody
|
|
|
+ public Map<String, Object> upload(MultipartFile file) {
|
|
|
+ Map<String, Object> message = new HashMap<String, Object>();
|
|
|
+
|
|
|
+ if (file == null || file.isEmpty()) {
|
|
|
+ message.put("message", "文件为空,无法进行上传!");
|
|
|
+ message.put("success", false);
|
|
|
+ logger.error((String) message.get("message"));
|
|
|
+ return message;
|
|
|
+ }
|
|
|
+
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
+ String targetFilePath = (UPLOAD_DIR.endsWith(File.separator) ? UPLOAD_DIR : UPLOAD_DIR + "/") + "kanbanlogo/" + fileName;
|
|
|
+ File targetFile = new File(targetFilePath);
|
|
|
+ if (!targetFile.getParentFile().exists()) {
|
|
|
+ targetFile.getParentFile().mkdirs();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ file.transferTo(targetFile);
|
|
|
+ Logo logo = new Logo();
|
|
|
+ logo.setName(fileName);
|
|
|
+ logo.setPath(targetFile.getAbsolutePath());
|
|
|
+ Logo saveLogo = logoDao.save(logo);
|
|
|
+ message.put("success", true);
|
|
|
+ message.put("message", "成功上传文件至 :" + targetFile.getCanonicalPath());
|
|
|
+ message.put("logoCode", saveLogo.getCode());
|
|
|
+ message.put("logoName", saveLogo.getName());
|
|
|
+ logger.info((String) message.get("message"));
|
|
|
+ return message;
|
|
|
+ } catch (IllegalStateException | IOException e) {
|
|
|
+ logger.error("", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ message.put("success", false);
|
|
|
+ message.put("message", "上传文件失败: " + fileName);
|
|
|
+ logger.error((String) message.get("message"));
|
|
|
+ return message;
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping(value = "/getLogo", method = RequestMethod.GET)
|
|
|
+ @ResponseBody
|
|
|
+ public String getLogo(String logoCode, HttpServletRequest request, HttpServletResponse response)
|
|
|
+ throws IOException {
|
|
|
+ Logo logo = logoDao.getLogoByCode(logoCode);
|
|
|
+ ServletOutputStream out = null;
|
|
|
+ FileInputStream ips = null;
|
|
|
+ try {
|
|
|
+ // 获取图片存放路径
|
|
|
+ String imgPath = logo.getPath();
|
|
|
+ File logoFile = new File(imgPath);
|
|
|
+
|
|
|
+ // 图片缺失时使用缺省图片
|
|
|
+ if (!logoFile.exists()) {
|
|
|
+ logoFile = new File(NO_LOGO_PATH);
|
|
|
+ }
|
|
|
+ ips = new FileInputStream(logoFile);
|
|
|
+ response.setContentType("multipart/form-data");
|
|
|
+ out = response.getOutputStream();
|
|
|
+ // 读取文件流
|
|
|
+ int len = 0;
|
|
|
+ byte[] buffer = new byte[1024 * 10];
|
|
|
+ while ((len = ips.read(buffer)) != -1) {
|
|
|
+ out.write(buffer, 0, len);
|
|
|
+ }
|
|
|
+ out.flush();
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ out.close();
|
|
|
+ ips.close();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|