欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實(shí)戰(zhàn)之實(shí)現(xiàn)文件資料上傳并生成縮略圖

 更新時(shí)間:2021年12月17日 11:00:06   作者:小小張自由—>張有博  
這篇文章主要介紹了通過(guò)Java實(shí)現(xiàn)文件資料的上傳并生成一個(gè)縮略圖,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Java有一定的幫助,感興趣的小伙伴可以了解一下

一:需求

用戶有一個(gè)需求就是收藏的功能,單純的收藏記錄好做,但是想加點(diǎn)難度就是,生成縮略圖。類(lèi)似B站的收藏一樣。

二:如何實(shí)現(xiàn)需求,以及其中遇到的問(wèn)題?

需求分析時(shí)遇到的問(wèn)題

1.如何生成縮略圖?

2.收藏的時(shí)候生成縮略圖還是文件上傳的時(shí)候生成縮略圖?

3.生成的縮略圖存在哪里?

編碼過(guò)程中遇到的問(wèn)題

1.文件截取比較慢,如何解決?

方法:使用多線程

2.通過(guò)new File()無(wú)法獲取網(wǎng)絡(luò)文件?

File file = new File("http://d-godone.dmsd.tech/goDone/M00/00/0A/wKg8O2D2mnqEMg7wAAAAALbl5Ys275.pdf");

方法:使用工具類(lèi)FileUtils.copyURLToFile(url,file)

3.文件上傳需要MultipartFile類(lèi)型,但是截取出來(lái)的類(lèi)型為Frame和BufferedImage,怎么弄?

方法:查閱資料封裝出,文件轉(zhuǎn)換的方法。

三:流程圖與UML圖

四:上傳文件資料生成縮略圖

1.上傳圖片生成縮略圖--FastDFS

/**
 * @Author: Promsing(張有博)
 * @Date: 2021/9/18 - 0:25
 * @Description: 圖片的處理類(lèi)
 * @version: 1.0
 */
@Slf4j
@Component
public class ImageProcessing extends TypeProcessing{
 
    @Autowired
    private FastFileStorageClient fastFileStorageClient;
 
    @Autowired
    private ThumbImageConfig thumbImageConfig;
 
    private  String group="http://192.168.y.y:8888/group1/";
 
    /**
     *
     * @param fileName 本地文件的位置
     * @return
     */
    @Override
    public String fileUpload(String fileName) {
 
        File file = new File(fileName);
 
 
        // 上傳并且生成縮略圖
        StorePath storePath = null;
        try {
            storePath = this.fastFileStorageClient.uploadImageAndCrtThumbImage(
                    new FileInputStream(file), file.length(), "png", null);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //String path = thumbImageConfig.getThumbImagePath(storePath.getPath());
 
        String imageUrl=group+thumbImageConfig.getThumbImagePath(storePath.getPath());
 
        return imageUrl;
 
    }
}

2.上傳視頻生成縮略圖--javacv

/**
 * @Author: Promsing(張有博)
 * @Date: 2021/9/18 - 0:37
 * @Description: 視頻的處理類(lèi)
 * @version: 1.0
 */
@Component
@Slf4j
public class VideoProcessing extends TypeProcessing {
 
    @Override
    public String fileUpload(String videoFileName) {
        //最后獲取到的視頻的圖片的路徑
        String videPicture="";
        //Frame對(duì)象
        Frame frame = null;
        //標(biāo)識(shí)
        int flag = 0;
        try {
             /*
            獲取視頻文件
            */
            // FFmpegFrameGrabber fFmpegFrameGrabber = new FFmpegFrameGrabber(videoPath + "/" + videoFileName);
            FFmpegFrameGrabber fFmpegFrameGrabber = new FFmpegFrameGrabber( videoFileName);
            fFmpegFrameGrabber.start();
 
            //獲取視頻總幀數(shù)
            int ftp = fFmpegFrameGrabber.getLengthInFrames();
            log.info("時(shí)長(zhǎng) " + ftp / fFmpegFrameGrabber.getFrameRate() / 60);
 
 
            while (flag <= ftp) {
                frame = fFmpegFrameGrabber.grabImage();
 
                /*
                對(duì)視頻的第五幀進(jìn)行處理
                 */
                if (frame != null && flag==5) {
 
//                    //文件轉(zhuǎn)換
                    BufferedImage bufferedImage =FileCaseUtil.FrameToBufferedImage(frame);
 
                    MultipartFile multipartFile =FileCaseUtil. fileCase(bufferedImage);
                    log.info("開(kāi)始文件上傳:");
                    //文件上傳
                    String fileLoad = FileUploadUtil.fileLoad(multipartFile);
 
                    videPicture=fileLoad;
                    log.info("文件上傳成功{}",fileLoad);
                    break;
 
                }
                flag++;
            }
            fFmpegFrameGrabber.stop();
            fFmpegFrameGrabber.close();
        } catch (Exception E) {
            E.printStackTrace();
        }
        return videPicture;
 
    }
 
}

3.上傳PDF生成縮略圖--pdfbox

/**
 * @Author: Promsing(張有博)
 * @Date: 2021/9/18 - 0:29
 * @Description: PDF的處理類(lèi)
 * @version: 1.0
 */
@Slf4j
@Component
public class PDFProcessing extends TypeProcessing{
    @Override
    public String fileUpload(String fileName) {
 
        String pdfUrl="";
        try
        {
            //將讀取URL生成File
            File file = FileCaseUtil. URLToFile(fileName);
 
 
            // 打開(kāi)來(lái)源 使用pdfbox中的方法
            PDDocument pdfDocument = PDDocument.load(file);
            PDFRenderer pdfRenderer = new PDFRenderer(pdfDocument);
 
            // 提取的頁(yè)碼
            int pageNumber = 0;
            // 以300 dpi 讀取存入 BufferedImage 對(duì)象
            int dpi = 300;
            BufferedImage buffImage = pdfRenderer.renderImageWithDPI(pageNumber, dpi, ImageType.RGB);
            // 將 BufferedImage 寫(xiě)入到 png
            // ImageIOUtil.writeImage(buffImage, "c:/temp/xx.png", dpi);
 
            //這里使用文件BufferedImage上傳
 
            // 文件上傳
            MultipartFile multipartFile =FileCaseUtil. fileCase(buffImage);
            log.info("PDF開(kāi)始上傳:");
             pdfUrl=FileUploadUtil. fileLoad(multipartFile);
            log.info("PDF上傳成功:{}",pdfUrl);
 
//            //文件儲(chǔ)存對(duì)象
//            File outPut = new File("C:\\Users\\Administrator\\Videos\\"+ UUID.randomUUID().toString()+".jpg");
//            // ImageIO.write(FrameToBufferedImage(frame), "jpg", outPut);
//            ImageIO.write(buffImage, "jpg",outPut);
 
            // 關(guān)閉文檔
            pdfDocument.close();
 
            //注意關(guān)閉文檔與刪除文檔的順序
            //刪除臨時(shí)的file
            String s = FileCaseUtil.threadLocal.get();
            System.out.println(s);
 
            File f=new File(s);
            boolean delete = f.delete();
            System.out.println("文件的刪除狀態(tài)"+delete);
        }
        catch (InvalidPasswordException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
 
        return pdfUrl;
 
    }
}

4.封裝好的方法

 public static File URLToFile(String url){
        log.info("讀取FastDFS上的ppt");
        File file1 = new File("test.pdf");
        try {
 
            URL url1 = new URL(url);
            FileUtils.copyURLToFile(url1,file1);
 
        } catch (IOException e) {
            e.printStackTrace();
        }
        File absoluteFile = file1.getAbsoluteFile();
        threadLocal.set(absoluteFile.toString());
        log.info("ppt已經(jīng)存儲(chǔ)到本地"+absoluteFile.toString());
        return file1;
    }
 
public static BufferedImage FrameToBufferedImage(Frame frame) {
        //創(chuàng)建BufferedImage對(duì)象
        Java2DFrameConverter converter = new Java2DFrameConverter();
        BufferedImage bufferedImage = converter.getBufferedImage(frame);
        return bufferedImage;
    }
 
 public static MultipartFile fileCase(BufferedImage image){
        //得到BufferedImage對(duì)象
       // BufferedImage bufferedImage = JoinTwoImage.testEncode(200, 200, url);
        MultipartFile multipartFile= null;
        try {
            //創(chuàng)建一個(gè)ByteArrayOutputStream
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            //把BufferedImage寫(xiě)入ByteArrayOutputStream
            ImageIO.write(image, "jpg", os);
            //ByteArrayOutputStream轉(zhuǎn)成InputStream
            InputStream input = new ByteArrayInputStream(os.toByteArray());
            //InputStream轉(zhuǎn)成MultipartFile
            multipartFile =new MockMultipartFile("file", "file.jpg", "text/plain", input);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return multipartFile;
 
    }
 
  public static String fileLoad(MultipartFile mf) throws IOException {
        //上傳視頻并返回視頻地址
        StorePath storePath = fastFileStorageClient.uploadFile(group,mf.getInputStream(), mf.getSize(), mf.getOriginalFilename().substring(mf.getOriginalFilename().lastIndexOf(".")+1));
        //將視頻地址和項(xiàng)目id存到項(xiàng)目資料表中
        String videoUrl = "http://d-godone.yyy.tech/goDone/"+storePath.getPath();
        return videoUrl;
    }

五:總結(jié)

1.收到需求先拆分業(yè)務(wù)、梳理邏輯,通過(guò)畫(huà)圖、偽代碼的形式去熟悉業(yè)務(wù),梳理邏輯

2.將大的工作任務(wù),拆分成小塊。一點(diǎn)一點(diǎn)去實(shí)現(xiàn),然后再去串起來(lái)。

3.遇到困難,不要怕,總會(huì)有解決辦法,好的辦法要不斷查找才能獲得。?

以上就是Java實(shí)戰(zhàn)之實(shí)現(xiàn)文件資料上傳并生成縮略圖的詳細(xì)內(nèi)容,更多關(guān)于Java 上傳文件 生成縮略圖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 使用JPA插入枚舉類(lèi)型字段

    使用JPA插入枚舉類(lèi)型字段

    這篇文章主要介紹了使用JPA插入枚舉類(lèi)型字段,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • java如何讀取yaml配置文件

    java如何讀取yaml配置文件

    這篇文章主要介紹了java如何讀取yaml配置文件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Java?Spring中Bean的作用域及生命周期

    Java?Spring中Bean的作用域及生命周期

    這篇文章主要介紹了Java?Spring中Bean的作用域及生命周期,Bean的作用域默認(rèn)是單例模式的,也就是說(shuō)所有?的使?的都是同?個(gè)對(duì)象,更多相關(guān)內(nèi)容需要的朋友可以參考一下
    2022-08-08
  • Java中Arrays.sort自定義一維數(shù)組、二維數(shù)組的排序方式

    Java中Arrays.sort自定義一維數(shù)組、二維數(shù)組的排序方式

    這篇文章主要介紹了Java中Arrays.sort自定義一維數(shù)組、二維數(shù)組的排序方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • 淺談Maven鏡像更換為阿里云中央倉(cāng)庫(kù)(精)

    淺談Maven鏡像更換為阿里云中央倉(cāng)庫(kù)(精)

    本篇文章主要介紹了Maven鏡像更換為阿里云中央倉(cāng)庫(kù)(精),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12
  • Java對(duì)象傳遞與返回的細(xì)節(jié)問(wèn)題詳析

    Java對(duì)象傳遞與返回的細(xì)節(jié)問(wèn)題詳析

    我們知道這是一個(gè)核心概念,在Java中總是按值傳遞而不是按引用傳遞,下面這篇文章主要給大家介紹了關(guān)于Java對(duì)象傳遞與返回的細(xì)節(jié)問(wèn)題的相關(guān)資料,需要的朋友可以參考下
    2022-11-11
  • java面試常見(jiàn)問(wèn)題---ConcurrentHashMap

    java面試常見(jiàn)問(wèn)題---ConcurrentHashMap

    ConcurrentHashMap是由Segment數(shù)組結(jié)構(gòu)和HashEntry數(shù)組結(jié)構(gòu)組成。Segment的結(jié)構(gòu)和HashMap類(lèi)似,是一種數(shù)組和鏈表結(jié)構(gòu),今天給大家普及java面試常見(jiàn)問(wèn)題---ConcurrentHashMap知識(shí),一起看看吧
    2021-06-06
  • Spring Cloud @EnableFeignClients注解的屬性字段basePacka詳解

    Spring Cloud @EnableFeignClients注解的屬性字段basePacka詳解

    這篇文章主要介紹了Spring Cloud @EnableFeignClients注解的屬性字段basePacka詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • SpringBoot與velocity的結(jié)合的示例代碼

    SpringBoot與velocity的結(jié)合的示例代碼

    本篇文章主要介紹了SpringBoot與velocity的結(jié)合的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • RabbitMQ,RocketMQ,Kafka?事務(wù)性,消息丟失,消息順序性和消息重復(fù)發(fā)送的處理策略問(wèn)題

    RabbitMQ,RocketMQ,Kafka?事務(wù)性,消息丟失,消息順序性和消息重復(fù)發(fā)送的處理策略問(wèn)題

    這篇文章主要介紹了RabbitMQ,RocketMQ,Kafka?事務(wù)性,消息丟失,消息順序性和消息重復(fù)發(fā)送的處理策略,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03

最新評(píng)論