一 背景
项目中掌机上传图片到web服务器并且在页面上展示,由于之前已采用webservice
接口作为数据交互,所以必须把图片文件转换为
二进制文件作为字符串随xml报文上传到主站,web服务器接收后
解析
二 代码
class="java" name="code">
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保存文件到服务器上,取文件相对路径保存到表中,推荐用第二种