文件拷贝以及base64
File inFile = new File("d:" + File.separator + "test.jpg");
File outFile = new File("e:" + File.separator + "test.jpg");
if(!outFile.getParentFile.exists()){
outFile.getParentFile().mkdirs();//输入文件的目录不存在就自动创建
}
byte data [] = new byte [1024] ;
InputStream input = new FileInputStream(inFile); //文件来源
OutputStream output = new FileOutputStream(outFile); //文件输出
int temp = 0 ; // 接收每次读取的数据
while ((temp = input.read(data)) != -1) { // 有内容读取
output.write(data, 0, temp); // 输出内容
}
input.close() ;
output.close() ;
一个简单的图片文件拷贝程序,也是文件上传的基本操作
关于接收base64格式
有java类库提供的,也有apcha提供的转换工具
// 将 s 进行 BASE64 编码
public static String getBASE64(String s) {
if (s == null) return null;
return (new sun.misc.BASE64Encoder()).encode( s.getBytes() );
}
// 将 BASE64 编码的字符串 s 进行解码
public static String getFromBASE64(String s) {
if (s == null) return null;
BASE64Decoder decoder = new BASE64Decoder();
try {
byte[] b = decoder.decodeBuffer(s);
return new String(b);
} catch (Exception e) {
return null;
}
}
或者apache的包
import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.binary.Base64;
public class Base64Util {
/**
* 将二进制数据编码为BASE64字符串
* @param binaryData
* @return
*/
public static String encode(byte[] binaryData) {
try {
return new String(Base64.encodeBase64(binaryData), "UTF-8");
} catch (UnsupportedEncodingException e) {
return null;
}
}
/**
* 将BASE64字符串恢复为二进制数据
* @param base64String
* @return
*/
public static byte[] decode(String base64String) {
try {
return Base64.decodeBase64(base64String.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
return null;
}
}
}
一个简单的小例子:
package cn.com;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class Base64Test {
public static void main(String[] args){
String strImg = GetImageStr();
//System.out.println(strImg);
GenerateImage(strImg);
System.out.println("是否转换成功:" + GenerateImage(strImg));
}
//图片转化成base64字符串
public static String GetImageStr(){//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
String imgFile = "d://test.jpg";//待处理的图片
InputStream in = null;
byte[] data = null;
//读取图片字节数组
try
{
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
//对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);//返回Base64编码过的字节数组字符串
}
//base64字符串转化成图片
public static boolean GenerateImage(String imgStr)
{ //对字节数组字符串进行Base64解码并生成图片
if (imgStr == null) //图像数据为空
return false;
BASE64Decoder decoder = new BASE64Decoder();
try
{
//Base64解码
byte[] b = decoder.decodeBuffer(imgStr);
for(int i=0;i<b.length;++i)
{
if(b[i]<0)
{//调整异常数据
b[i]+=256;
}
}
//生成jpeg图片
String imgFilePath = "d://222.jpg";//新生成的图片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
}
catch (Exception e)
{
return false;
}
}
}
浙公网安备 33010602011771号