简体   繁体   English

将具有base64格式图像的html文件转换为doc

[英]Convert html file with images in base64 format to doc

I am trying to read the file in using buffered reader. 我正在尝试使用缓冲读取器读取文件。

String totalStr = "";
while ((s = br.readLine()) != null) {
    totalStr += s + "\n";
}

When s consist of base64 image, the string is half cut in between also i'm not able to append rest of the contents of the files to totalstr. s由base64图像组成时,该字符串在中间被切成两半,我也无法将文件的其余内容附加到totalstr。

How can i handle base64 image string as it consists of special characters? 我如何处理由特殊字符组成的base64图像字符串?

First I suggest do not use readLine() method, since this method may read more black characters or extra encodings. 首先,我建议不要使用readLine()方法,因为此方法可能会读取更多黑色字符或额外的编码。

If you only want to convert a image with base64 to String: 如果仅要将具有base64的图像转换为String:

The following class includes two methods: Converts a image to Base64 String and Decode it 下列类包括两种方法:将图像转换为Base64 String并将其解码

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class ImageToBase64 {

    public static String GetImageStr(String imgFile) {
        // Convert the image file to byte array, and do Base64 encoding
        InputStream in = null;
        byte[] data = null;
        // Read Image Data Array
        try {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // Base64 Encoding
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);// Return the string encoded by Base64
    }

    public static boolean GenerateImage(String base64str, String savepath) { 
        // Decoding the string and generate the image
        if (base64str == null) 
            return false;
        System.out.println("Decoding Start...");
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Base64 Decoding
            byte[] b = decoder.decodeBuffer(base64str);
            System.out.println("Decoding End...");
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            System.out.println("Start Generating Image");
            // Gen the jpeg image
            OutputStream out = new FileOutputStream(savepath);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static void main(String[] args) {
        String image  = GetImageStr("C:/Users/Perspectivar/Downloads/001.jpg");

        System.out.println(image);
        GenerateImage(image,"C:/Users/Perspectivar/Desktop/SYZ01.jpg");
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM