• 上传图片的工具类


    package com.laima.car.common;

    import java.io.File;
    import java.io.IOException;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.UUID;

    import javax.servlet.http.HttpServletRequest;

    import org.springframework.stereotype.Controller;
    import org.springframework.util.FileCopyUtils;
    import org.springframework.web.bind.annotation.CrossOrigin;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.multipart.MultipartFile;

    import com.laima.car.common.NotLogin;
    import com.laima.car.util.DateUtils;

    @Controller
    @RequestMapping(value = "/upLoad")
    @CrossOrigin(origins = "*",maxAge = 3600)
    public class UpLoadifyController {

        @RequestMapping(value = "/uploadRichImage", method = RequestMethod.POST)
        @ResponseBody
        @NotLogin
        public Map<String, Object> uploadRichImage(HttpServletRequest request, @RequestParam("file") MultipartFile uploadify)
                throws IOException {
            Map<String, Object> res = new HashMap<String, Object>();
            String uuid = UUID.randomUUID().toString().replace("-", "");
            //打印文件地址,用分割,D:eclipseworkcarrsrcmainwebapp,存储的目标地址
            String filePath = request.getSession().getServletContext().getRealPath("/");
            //获取本地的文件名称
            String fileName2 = uploadify.getOriginalFilename();
            //获取最后一个.
            String fileExt = fileName2.substring(fileName2.lastIndexOf("."));
            
            String newFileName = uuid + fileExt;
            String date = DateUtils.parseDate(new Date(), "yyyyMMdd");
            String configPath = File.separator + "uploadify" + File.separator + "image" + File.separator + date
                    + File.separator;
            filePath += configPath;
            File file = new File(filePath);
            if (!file.exists()) {
                file.mkdirs();
            }
            File newFile = new File(filePath + newFileName);
            FileCopyUtils.copy(uploadify.getBytes(), newFile);
            Map<String, Object> map = new HashMap<String, Object>();
            res.put("code", "0");
            res.put("msg", "");
            res.put("data", map);
            map.put("src", SysConstant.URL+"/uploadify/image/"+date+"/"+newFileName);
            map.put("title", fileName2);
            return res;
        }
        
        @RequestMapping(value="/uploadImage",method=RequestMethod.POST)
        @ResponseBody
        @NotLogin
        public Map<String, Object> uploadImage(HttpServletRequest request, @RequestParam("file") MultipartFile uploadify) throws IOException{
            String uuid = UUID.randomUUID().toString().replace("-", "");
            String filePath = request.getSession().getServletContext().getRealPath("/");
            String fileName2 = uploadify.getOriginalFilename();
            String fileExt = fileName2.substring(fileName2.lastIndexOf("."));
            String newFileName = uuid + fileExt;
            String date = DateUtils.parseDate(new Date(), "yyyyMMdd");
            String configPath = File.separator + "uploadify" + File.separator + "image"
                + File.separator + date + File.separator;
            filePath +=configPath;
            File file = new File(filePath);
            if (!file.exists()) {
                file.mkdirs();
            }
            File newFile = new File(filePath + newFileName);
             FileCopyUtils.copy(uploadify.getBytes(), newFile);
             Map<String, Object> map = new HashMap<String, Object>();
             map.put("url", date+"/"+newFileName);
             map.put("oldName", fileName2);
            return map;
        }
        
        @RequestMapping(value = "/uploadActivityImage", method = RequestMethod.POST)
        @ResponseBody
        @NotLogin
        public Map<String, Object> uploadActivityImage(HttpServletRequest request, @RequestParam("file") MultipartFile uploadify)
                throws IOException {
            String uuid = UUID.randomUUID().toString().replace("-", "");
            String filePath = request.getSession().getServletContext().getRealPath("/");
            String fileName2 = uploadify.getOriginalFilename();
            String fileExt = fileName2.substring(fileName2.lastIndexOf("."));
            String newFileName = uuid + fileExt;
            String date = DateUtils.parseDate(new Date(), "yyyyMMdd");
            String configPath = File.separator + "uploadify" + File.separator + "activity"
                + File.separator + date + File.separator;
            filePath +=configPath;
            File file = new File(filePath);
            if (!file.exists()) {
                file.mkdirs();
            }
            File newFile = new File(filePath + newFileName);
             FileCopyUtils.copy(uploadify.getBytes(), newFile);
             Map<String, Object> map = new HashMap<String, Object>();
             map.put("url", date+"/"+newFileName);
             map.put("oldName", fileName2);
            return map;
        }
        
        @RequestMapping(value="/uploadAppImage",method=RequestMethod.POST)
        @ResponseBody
        @NotLogin
        public Map<String, Object> uploadAppImage(HttpServletRequest request, @RequestParam("file") MultipartFile uploadify) throws IOException{
            String uuid = UUID.randomUUID().toString().replace("-", "");
            String filePath = request.getSession().getServletContext().getRealPath("/");
            String fileName2 = uploadify.getOriginalFilename();
            String fileExt = fileName2.substring(fileName2.lastIndexOf("."));
            String newFileName = uuid + fileExt;
            String date = DateUtils.parseDate(new Date(), "yyyyMMdd");
            String configPath = File.separator + "uploadify" + File.separator + "image"
                + File.separator + date + File.separator;
            filePath +=configPath;
            File file = new File(filePath);
            if (!file.exists()) {
                file.mkdirs();
            }
            File newFile = new File(filePath + newFileName);
             FileCopyUtils.copy(uploadify.getBytes(), newFile);
             Map<String, Object> map = new HashMap<String, Object>();
             map.put("url", SysConstant.URL+"/uploadify/image/"+date+"/"+newFileName);
             map.put("oldName", fileName2);
            return map;
        }

    }

  • 相关阅读:
    冷门JS技巧
    JavaScript小技巧整理篇(非常全)
    JS实现标签页切换效果
    MySQL主从配置详解
    mysql主从复制(超简单)
    1.4isAlive()方法
    1.3currentThread()方法
    1.2.4注意Sysyem.out.println与i--
    1.2.3实例变量与线程安全
    1.2.2实现Runnable接口
  • 原文地址:https://www.cnblogs.com/layuechuquwan/p/12692495.html
Copyright © 2020-2023  润新知