Tag Archives: java

Base64 Image Compression Example

Compress picture size

@Log4j2
public class Base64Util {

    /**
     * Compressed images within 100k
     *
     * @param base64Img
     * @return
     */
    public static String resizeImageTo100K(String base64Img) {
        try {
            BufferedImage src = base64String2BufferedImage(base64Img);
            BufferedImage output = Thumbnails.of(src).size(src.getWidth()/3, src.getHeight()/3).asBufferedImage();
            String base64 = imageToBase64(output);
            if (base64.length() - base64.length()/8 * 2 > 40000) {
                output = Thumbnails.of(output).scale(1/(base64.length()/40000)).asBufferedImage();
                base64 = imageToBase64(output);
            }
            return base64;
        } catch (Exception e) {
            return base64Img;
        }
    }


    /**
     * base64 convert to BufferedImage
     *
     * @param base64string
     * @return
     */
    public static BufferedImage base64String2BufferedImage(String base64string) {
        BufferedImage image = null;
        try {
            InputStream stream = BaseToInputStream(base64string);
            image = ImageIO.read(stream);
        } catch (IOException e) {
            log.info("");
        }
        return image;
    }

    /**
     * Base64 convert to InputStream
     *
     * @param base64string
     * @return
     */
    private static InputStream BaseToInputStream(String base64string) {
        ByteArrayInputStream stream = null;
        try {
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] bytes1 = decoder.decodeBuffer(base64string);
            stream = new ByteArrayInputStream(bytes1);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return stream;
    }

    /**
     * BufferedImage switch to base64
     *
     * @param bufferedImage
     * @return
     */
    public static String imageToBase64(BufferedImage bufferedImage) {
        Base64 encoder = new Base64();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            ImageIO.write(bufferedImage, "jpg", baos);
        } catch (IOException e) {
            log.info("");
        }
        return new String(encoder.encode((baos.toByteArray())));
    }

}

How to Solve Assert error (IDEA Package Imported)

When using the MyBatis-Plus starter project, we found that no matter which package under IDEA is imported, Assert will report an error, indicating that the suitable package is not downloaded from maven, and then we have to go to the maven repository ourselves to download the required package Junit.

Open the maven repository, https://mvnrepository.com/, and search for junit will appear many, I will generally choose the number of people using.



[Solved] nacos Startup Error: java.io.IOException…

tip: here is a brief description of the project background:

Problem Scenario: Error on startup after installing nacos

Problem description

tip: describe the project here

Nacos startup error

Cause analysis:

Nacos defaults to cluster startup. Here is a startup error in the windows stand-alone environment

Solution:

Line 26 Modify set MODE="cluster" to MODE="standalone" and the problem is solved

Mybatis Error: All elements are null [How to Solve]

Console error: your MySQL server version for the right syntax to use near ”at line 1

Reason: the database field and entity class field are inconsistent

The in the database is underlined, and the entity class uses the hump naming method.

Solution:

In the application.yml configuration file, turn on mybatis’ camel nomenclature conversion settings

The problem is solved

SpringCloud Use openFeign Multipartfile to Upload Files Error: Current request is not a multipart request

Error reporting information

feign MultipartException: Current request is not a multipart request

 

On the premise of introducing and configuring openfeign

1. Create FeignMultipartSupportConfig.class configuration file

Just copy and paste it directly

import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;

@Configuration
public class FeignMultipartSupportConfig {

    @Bean
    @Primary
    @Scope("prototype")
    public Encoder multipartFormEncoder() {
        return new SpringFormEncoder();
    }

    @Bean
    public feign.Logger.Level multipartLoggerLevel() {
        return feign.Logger.Level.FULL;
    }

}

2. Modify postmapping

    1. 1. MultipartFile type parameters use @RequestPart annotation, string parameters use annotation @RequestParam
    1. 2. Add consumes = MediaType.MULTIPART_FORM_DATA_VALUE
@FeignClient(value = "mk-other", fallback = MkOtherFeignFallBack.class)
public interface MkOtherFeign {
    @PostMapping(value = "/file/pic/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseResult picUpload(@RequestPart("file") MultipartFile file);
}

Reference link

Android Studio Cannot resolve symbol [How to Solve]

Problem:
Previously the project is normal, one day suddenly appeared red exception, the code is prompted in many places “Cannot resolve symbol xxxx” Log

Analysis:
The problem is caused by the failure of the three-party library dependency, the guide package can not be found

Solution (you can follow the following process to try):
1, Build->Clean Project and then Build->Rebuild Project to see if it is OK, not continue
2,File->Sync Project With Gradle Files to see if it’s OK, if not, continue
3,File->Invalidate Caches/Restart (check Clear Cache or Files) to see if it’s OK, or not.
4, close AS, manually enter the project to delete the .gradle and .idea files, reopen AS build to see if it is OK, not continue
5, downgrade the AS version, keep the settings and uninstall the current version, download and install the old version without problems before

The above five steps will basically solve the problem, which step is OK, just follow the order.
My problem is to the fifth step, from AS Bumblebee directly downgrade to AS4.2.1 before the problem is solved, the middle of the Arctic fox also have problems, specifically ignore.

Translated with www.DeepL.com/Translator (free version)

How to Solve the Primary Key of mybatisPlus Inserted Data is too Large Issue

Solve the problem that the primary key of mybatisplus inserted data is suddenly large

1. Enter the corresponding database in the administrator window, and then enter the following instructions:

alter table sys_user AUTO_INCREMENT=30;

​sys_user is the name of the data table to be modified, and 30 is to modify the self increment starting value to 30

In this way, the primary key value of the data table will return to normal

2. In the entity class of the project, add a @Tableid annotation to the primary key of the class

package baocms.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "sys_user")
public class User {
    @TableId(value = "id",type = IdType.AUTO)
    private Integer id;
    private String username;
    private String password;
    private String nickname;
    private String email;
    private String phone;
    private String address;
}

Eclipse start project error: Errors occurred during the build. Errors running builder ‘Integrated External Tool Builder’ on…

When eclipse starts the project, it suddenly reports: Errors occurred during the build. Errors running builder ‘Integrated External Tool Builder’ on project ‘acc-bid’. The builder launch configuration could not be found. The builder launch configuration could not be found.

The project kept building when it started, so I unchecked the validation box, which resulted in the above error

And when you look at buliders again, you bring a red cross

If you remove the fork, you won’t report an error

How to Solve c3p0 error (Cause & Solution)

Error Cause & Solution:
1. Configuration is wrong: driver=com.mysql.jdbc.Driver.
2. Wrong database connection address: url=jdbc:mysql://localhost:3306/test (test is user-defined creation, variable).
3. Wrong password or account: username=root (default is root).
4. Wrong account: password=root (password is set by the user).
5. The database is not started or not authorized to access.
6. The corresponding driver jar package mysql-connector-java-5.1.6-bin.jar has not been introduced, or the mysql-connector-java dependency has not been injected in the Maven project.