zoukankan      html  css  js  c++  java
  • 上传图片的工具类

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

    }

  • 相关阅读:
    WSS3.0安装后,系统资源消耗这么大
    通过名称找到控件(VB.NET)
    zencart 对首页静态化处理
    zen cart 模板类 template_fun class
    现在网络上流行的病毒都太“厚道”了
    从SPS2003的邮件设置上看微软标准与国际标准
    我也有了BLOG,欢迎交流
    如何给WEBPART中增加客户端代码
    如何修改More Information 中的Page 2,Page 3,Page4
    汽车保养项目[转载]
  • 原文地址:https://www.cnblogs.com/layuechuquwan/p/12692495.html
Copyright © 2011-2022 走看看