• 上传图片的工具类


    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;
        }

    }

  • 相关阅读:
    PAT甲题题解-1030. Travel Plan (30)-最短路+输出路径
    PAT甲题题解-1029. Median (25)-求两序列的中位数,题目更新了之后不水了
    PAT甲题题解-1028. List Sorting (25)-水排序
    BZOJ 1492 货币兑换Cash
    Codeforces 276D Little Girl and Maximum XOR
    Codeforces 526E Transmitting Levels
    Codeforces 335B Palindrome
    BZOJ 2527 Meteors
    Codeforces 449D Jzzhu and Numbers
    FJ省队集训DAY4 T3
  • 原文地址:https://www.cnblogs.com/layuechuquwan/p/12692495.html
Copyright © 2020-2023  润新知