图形文件和二进制文件的相互转换

一 背景
项目中掌机上传图片到web服务器并且在页面上展示,由于之前已采用webservice接口作为数据交互,所以必须把图片文件转换为二进制文件作为字符串随xml报文上传到主站,web服务器接收后解析。
注意点:开发语言不通(java和.net),因此图片二进制文件需要base64转换(进过转换就变成utf-8字符集),否则生成图片异常。下面无论哪种方式都可以解析
in = new ByteArrayInputStream(org.apache.axis.encoding.Base64.decode(imgStr));
in = new ByteArrayInputStream(org.apache.commons.codec.binary.Base64.decodeBase64(imgStr.getBytes("UTF-8")));

二 代码

package picture;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import cn.hexing.ami.util.Coder;

/**
* @Description xxxxx
* @author xxx
* @Copyright 2014 hexing Inc. All rights reserved
* @time:2014-7-10
* @version 1.0
*/
public class Server {

/**
* @param args
*/
public static void main(String[] args) {
saveToImgByBytes(new File("D:\\proccess1.png"), "d:\\pic.data");
String imgStr = imgToString("d:\\pic.data");
saveToImgByStr(imgStr, "d:\\pic.png");
}

/**
* 图形转换成二进制文件
* @param imgFile 图形文件
* @param sfile 二进制文件
* @return
*/
public static int saveToImgByBytes(File imgFile, String sfile){
int stateInt = 1;
FileOutputStream fos = null;
FileInputStream fis = null;

try{
fos = new FileOutputStream(new File(sfile));
fis = new FileInputStream(imgFile);

byte[] b = new byte[1024];
int nRead =0;
while((nRead = fis.read(b)) != -1) {
fos.write(b,0, nRead);
}
fos.flush();


} catch(Exception e) {
stateInt =0;
e.printStackTrace();
} finally {
try {
if(null != fos)
fos.close();
if(null != fis)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}

return stateInt;
}

/**
* 读取二进制并生成字符串
* @param byteFile
* @return
*/
public static String imgToString(String byteFile) {
ByteArrayOutputStream bos = null;
BufferedInputStream bis = null;

try{
bos = new ByteArrayOutputStream();
bis = new BufferedInputStream(new FileInputStream(byteFile));
byte[] b = new byte[1024];
int nRead = 0;
while((nRead = bis.read(b)) != -1) {
bos.write(b, 0, nRead);
}
return Coder.encryptBASE64(bos.toByteArray());
} catch(Exception e) {

} finally {
try {
if(null != bis)
bis.close();
if(null != bos)
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}

return null;
}

/**
* 生成图形
* @param imgStr 二进制字符串
* @param imgFile 图形文件
* @return
*/
public static int saveToImgByStr(String imgStr, String imgFile) {
int stateInt = 1;
InputStream in = null;
FileOutputStream fos = null;

try{
in = new ByteArrayInputStream(Coder.decryptBASE64(imgStr));
fos = new FileOutputStream(new File(imgFile));

byte[] b =new byte[1024];
int iRead =0;
while((iRead = in.read(b)) != -1) {
fos.write(b, 0, iRead);
}

fos.flush();
} catch(Exception e) {
stateInt =0;
e.printStackTrace();
} finally {
try {
if(null != in)
in.close();
if(null != fos)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}

return stateInt;
}

//----------------------------------------------------------------------------------------
public static byte[] imgByte(String txtFile) {
try{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(txtFile));
byte[] b = new byte[1024];
int nRead =0;
while((nRead = bis.read(b)) != -1) {
bos.write(b, 0, nRead);
}
return bos.toByteArray();
} catch(Exception e){

}
return null;
}

//二进制文件转换成字符串
public static int saveToImgByStr(byte[] imgStr, String imgFile) {
int stateInt =1;

try{
InputStream in = new ByteArrayInputStream(imgStr);
File file = new File(imgFile);
FileOutputStream fos = new FileOutputStream(file);

byte[] b =new byte[1024];
int nRead =0;
while((nRead = in.read(b)) != -1) {
fos.write(b,0, nRead);
}

fos.flush();
fos.close();
in.close();

}catch(Exception e) {
stateInt =0;
e.printStackTrace();
}finally{
}

return stateInt;
}

// 字符串转二进制
public static byte[] hex2byte(String str) {
if(str ==null)
return null;
str = str.trim();
int len = str.length();
if(len ==0|| len %2==1)
return null;

byte[] b =new byte[len /2];
try{
for(int i =0; i < str.length(); i +=2) {
b[i /2] = (byte) Integer
.decode("0X"+ str.substring(i, i +2)).intValue();
}
return b;
}catch(Exception e) {
return null;
}
}

// 二进制转字符串
public static String byte2hex(byte[] b) {
StringBuffer sb = new StringBuffer();
String stmp ="";
for(int n = 0; n < b.length; n++) {
stmp = Integer.toHexString(b[n] &0XFF);
if(stmp.length() ==1) {
sb.append("0"+ stmp);
}else{
sb.append(stmp);
}
}
return sb.toString();
}
}



package cn.hexing.ami.util;
import java.security.MessageDigest;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

import org.apache.axis.encoding.Base64;

/**
* @Description xx
* @author xx
* @Copyright 2014 hexing Inc. All rights reserved
* @time 2014-07-10
* @version 1.0
*/
public abstract class Coder {
public static final String KEY_SHA = "SHA";
public static final String KEY_MD5 = "MD5";

/**
* MAC算法可选以下多种算法
* HmacMD5
* HmacSHA1
* HmacSHA256
* HmacSHA384
* HmacSHA512
*/
public static final String KEY_MAC = "HmacMD5";

/**
* BASE64解密
* @param key
* @return
* @throws Exception
*/
public static byte[] decryptBASE64(String key) throws Exception {
return Base64.decode(key);
}

/**
* BASE64加密
* @param key
* @return
* @throws Exception
*/
public static String encryptBASE64(byte[] key) throws Exception {
return Base64.encode(key);
}

/**
* MD5加密
* @param data
* @return
* @throws Exception
*/
public static byte[] encryptMD5(byte[] data) throws Exception {

MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
md5.update(data);

return md5.digest();

}

/**
* SHA加密
* @param data
* @return
* @throws Exception
*/
public static byte[] encryptSHA(byte[] data) throws Exception {

MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
sha.update(data);

return sha.digest();

}

/**
* 初始化HMAC密钥
* @return
* @throws Exception
*/
public static String initMacKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);

SecretKey secretKey = keyGenerator.generateKey();
return encryptBASE64(secretKey.getEncoded());
}
}



三 web服务器端实现
方式1保存文件到数据库,一般文件不多图片很小可以用此种方式;方式2保存文件到服务器上,取文件相对路径保存到表中,推荐用第二种
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值