IO流(copy文件,流转成PDF,流编码)

来源:互联网 发布:广告联盟评测源码 编辑:程序博客网 时间:2024/06/01 09:45
package com.xiaomar.bis.utils;import java.io.BufferedReader;import java.io.ByteArrayOutputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import org.apache.commons.codec.binary.Base64;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.itextpdf.text.Document;import com.itextpdf.text.pdf.PdfStream;import com.itextpdf.text.pdf.PdfWriter;public class FileUtils {    private static final Logger logger = LoggerFactory.getLogger(FileUtils.class);    /**     *copy文件     */ public static void rwFile(InputStream in){        FileWriter fw = null;        BufferedReader br = null;        try {            fw = new FileWriter("A:\\text.txt", true);            br = new BufferedReader(new InputStreamReader(in,"utf-8"));            String line = null;            while ((line = br.readLine()) != null) {                logger.info("文件内容.line={}" + line);                fw.write(line);                fw.flush();            }            br.close();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } finally {            if (fw != null) {                try {                    fw.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }      /**    * 流转成PDF    */ public static void getPDF(InputStream in){        try {            FileOutputStream fo = new FileOutputStream("A:\\fx.pdf");            ByteArrayOutputStream baos = new ByteArrayOutputStream();            for(int i;(i=in.read())!=-1;){                baos.write(i);            }            baos.flush();            Document doc = new Document();            PdfStream pdfStream=new PdfStream(baos.toByteArray());            PdfWriter pw =PdfWriter.getInstance(doc, fo);            pdfStream.toPdf(pw,fo);            logger.info("doc内容.doc={}",doc.newPage());            pw.flush();            baos.close();            pw.close();            fo.close();            in.close();        } catch (Exception e) {            e.printStackTrace();        }     }  /**    *流编码    */ public static byte[] encrypt(InputStream in) throws Exception {        ByteArrayOutputStream bos = new ByteArrayOutputStream();          byte[] buffer = new byte[1024*1024];           int r;           while ((r = in.read(buffer)) > 0) {               bos.write(buffer, 0, r);               bos.flush();        }          byte[] outBytes = bos.toByteArray();        in.close();         bos.close();        return new Base64().encode(outBytes);      }         /**        * 流解码      *        */         public static byte[] decrypt(InputStream in) throws Exception {             ByteArrayOutputStream bos = new ByteArrayOutputStream();            byte[] buffer = new byte[1024];             int r;             while ((r = in.read(buffer)) > 0) {                 bos.write(buffer, 0, r);               bos.flush();          }            byte[] outBytes = bos.toByteArray();          in.close();           bos.close();           return new Base64().encode(outBytes);      }        }   还有几个常用的方法:

使用org.apache.commons.io.IOUtils;jar包下的方法

InputStream in = getStreamBody(exchange);

1.流转成字节====》    byte[] bytes = IOUtils.toByteArray(InputStream);

字节转成流=====》ByteArrayInputStream in = new ByteArrayInputStream(bytes);

URI   uri   /byte[]  bytes  /InputStream  in  /Reader reader

2.流转成String===》 IOUtils.toString(uri  / bytes  / in / reader );

String 转成流=====》IOUtils.toInputStream(String);




原创粉丝点击