Get the App
SLTechnology News&Howtos  ›  Development  › 

How to upload files and generate thumbnails in Java

Shulou Source: shulou.com Published: 2022-06-02 06:18:30 09月19日 Update

This article mainly introduces how to upload files and generate thumbnails in Java. It is very detailed and has a certain reference value. Friends who are interested must read it!

One: demand

One of the needs of users is the collection function, simple collection records are easy to do, but want to add some difficulty is to generate thumbnails. Similar to bilibili's collection.

Second: how to achieve the requirements and the problems encountered in it? Problems encountered in requirements analysis

1. How do I generate thumbnails?

two。 Generate thumbnails when collecting or thumbnails when uploading files?

3. Where are the generated thumbnails stored?

Problems encountered in the coding process

1. File interception is relatively slow, how to solve it?

Method: use multithreading

two。 Can't get the network file through new File ()?

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

Method: use the tool class FileUtils.copyURLToFile (url,file)

3. MultipartFile type is required for file upload, but the intercepted types are Frame and BufferedImage. How to do it?

Methods: the method of consulting data to encapsulate and converting files.

Three: flow chart and UML diagram

Four: upload files to generate thumbnails 1. Upload pictures to generate thumbnails-- FastDFS/** * @ Author: Promsing (Zhang Youbo) * @ Date: 2021-9-18-0:25 * @ Description: image processing class * @ version: 1.0 * / @ Slf4j@Componentpublic class ImageProcessing extends TypeProcessing {@ Autowired private FastFileStorageClient fastFileStorageClient; @ Autowired private ThumbImageConfig thumbImageConfig; private String group= "http://192.168.y.y:8888/group1/"; / * * @ param fileName location of local files * @ return * / @ Override public String fileUpload (String fileName) {File file = new File (fileName); / / upload and generate thumbnails 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. Upload videos to generate thumbnails-- javacv/** * @ Author: Promsing (Zhang Youbo) * @ Date: 2021-9-18-0:37 * @ Description: video processing class * @ version: 1.0 * / @ Component@Slf4jpublic class VideoProcessing extends TypeProcessing {@ Override public String fileUpload (String videoFileName) {/ / path of the image of the last obtained video String videPicture= "" / / Frame object Frame frame = null; / / identity int flag = 0; try {/ * get video file * / FFmpegFrameGrabber fFmpegFrameGrabber = new FFmpegFrameGrabber (videoPath + "/" + videoFileName); FFmpegFrameGrabber fFmpegFrameGrabber = new FFmpegFrameGrabber (videoFileName); fFmpegFrameGrabber.start () / / get the total number of video frames int ftp = fFmpegFrameGrabber.getLengthInFrames (); log.info ("duration" + ftp / fFmpegFrameGrabber.getFrameRate () / 60); while (flag)

Tags: Files generation videos methods data objects requirements processing documents pictures documents questions Zhang you success content address that is time article type Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Microsoft Docker macOS Apple