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


```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);

                }
            }
        }
    }
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值