Java determines the upload file size, MultipartFile, File

Reference materials:



Body:

    /**
           * Determine the file size
     *
     * @param len
           * File length
     * @param size
           * Restricted size
     * @param unit
           * Restricted units (B, K, M, G)
     * @return
     */
    public static boolean checkFileSize(Long len, int size, String unit) {
//        long len = file.length();
        double fileSize = 0;
        if ("B".equals(unit.toUpperCase())) {
            fileSize = (double) len;
        } else if ("K".equals(unit.toUpperCase())) {
            fileSize = (double) len / 1024;
        } else if ("M".equals(unit.toUpperCase())) {
            fileSize = (double) len / 1048576;
        } else if ("G".equals(unit.toUpperCase())) {
            fileSize = (double) len / 1073741824;
        }
        if (fileSize > size) {
            return false;
        }
        return true;
    }

Quote:

Returns: boolean (boolean)

// Upload file format
MultipartFile multipartFile = null;
 // file is no larger than 100M
FileUtil.checkFileSize( multipartFile.getSize(),100,"M");

 // Upload file format
File file = null;
 // file is no larger than 100M
FileUtil.checkFileSize( file.length(),100,"M")

 

Intelligent Recommendation

Struts2 upload file, JavaScript determines file size

Project requirements: Use Ext's FormPanel to define an Ext.ux.form.FileUploadField in the formpanel (an upload component provided by ExtJS officially). The server uses commons-fileupload.jar combined ...

Multipartfile to File in the upload file

When the file is uploaded, the received file format is MultipartFile, but it needs the file in the File format in many scenarios, so you need to turn MultipartFile to File, as a note record my gain. I...

Java MultipartFile upload file / upload picture

1 Judgment the file suffix / picture suffix / photo suffix and other tools to allow upload files 2 upload interface Summarize the upload file MultipartFile interface Springboot provides us withMultipa...

[Java development] example of file upload using MultipartFile

Why can't 80% of programmers be architects? >>>   This is an example of file upload. The backend uses Spring MVC's MultipartFile object to receive the data POSTed from the page. The...

Java MultipartFile implementation file upload (1)

Upload to local or file servers using MultipartFile Direct code First, upload to the server (Controller layer) Second, the service layer Third, the file upload implementation class Store the uploaded ...

More Recommendation

Java MultipartFile implements file upload and watermark (2)

Prevent it from being lost ~~ Welcome everyone to send a message In the previous one, we have implemented the upload of the file, then if we are uploaded to the picture, we have to add the watermark i...

Java upload file Multipartfile convert to JSON string

Accept the JSON upload interface Use a tool class...

In the client determines upload file size (not supported opera)

Reference to write client determines online upload file size, opera can not support        Reproduced in: https: //www.cnblogs.com/chentao5211314/archive/2011/06/21/2085929.html...

JS determines the type and size of the upload file

Why can't 80% of the code farmers can't do architects? >>>   The type and size of the upload file can be judged by JS, and there is a good example in this article.  Code: Artic...

MultipartFile (File Upload)--CommonsMultipartResolver

In SpringMVC, file uploads are implemented through the MultipartResolver. So, if you want to upload a file, just register the corresponding MultipartResolver in spring-mvc.xml. Spring_mvc.xml configur...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top