UploadServiceImpl.java 13.8 KB
Newer Older
1 2
package cn.wisenergy.service.app.impl;

3 4
import cn.wisenergy.common.utils.FileException;
import cn.wisenergy.common.utils.FileUtils;
5 6
import cn.wisenergy.common.utils.FrameGrabberKit;

liqin's avatar
liqin committed
7
import cn.wisenergy.common.utils.StringUtil;
8 9
import cn.wisenergy.model.app.Page;
import cn.wisenergy.model.app.shopZx;
10
import com.alibaba.fastjson.JSON;
11
import com.youzan.cloud.open.sdk.common.util.CheckUtils;
12 13 14 15 16 17
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
18

19 20 21 22 23 24 25 26 27 28 29
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import cn.wisenergy.service.app.UploadService;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
30 31 32 33 34 35 36
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import cn.wisenergy.mapper.ShopZxMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ClassUtils;
37

38 39 40 41 42 43

/**
 * Created by m1991 on 2021/2/24 13:42
 */
@Slf4j
@Transactional
licc's avatar
licc committed
44
@Service
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
public class UploadServiceImpl implements UploadService {

    @Autowired
    private ShopZxMapper shopZxMapper;

        /**
         * 视频文件上传
         */
        @Override
        public Map<String, Object> uploadVideo(MultipartFile file,HttpServletRequest request,String userId,String zxField,String zxAddress) throws Exception {
            Map<String, Object> resultMap=new HashMap<String, Object>();

            String basePath = request.getScheme() + "://" + request.getServerName()
                    + ":" + request.getServerPort()+"/mimi/upload/video/";

licc's avatar
licc committed
60
            Long time = System.currentTimeMillis();
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

            String zxName = file.getOriginalFilename();//文件原始名称
            String suffixName = zxName.substring(zxName.lastIndexOf("."));//从最后一个.开始截取。截取zxName的后缀名
            String newzxName = time+suffixName; //文件新名称
            //设置文件存储路径,可以存放在你想要指定的路径里面
            String rootPath="D:/mimi/"+File.separator+"upload/video/"; //上传视频存放位置

            String filePath = rootPath+newzxName;

            File newFile = new File(filePath);


            //判断目标文件所在目录是否存在
            if(!newFile.getParentFile().exists()){
                //如果目标文件所在的目录不存在,则创建父目录
                newFile.getParentFile().mkdirs();
            }

            //将内存中的数据写入磁盘
            file.transferTo(newFile);
            //视频上传保存url
            String zxUrl = basePath + newzxName;

            //视频封面图处理
            String newImgName = time+".jpg";
            String framefile = rootPath + newImgName;
            String imgUrlSave = basePath+newImgName;//图片最终位置路径


            //视频截取封面图
            String imgUrl=FrameGrabberKit.getVedioImg(zxUrl, framefile, imgUrlSave);


            resultMap.put("zxUrl", zxUrl);
            resultMap.put("imgUrl", imgUrl);
            resultMap.put("returnCode", 0);
            System.out.println("上传的文件名为:"+zxName+",后缀名为:"+newzxName);

            shopZxMapper.zxadd(zxUrl,userId,zxName,zxField,imgUrl,zxAddress);
            return resultMap;
        }

        /**
         * 图片文件上传
         */
        @Override
        public Map<String, Object> uploadImage(MultipartFile file, HttpServletRequest request,String userId,String zxField, String zxAddress) throws Exception {
            Map<String, Object> resultMap=new HashMap<String, Object>();

            String basePath = request.getScheme() + "://" + request.getServerName()
                    + ":" + request.getServerPort()+"/mimi/upload/images/";

licc's avatar
licc committed
113
            Long time = System.currentTimeMillis();
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

            String zxName = file.getOriginalFilename();//文件原始名称
            String suffixName = zxName.substring(zxName.lastIndexOf("."));//从最后一个.开始截取。截取zxName的后缀名
            String newzxName = time+suffixName; //文件新名称
            //设置文件存储路径,可以存放在你想要指定的路径里面
            String rootPath="D:/mimi/"+File.separator+"upload/images/"; //上传图片存放位置

            String filePath = rootPath+newzxName;
            File newFile = new File(filePath);
            //判断目标文件所在目录是否存在
            if(!newFile.getParentFile().exists()){
                //如果目标文件所在的目录不存在,则创建父目录
                newFile.getParentFile().mkdirs();
            }

            //将内存中的数据写入磁盘
            file.transferTo(newFile);
            //图片上传保存url
            String imgUrl = basePath + newzxName;

            resultMap.put("imgUrl", imgUrl);
            resultMap.put("returnCode", 0);
            String zxUrl="";
            shopZxMapper.zxadd(zxUrl,userId,zxName,zxField,imgUrl,zxAddress);
            return resultMap;
        }

        /**
         * 项目目录下的图片文件上传
         */
        @Override
        public Map<String, Object> getImageUrl(HttpServletRequest request) throws Exception {
            Map<String,Object> result = new HashMap<String,Object>();
            //获取图片在项目路径下的地址
            String basePath= ClassUtils.getDefaultClassLoader().getResource("").getPath();
            String oldPath=basePath+"/static/images/animate.png";

licc's avatar
licc committed
151
            Long res =System.currentTimeMillis();
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
            //设置文件存储路径,可以存放在你想要指定的路径里面
            String rootPath="D:/mimi/"+File.separator+"upload/images/";
            // 新文件名
            String newzxName =res + oldPath.substring(oldPath.lastIndexOf("."));
            //新文件
            File newFile=new File(rootPath+File.separator+newzxName);
            //判断文件目录是否存在
            if(!newFile.getParentFile().exists()){
                //如果目标文件所在的目录不存在,则创建父目录
                newFile.getParentFile().mkdirs();
            }

            //-------把图片文件写入磁盘 start ----------------
            FileOutputStream fos = new FileOutputStream(newFile);
            //读取本地文件
            File localFile = new File(oldPath);
            //获取本地文件输入流
            InputStream stream=new FileInputStream(localFile);
            //写入目标文件
            byte[] buffer=new byte[1024*1024];
            int byteRead=0;
            //stream.read(buffer) 每次读到的数据存放在 buffer 数组中
            while((byteRead=stream.read(buffer))!=-1){
                //在 buffer 数组中 取出数据 写到 (输出流)磁盘上
                fos.write(buffer, 0, byteRead);
                fos.flush();
            }
            fos.close();
            stream.close();
            //-------把图片文件写入磁盘 end ----------------

            //服务器图片地址
            String baseURL = request.getScheme() + "://" + request.getServerName()
                    + ":" + request.getServerPort()+"/mimi/upload/images/";
            String imgUrl=baseURL+newzxName;

            result.put("imgUrl", imgUrl);
            result.put("returnCode", 0);
            return result;
        }
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
        //, String userId, String zxField, String zxAddress

    @Override
    public List imageUpload(MultipartFile[] files,String userId, String zxField, String zxAddress) {
        //获取上传图片数量,打印在控制台
        System.out.println("上传图片数量" + files.length);
        //创建集合
        List<Map<String, Object>> root = new ArrayList<Map<String, Object>>();

        String imgUrl = null;
        String zxName=null;
        String fileName=null;
        String zxUrl=null;
        // 要上传的目标文件存放的绝对路径
        final String localPath = "d:\\IDEAProject\\imageupload\\target\\classes\\static\\img";
        Map<String, Object> result = new HashMap<String, Object>();//一个文件上传的结果
        String result_msg = "";//上传结果信息
        //遍历图片数据
        for (MultipartFile file : files) {
//            Map<String, Object> result = new HashMap<String, Object>();//一个文件上传的结果

            if (file.isEmpty()) {
                System.out.println("上传图片为空,请重新上传");
            }
            //判断上传文件格式
            String fileType = file.getContentType();
            if (fileType.equals("image/jpeg") || fileType.equals("image/png") || fileType.equals("image/jpeg")) {
                // 要上传的目标文件存放的绝对路径

                //上传后保存的文件名(需要防止图片重名导致的文件覆盖)
                //获取文件名
                fileName = file.getOriginalFilename();
                //获取文件后缀名
                String suffixName = fileName.substring(fileName.lastIndexOf("."));
                //重新生成文件名
                fileName = UUID.randomUUID() + suffixName;

                zxUrl="";
                zxName+=fileName+",";
                imgUrl+=localPath+fileName+",";
               // shopZxMapper.zxadd(zxUrl,userId,zxName,zxField,imgUrl,zxAddress);
                if (FileUtils.upload(file, localPath, fileName)) {
                    //文件存放的相对路径(一般存放在数据库用于img标签的src)
                    String relativePath = "img/" + fileName;
                    result.put("relativePath", relativePath);//前端根据是否存在该字段来判断上传是否成功
                    result_msg = "图片上传成功";
                    result.put("imgUrl", imgUrl);

                } else {
                    result_msg = "图片上传失败";
                }
            } else {
                result_msg = "图片格式不正确";
            }

        }
        result.put("result_msg", result_msg);
        root.add(result);
        String root_json = JSON.toJSONString(root);
        System.out.println(root_json);
        result.put("returnCode", 0);
        System.out.println(zxName);
        System.out.println(imgUrl);
        shopZxMapper.zxadd(zxUrl,userId,zxName,zxField,imgUrl,zxAddress);
        return root;


    }
260

261

262 263 264 265 266

    /**
     *  获取资讯分页数据
     * @return
  */
267
//    @Override
268 269 270 271 272
    /**
     * 多文件上传 TODO
     */
    private Path fileStorageLocation; // 文件在本地存储的地址

liqin's avatar
liqin committed
273
    public UploadServiceImpl(@Value("${file.upload.path:#{null}}") String path) {
liqin's avatar
liqin committed
274 275 276 277 278 279 280
        if (!StringUtil.isBlank(path)) {
            this.fileStorageLocation = Paths.get(path).toAbsolutePath().normalize();
            try {
                Files.createDirectories(this.fileStorageLocation);
            } catch (IOException e) {
                throw new FileException("Could not create the directory", e);
            }
281 282 283 284 285 286 287 288
        }
    }

    /**
     * 存储文件到系统
     * @param file 文件
     * @return 文件名
     */
licc's avatar
licc committed
289
    @Override
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
    public String storeFile(MultipartFile file) {
        // Normalize file name
        String fileName = StringUtils.cleanPath(file.getOriginalFilename());

        try {
            // Check if the file's name contains invalid characters
            if(fileName.contains("..")) {
                throw new FileException("Sorry! Filename contains invalid path sequence " + fileName);
            }

            // Copy file to the target location (Replacing existing file with the same name)
            Path targetLocation = this.fileStorageLocation.resolve(fileName);
            Files.copy(file.getInputStream(), targetLocation, StandardCopyOption.REPLACE_EXISTING);

            return fileName;
        } catch (IOException ex) {
            throw new FileException("Could not store file " + fileName + ". Please try again!", ex);
        }
    }

licc's avatar
licc committed
310
    @Override
311 312 313 314 315 316 317 318 319 320 321 322 323 324
    public Resource loadFileAsResource(String fileName) {
        try {
            Path filePath = this.fileStorageLocation.resolve(fileName).normalize();
            Resource resource = new UrlResource(filePath.toUri());
            if(resource.exists()) {
                return resource;
            } else {
                throw new FileException("File not found " + fileName);
            }
        } catch (MalformedURLException  ex) {
            throw new FileException("File not found " + fileName, ex);
        }
    }

325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
    @Override
    public Map getList(int beginPos, int pageSize) {
        Map map =new HashMap();
        String[] ImgUrl = new String[0];
        List<shopZx> date=shopZxMapper.selectAll(beginPos,pageSize);
        for(int i=0;date.size()>0;i++){
            shopZx s = (shopZx)date.get(i);
            ImgUrl=s.getImgUrl().split(",");
        }
        
        if(null==date){
            map.put("code",1);
            map.put("msg","获取资讯分页数据失败!");
            return map;
        }else {
            map.put("date",date);
            map.put("ImgUrl",ImgUrl);
            map.put("code",0);
            map.put("msg","获取资讯分页数据成功!");
            return map;
        }
    }

//    @Override
//    public Page<shopZx> find() {
//        if (null == page) {
//            page = 0;
//        }
//        if (CheckUtils.isEmpty(size)) {
//            size = 10;
//        }
//        PageRequest pageable = PageRequest.of(page, size, Sort.Direction.DESC, "updateTime");
//        Page<User> users = userRepository.findAll(pageable);
//        return users;
//    }

361
}