zoukankan      html  css  js  c++  java
  • 字符串转码 将文本转为PDF

     @Test
        public void testBasic64Code() throws Exception {
            String strdata = new String("how are you".getBytes("UTF-8"));
              BASE64Decoder decoder = new BASE64Decoder();
              byte[] decodedBytes;
              decodedBytes = decoder.decodeBuffer(strdata);
              System.out.println(decodedBytes);
            
        }

     1.转PDF test

    public class Base64ConvertPDFTest {
    
      public static void main(String[] args) {
        // TODO Auto-generated method stub
        
      
        try {
          String encodedBytes =  readFile("/home/wrightdeng/Desktop/test.txt", StandardCharsets.UTF_8);
          
          BASE64Decoder decoder = new BASE64Decoder();
          byte[] decodedBytes;
          decodedBytes = decoder.decodeBuffer(encodedBytes);
        
        File file = new File("/home/wrightdeng/Desktop/newfile.pdf");;
        FileOutputStream fop = new FileOutputStream(file);
        
        fop.write(decodedBytes);
        fop.flush();
        fop.close();
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    
        static String readFile(String path, Charset encoding) throws IOException 
          {
            byte[] encoded = Files.readAllBytes(Paths.get(path));
            return new String(encoded, encoding);
          }
    
    }

    工具类:

     ***************************************************************************/
    
    public class Base64ConverterUtils {
    
        private static final Logger LOGGER = LoggerFactory.getLogger(Base64ConverterUtils.class);
    
        public static void Base64Converter(String encodedBytes, String tempPath) {
            // BASE64Decoder decoder = new BASE64Decoder();
            
            LOGGER.info("The file temp saved in :"+tempPath);
            byte[] decodedBytes;
            try {
                decodedBytes = Base64.getDecoder().decode(encodedBytes); // decoder.decodeBuffer(encodedBytes);
                File file = new File(tempPath);
                FileOutputStream fop = new FileOutputStream(file);
                fop.write(decodedBytes);
                fop.flush();
                fop.close();
            } catch (IOException e) {
                LOGGER.error("write file to server occurred exception,the reason: "+e.getMessage());
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    thinkphp3.1.3验证码优化
    php导出数据为CSV文件DEMO
    python学习笔记十七:base64及md5编码
    linux运维笔记
    [转]如何像Python高手(Pythonista)一样编程
    用gulp清除、移动、压缩、合并、替换代码
    [蓝桥杯][2017年第八届真题]小计算器(模拟)
    [蓝桥杯][2017年第八届真题]发现环(基环树输出环)
    [蓝桥杯][2017年第八届真题]合根植物(并查集)
    省赛训练5-3(四个签到题)
  • 原文地址:https://www.cnblogs.com/lshan/p/9204491.html
Copyright © 2011-2022 走看看