即日起在codingBlog上分享您的技术经验即可获得积分,积分可兑换现金哦。

Java语言序列化和反序列化超强工具类(包含tif图片和其他格式互转)

编程语言 qq_22260641 16℃ 0评论
import java.awt.image.RenderedImage;
import java.awt.image.renderable.ParameterBlock;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import javax.media.jai.InterpolationNearest;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;

import org.apache.log4j.Logger;

import com.sun.media.jai.codec.ByteArraySeekableStream;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.ImageEncoder;
import com.sun.media.jai.codec.JPEGEncodeParam;
import com.sun.media.jai.codec.SeekableStream;
import com.sun.media.jai.codec.TIFFDirectory;

/**
 * @author 
 *
 */
public class IdepUtil {
 private static Logger logger = Logger.getLogger(IdepUtil.class);
 static final int BUFFER = 2048;
 public IdepUtil(){}
 public static String serializer(Object ob) throws Exception{//图片序列化
  ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
  objectOutputStream.writeObject(ob);  
  String serStr = byteArrayOutputStream.toString("ISO-8859-1");
  serStr = java.net.URLEncoder.encode(serStr, "UTF-8");
  
  objectOutputStream.close();
  byteArrayOutputStream.close();
  return serStr;
 }
 

 public static Object unSerializer(String xml) throws Exception{//反序列化
  String redStr = java.net.URLDecoder.decode(xml, "UTF-8");
  ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(redStr.getBytes("ISO-8859-1"));
  ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream); 
  Object ob = objectInputStream.readObject(); 
  objectInputStream.close();
  byteArrayInputStream.close();
  return ob;
 }
 
 @SuppressWarnings("unchecked")
 public static Map zipSerializer(String zipPath) throws Exception{//压缩文件序列化
  File file = new File(zipPath);
  ZipFile zip = new ZipFile(file); 
  Enumeration entries = (Enumeration)zip.entries(); 
  Map map = new HashMap();
  while(entries.hasMoreElements()){
   ZipEntry entry = entries.nextElement();  
   String entryName = entry.getName();
   //将名称统一化
   String newStr = entryName.replaceAll("^(0+)", "");
   String regEx="[^0-9]";   
   Pattern p = Pattern.compile(regEx);   
   Matcher m = p.matcher(newStr);
   String finName = m.replaceAll("").trim()+".tif";
   
   InputStream inputStream = zip.getInputStream(entry);
   
   ByteArrayOutputStream out=new ByteArrayOutputStream();
         byte[] buffer=new byte[1024*4];
         int n=0;
         while ( (n=inputStream.read(buffer)) !=-1) {
             out.write(buffer,0,n);
         }
//         byte[] b = out.toByteArray();
         byte[] b = tifToJpg(out.toByteArray());//可以换tiffToPng这个方法
   
//   String s = IdepUtil.serializer(b);
   String s = byte2hex(b);
   
   map.put(finName, s);
   out.close();
   inputStream.close();
//   System.out.println(s);
  }
  return map;
 }
 
 /** tif格式压缩转换为jpg格式压缩
  * @param zipPath
  * @return
  * @throws Exception
  */
 public static byte[] zipTifToJpg(String zipPath) throws Exception{
  File file = new File(zipPath);
  ZipFile zip = new ZipFile(file); 
  Enumeration entries = (Enumeration)zip.entries(); 
  List list = new ArrayList();
  while(entries.hasMoreElements()){
   ZipEntry entry = entries.nextElement();  
   String entryName = entry.getName();
   System.out.println("entryName===="+entryName);//本地测试时添加,记得删除
   InputStream inputStream = zip.getInputStream(entry);
   
   ByteArrayOutputStream out=new ByteArrayOutputStream();
         byte[] buffer=new byte[1024*4];
         int n=0;
         while ( (n=inputStream.read(buffer)) !=-1) {
             out.write(buffer,0,n);
         }
//         byte[] b = out.toByteArray();
         out.flush();
         byte[] b = tiffToPng(out.toByteArray());
         list.add(b);
   out.close();
   inputStream.close();
//   System.out.println(s);
  }
  
  return toZip(list);
 }
  /**
   * 打zip包
  * @param fileName
  * @param FilePath
  * @return
  * @throws IOException 
  */
 public static byte[] toZip(List list) throws IOException {
  logger.info("开始将tif压缩成zip");
   ByteArrayOutputStream out=new ByteArrayOutputStream();
   ZipOutputStream zo = new ZipOutputStream(out);
   byte[] b = null;
   if(list!=null&&list.size()>0){
    for(int i=0; i


转载请注明:CodingBlog » Java语言序列化和反序列化超强工具类(包含tif图片和其他格式互转)

喜欢 (0)or分享 (0)
发表我的评论
取消评论

*

表情