在最近的项目中,遇到需求,PDM系统通过webservice传输文件实体给其他系统,同时其他系统也需要通过webservice传送实体文件给PDM系统,此外,传输的文件还需要做压缩。
现在把实现方案整理一下,希望会对有这种需求的人有所帮助。
大概方案:
系统A:实体文件->byte[]->BASE64Encoder加密得到字符串,构造xml,作为返回内容(或在调用端作为参数)->压缩xml->BASE64Encoder加密。(实体文件到字符串)
系统B:取到结果->BASE64Decoder解密->解压->得到xml->解析到xml中内容->BASE64Decoder解密得到byte[]->写入文件(字符串到实体文件)。
XML如:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<document docNum="Document000001" version="A.1" >
<file name="test1.doc">文件实体内容(加密字符串)</file>
<file name="test2.xls">文件实体内容(加密字符串)</file>
</document>
</root>
加密压缩,解密解压等方法整理在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;
}
}
分享到:
相关推荐
C#开发核心代码,用拼接XML方式,调用封装了实体类的SOAP接口
通过WebService发布服务,客户端发送文件。服务端接收
webservice4 二进制文件读取 webservice4 二进制文件读取
java调用windows系统的com组件,用jacob来处理。JACOB一个Java-COM中间件.通过这个组件你可以在Java应用程序中调用COM组件和Win32程序库。
win7环境,利用VC和gsoap实现webservice文件传输
WebService中上传文件
使用webservice实现json文件传输,包含压包解包。在jdk1.6,myeclipse10下测试成功.完整源码
通过wsdl文件就可以通过客户端调用服务端的接口,可以使用Webservice的服务了
引用的webservice 的示例文件.
ajax跨域调用webservice实现文件上传,项目使用vs2019创建,请示用vs2019以上版本工具进行打开。
用webservice作为服务器端,以流的方式实现文件的上传和下载,可以自动调用winrar进行压缩和加密,支持多线程和断点续传功能,默认是16K一个包,全部源码,已经在实际项目中应用。客户端有winform和服务两种方式的...
webservice 实现文件的传输。
WebService的WSDL文件生成Java代码工具
这个是使用java语言,来制作的webservice,有详细的使用方法和齐全的库文件,这个库文件用来替换tomcat7.0的库文件,因为里面加入了很多axis2的库文件,所以可以少走很多的弯路,直接替换就可以了
webservice的简单使用免费教程 体积小效率高 学习引用首备
C#实现基于webService 的文件传输程序,WEB服务的创建和调用,并WSE3.0、后台线程的创建模式、文件哈希校验
delphi+C#webservice上传文件
java WebService全部jar文件,很全的噢,免积分,希望大家会喜欢,谢谢
自己搞了半天才搞出来的,所以分稍微高了一点,算是对我努力的一点回报吧,谢谢大家了。
请参考:http://blog.csdn.net/dwj901125/article/details/23701063 这里有相当详尽的实现过程。