webservice传送实体文件

阅读更多
在最近的项目中,遇到需求,PDM系统通过webservice传输文件实体给其他系统,同时其他系统也需要通过webservice传送实体文件给PDM系统,此外,传输的文件还需要做压缩。
现在把实现方案整理一下,希望会对有这种需求的人有所帮助。
大概方案:
系统A:实体文件->byte[]->BASE64Encoder加密得到字符串,构造xml,作为返回内容(或在调用端作为参数)->压缩xml->BASE64Encoder加密。(实体文件到字符串)
系统B:取到结果->BASE64Decoder解密->解压->得到xml->解析到xml中内容->BASE64Decoder解密得到byte[]->写入文件(字符串到实体文件)。
XML如:


  
    文件实体内容(加密字符串)
    文件实体内容(加密字符串)
  
加密压缩,解密解压等方法整理在ZipByteUtil类中,下面附上。
解析xml代码如下(xml操作用的jdom包):
String xmlStr = call.invoke(..........);
xmlStr = ZipByteUtil.unzipAndDecode(str);//解密解压
SAXBuilder builder = new SAXBuilder();
Document newDoc = builder.build(new StringReader(xmlStr));
String path = "//file";
List list = XPath.selectNodes(newDoc, path);
System.out.println("文件总个数:"+list.size());
for (int i = 0; i < list.size(); i++) {
    Element elm1 = (Element) list.get(i);
    String fileName = elm1.getAttributeValue("name");
    String content = elm1.getText();
    FileOutputStream out = null;
    String newFile = "C:\\temp\\" + fileName;
    try {
        out = new FileOutputStream(newFile);
        try {
            //把文件内容解密得到byte[],写入文件
            byte[] bs = new BASE64Decoder().decodeBuffer(content);
    	   out.write(bs);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        if(out != null){
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}


字符串、文件进行解压解密,压缩加密的方法整理在如下类中
package ext.leanore.integration.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
 * 添加工具类,对字符串进行二进制压缩和解压操作
 * @author leanore
 *
 */
public class ZipByteUtil {
	/**
	 * 压缩字符串
	 * @author   leanore
	 * @param 需要压缩的字符串
	 * @return 压缩后的byte数组
	 ***************************************
	 */
	public static byte[] compress(String str) {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ZipOutputStream zout = new ZipOutputStream(out);
		try {
			zout.putNextEntry(new ZipEntry("0"));
			zout.write(str.getBytes());
			zout.closeEntry();
			return out.toByteArray();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			try {
				zout.close();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return new byte[0];
	}
	/**
	 * 传入压缩后的byte数组,得到压缩之前的字符串
	 * @author   leanore
	 * @param compressed 字符串压缩得到的byte数组
	 * @return
	 ***************************************
	 */
	public static String decompress(byte[] compressed) {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ByteArrayInputStream in = new ByteArrayInputStream(compressed);
		ZipInputStream zin = new ZipInputStream(in);
		String decompressed = "";
		try {
			zin.getNextEntry();
			byte[] buffer = new byte[1024];
			int offset = -1;
			while ((offset = zin.read(buffer)) != -1) {
				out.write(buffer, 0, offset);
			}
			decompressed = out.toString();
			return decompressed;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				zin.closeEntry();
				zin.close();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return "";
	}

	/**
	 * @TODO 对字符串进行压缩并做base64加密
	 * @auth leanore
	 * @return String
	 */
	public static String zipAndEncode(String source) {
		byte[] bytes = compress(source);
		return new BASE64Encoder().encode(bytes);
	}
	/**
	 * @TODO 对字符串进行解压并做base64解密
	 * @auth leanore
	 * @return String
	 */
	public static String unzipAndDecode(String source) throws IOException {
		return decompress(new BASE64Decoder().decodeBuffer(source));
	}

	/**
	 * @TODO 将传入的文件,转换为BASE64加密字符串
	 * @auth leanore
	 * @return String
	 */
	public static String encodeFileAsStr(File file) throws Exception {
		FileInputStream in = new FileInputStream(file);
		byte[] bytes = new byte[in.available()];
		in.read(bytes);
		String str = new BASE64Encoder().encode(bytes);
		return str;
	}
	
	/**
	 * @TODO 将一个文件流加密后的字符串,做解密操作,并写入指定路径
	 * @auth leanore
	 * @return File
	 */
	public static File decodeStrToFile(String content,String fullPath) {
		FileOutputStream out = null;
		File file = new File(fullPath);
		try {
			out = new FileOutputStream(file);
			try {
				byte[] bs = new BASE64Decoder().decodeBuffer(content);
				out.write(bs);
				out.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			if(out != null){
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return file;
	}
}

你可能感兴趣的:(webservice,文件传输)