MultipartFile轉base64


MultipartFile首先轉換成file然后再講file轉換成base64格式
 public String getBase64String(MultipartFile multiPartFile) throws IOException {
        String baseStr = null;

        //把MultipartFile轉化為File
        File file = new File(multiPartFile.getOriginalFilename());
        FileUtils.copyInputStreamToFile(multiPartFile.getInputStream(), file);

        try {
        //file轉base64 FileInputStream inputStream
= new FileInputStream(file); byte[] buffer = new byte[(int) file.length()]; inputStream.read(buffer); inputStream.close(); baseStr = new BASE64Encoder().encode(buffer); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //刪除臨時文件 if (file.exists()) { file.delete(); } baseStr = baseStr.replaceAll("\r\n", ""); return baseStr; }

 

 

MultipartFile直接轉base64
public String getBase64String(MultipartFile multiPartFile) throws IOException {
        String baseStr = null;
        BASE64Encoder encoder = new BASE64Encoder();
        baseStr= encoder.encode(multiPartFile.getBytes());
        baseStr = baseStr.replaceAll("\r\n", "");
        return baseStr;
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM