最近研究 xfire 进行文件传输,现提供BASE64 加密 byte流传输方法:
https 及apache 调用方式 注意服务端xalan.jar包
客户端:
1、byte[] buffer = image2Bytes("D:/Koala.jpg"); //读文件,转换为byte流
2、String encodedFileString = Base64.encode(buffer);//对数据流进行加密,需要注意的是此BASE64加密方法为import org.codehaus.xfire.util.Base64;包提供
buffer = Base64.decode(encodedFileString);
//将图片转成字节流
public static byte[] image2Bytes(String imagePath) throws FileNotFoundException {
// start
// BufferedImage input = ImageIO.read(file1);
// Image scaledImage = input.getScaledInstance(256,256,Image.SCALE_DEFAULT);
// BufferedImage output = new BufferedImage(256,BufferedImage.TYPE_INT_BGR);
// output.createGraphics().drawImage(scaledImage,null); //画图
//第一种方法,此方法不对图片进行任何改动
File image = new File( imagePath);//读取文件路径,存文件
InputStream is = new FileInputStream(image);
byte[] buff = new byte[(int)image.length()];//文件大小 System.out.println("image.length()=="+image.length());
//第二种方法 对图片进行格式化
// ImageIcon ima = new ImageIcon(imagePath);
// int outputWidth = ima.getimage().getWidth(null);
// if (outputWidth < 1) {
// throw new IllegalArgumentException("output image width " + outputWidth + " is out of range");
// }
// int outputHeight = ima.getimage().getHeight(null);
// if (outputHeight < 1) {
// throw new IllegalArgumentException("output image height " + outputHeight + " is out of range");
// }
// BufferedImage bu = new BufferedImage(ima.getimage().getWidth(null),ima.getimage().getHeight(null),BufferedImage.TYPE_INT_RGB);
// ByteArrayOutputStream imagestream = new ByteArrayOutputStream();
try {
//把这个jpg图像写到这个流中去,这里可以转变图片的编码格式
//boolean resultWrite = ImageIO.write(bu,"png",imagestream);
is.read(buff);
is.close();
} catch (IOException e) {
e.printstacktrace();
}
//byte[] tagInfo = imagestream.toByteArray();
byte[] tagInfo = buff;
return tagInfo;
}
服务端
1、buffer = Base64.decode(encodedFileString);//服务端接收到加密串后进行解密
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。