最近研究 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);
3、函数,里面提供两种方法
//将图片转成字节流
?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); //画图
//? end 这断方法 提供测试用 可直接将图片画出
?
//第一种方法,此方法不对图片进行任何改动
??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);//服务端接收到加密串后进行解密