上传视频、音频、图片的工具


```java
package org.jeecg.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.commons.io.FileUtils;
import org.jeecg.modules.ly.entity.CaseBase;
import org.jeecg.modules.ly.entity.CaseImg;
import org.jeecg.modules.ly.entity.CaseSound;
import org.jeecg.modules.ly.entity.CaseVideo;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FileUploadUtil {

    public void fileUpload(MultipartFile[] file, CaseBase caseBase, HttpServletRequest request) {
        ServletContext sc = request.getSession().getServletContext();
        CaseImg caseImg = new CaseImg();
        CaseVideo caseVideo = new CaseVideo();
        CaseSound caseSound = new CaseSound();
        if (file.length > 0) {
            List<CaseImg> imgList = new ArrayList();
            List<CaseVideo> videoList = new ArrayList();
            List<CaseSound> soundList = new ArrayList();
            for (MultipartFile multipartFile : file) {
                String contentType = multipartFile.getContentType().substring(0, multipartFile.getContentType().indexOf("/"));
                System.out.println(contentType);
                String dir = sc.getRealPath("/webDisk/files");    //设定文件保存的目录
                String filename = multipartFile.getOriginalFilename();    //得到上传时的文件名
                //写入文件到指定路径
                try {
                    FileUtils.writeByteArrayToFile(new File(dir, filename), multipartFile.getBytes());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //储存信息到表tbweddislicon
                String path = "/webDisk/files/" + filename;
                if (contentType.equals("image")) {
                    caseImg.setImgPath(path);
                    caseImg.setCaseId(caseBase.getCaseId());
                    caseImg.setImgName(filename);
                    imgList.add(caseImg);
                    String Imglist = JSON.toJSONString(imgList, SerializerFeature.DisableCircularReferenceDetect);
                    caseBase.setImgLst(Imglist);

                } else if (contentType.equals("video")) {
                    caseVideo.setVideoPath(path);
                    caseVideo.setCaseId(caseBase.getCaseId());
                    caseVideo.setVideoName(filename);
                    videoList.add(caseVideo);
                    String VideoOne = JSON.toJSONString(videoList, SerializerFeature.DisableCircularReferenceDetect);
                    caseBase.setVideoLst(VideoOne);

                } else if (contentType.equals("audio")) {
                    caseSound.setSoundPath(path);
                    caseSound.setCaseId(caseBase.getCaseId());
                    caseSound.setSoundName(filename);
                    soundList.add(caseSound);
                    String SoundOne = JSON.toJSONString(soundList, SerializerFeature.DisableCircularReferenceDetect);
                    caseBase.setSoundLst(SoundOne);

                }
            }
        }
    }
}

发布了14 篇原创文章 · 获赞 23 · 访问量 577

猜你喜欢

转载自blog.csdn.net/qq_41550921/article/details/103857091
今日推荐