溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java中如何實(shí)現(xiàn)實(shí)現(xiàn)文件資料上傳并生成縮略圖

發(fā)布時(shí)間:2021-12-17 14:12:34 來源:億速云 閱讀:579 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹Java中如何實(shí)現(xiàn)實(shí)現(xiàn)文件資料上傳并生成縮略圖,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

一:需求

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

Java中如何實(shí)現(xiàn)實(shí)現(xiàn)文件資料上傳并生成縮略圖

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

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

1.如何生成縮略圖?

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

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

編碼過程中遇到的問題

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

方法:使用多線程

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

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

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

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

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

三:流程圖與UML圖

Java中如何實(shí)現(xiàn)實(shí)現(xiàn)文件資料上傳并生成縮略圖

Java中如何實(shí)現(xiàn)實(shí)現(xiàn)文件資料上傳并生成縮略圖

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

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

/**
 * @Author: Promsing(張有博)
 * @Date: 2021/9/18 - 0:25
 * @Description: 圖片的處理類
 * @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: 視頻的處理類
 * @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í)長 " + 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("開始文件上傳:");
                    //文件上傳
                    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的處理類
 * @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);
 
 
            // 打開來源 使用pdfbox中的方法
            PDDocument pdfDocument = PDDocument.load(file);
            PDFRenderer pdfRenderer = new PDFRenderer(pdfDocument);
 
            // 提取的頁碼
            int pageNumber = 0;
            // 以300 dpi 讀取存入 BufferedImage 對(duì)象
            int dpi = 300;
            BufferedImage buffImage = pdfRenderer.renderImageWithDPI(pageNumber, dpi, ImageType.RGB);
            // 將 BufferedImage 寫入到 png
            // ImageIOUtil.writeImage(buffImage, "c:/temp/xx.png", dpi);
 
            //這里使用文件BufferedImage上傳
 
            // 文件上傳
            MultipartFile multipartFile =FileCaseUtil. fileCase(buffImage);
            log.info("PDF開始上傳:");
             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寫入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;
    }

以上是“Java中如何實(shí)現(xiàn)實(shí)現(xiàn)文件資料上傳并生成縮略圖”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI