我用base64对图片的字节流进行加密,然后存到txt文件中,对txt文件读取,为什么读不出来?
文件中能识别的都能读出来,中间的乱码就读不出来了,为什么??我是用字符流读的
<xml>
<protocol></protocol>
<version>5<version>
<content>fffffffffffffffffffffcxzczx</content>
<media><type>2</type>
<name>1.jpg</name>
<file>
�� JFIF H H � ACD Systems Digital Imaging� C
$.' ",#(7),01444'9=82<.342� C 2!!22222222222222222222222222222222222222222222222222� �`" � � B !1AQa"2q��#BR”$裂34Cbr狃%傫5�&S� � ) !1A"2Qa#3Bq� ? 妣M!啴q^喇'6ux":
c!�樠h� 牳4槴o;T%1MLL⑷昸叹弗#欽 �B3J�$舄蕹1XB.] � 駺�<�猢�硱Ч阫Gogr禖�痒*矒�敱H�枏�p爊�藡y巧�uzmX胹�*�Qd籁臗F�n您┳'��E)郣伵.遤�f)繱傮覝畫侊N�S伦敝87$}j�e�孄殬廽%裗苨��
P*坽�幋橍�殾X獱�灛复Z#`骍5�忁�⑦Fp�f懙嬵S廅�5~皱�N6呧�裮h5eL.鞄X�粡壯UP庘Υ�y怇�R僙�b脯镰N�
</file>
</media>
</xml>
文件中能识别的都能读出来,中间的乱码就读不出来了,为什么??我是用字符流读的
<xml>
<protocol></protocol>
<version>5<version>
<content>fffffffffffffffffffffcxzczx</content>
<media><type>2</type>
<name>1.jpg</name>
<file>
�� JFIF H H � ACD Systems Digital Imaging� C
$.' ",#(7),01444'9=82<.342� C 2!!22222222222222222222222222222222222222222222222222� �`" � � B !1AQa"2q��#BR”$裂34Cbr狃%傫5�&S� � ) !1A"2Qa#3Bq� ? 妣M!啴q^喇'6ux":
c!�樠h� 牳4槴o;T%1MLL⑷昸叹弗#欽 �B3J�$舄蕹1XB.] � 駺�<�猢�硱Ч阫Gogr禖�痒*矒�敱H�枏�p爊�藡y巧�uzmX胹�*�Qd籁臗F�n您┳'��E)郣伵.遤�f)繱傮覝畫侊N�S伦敝87$}j�e�孄殬廽%裗苨��
P*坽�幋橍�殾X獱�灛复Z#`骍5�忁�⑦Fp�f懙嬵S廅�5~皱�N6呧�裮h5eL.鞄X�粡壯UP庘Υ�y怇�R僙�b脯镰N�
</file>
</media>
</xml>
正常应该是 /9j/4AAQSkZJRgABAQEAYABgAAD 这样的字符串
public static void main(String[] args){
BASE64Encoder encoder = new BASE64Encoder();
try {
File f=new File("D:\\in.jpg");
if(f.exists())
{
FileInputStream fis = new FileInputStream(f);
byte[] buffer = new byte[(int)f.length()];
fis.read(buffer);
String s_imageData = encoder.encode(buffer);
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("ImageList");
Element imageID = root.addElement("imageID");
Element imageInfo = root.addElement("imageInfo");
Element imageSize = root.addElement("imageSize");
Element imageData = root.addElement("imageData");
imageID.addText("01");
imageInfo.addText("图片1");
imageSize.addText(String.valueOf(f.length()));
imageData.addText(s_imageData);
XMLWriter writer = new XMLWriter(new FileOutputStream("d:\\out.xml"));
writer.write(doc);
writer.flush();
writer.close();
}
else {
System.out.println("找不到要转换的图片文件!");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
这段代码你可以参考一下 需要dom4j
String fileName = strPath; // 源文件
String strBase64 = null;
try {
InputStream in = new FileInputStream(fileName);
// in.available()返回文件的字节长度
byte[] bytes = new byte[in.available()];
// 将文件中的内容读入到数组中
in.read(bytes);
strBase64 = new BASE64Encoder().encode(bytes); // 将字节流数组转换为字符串
in.close();
} catch (FileNotFoundException fe) {
fe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
System.out.println("str:"+strBase64);
return strBase64;
} public void base64ToIo(String strBase64,String strPath) throws IOException {
String string = strBase64;
String fileName = strPath; // 生成的新文件
try {
// 解码,然后将字节转换为文件
byte[] bytes = new BASE64Decoder().decodeBuffer(string); // 将字符串转换为byte数组
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
byte[] buffer = new byte[1024];
FileOutputStream out = new FileOutputStream(fileName);
int bytesum = 0;
int byteread = 0;
while ((byteread = in.read(buffer)) != -1) {
bytesum += byteread;
out.write(buffer, 0, byteread); // 文件写操作
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
/**
* 转化为byte数组
* @param strBase64
* @return
* @throws IOException
*/
public byte[] base64ToIo(String strBase64) throws IOException {
// 解码,然后将字节转换为文件
return new BASE64Decoder().decodeBuffer(strBase64); // 将字符串转换为byte数组
}
/**
* 将byte数组转化为字符串
* @param iobs
* @return
* @throws IOException
*/
public String ioToBase64(byte[] iobs) throws IOException {
return new BASE64Encoder().encode(iobs); // 将字节流数组转换为字符串;
}
}
测试:public class MyXMLReader {
public static void main(String arge[]) { long lasting = System.currentTimeMillis();
Base64Convert con = new Base64Convert();
try {
con.base64ToIo(con.ioToBase64("b.doc"),"c.doc");
} catch (Exception e) {
e.printStackTrace();
}
finally
{
con = null;
}
}
}
可以打开c.doc看看内容完全一致,包含图片
public chinese wholesalers
ugg void main(String arge[]) {long lasting = System.currentTimeMillis();
Base64Convert con = new Base64Convert();
try
怎么少的?
base64 编码后都是字母、数字、/的组合,怎么肯能出现乱码呢?你是否自己转码过?
File audioFile = new File(audioPath);
if(audioFile.isDirectory()){
File[] files = audioFile.listFiles();
if(files != null && files.length > 0){
osw.write("<type>1</type>\r\n"); for(File f:files){
if(f != null && f.length() > 0){
osw.write("<name>"+f.getName()+"</name>\r\n");
osw.write("<file>\r\n"); FileInputStream isr = new FileInputStream(f);
byte[] bs = new byte[this.LENGTH];
int flag = 0;
while((flag = isr.read(bs)) > 0){
if(flag == this.LENGTH){
String str = base64.encode(bs);
osw.write(str+"\r\n");
}else if(flag > 0){
byte[] tempBs = new byte[flag];
for(int i =0;i<flag;i++){
tempBs[i] = bs[i];
}
String str = new BASE64Encoder().encode(tempBs);
osw.write(str+"\r\n");
}
}
osw.write("</file>\r\n");
isr.close();
}
}
}
}
这是生成xml的程序
public class MyTest {
public static void main(String[] args) {
getStr();
}
private static void getStr(){
File file = new File("update5(2).xml");
BufferedReader br = null;
FileOutputStream fos = null;
boolean flag = false;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"utf-8"));
fos = new FileOutputStream(new File("5.jpg"));
String str = null;
String str2 = "";
while(true){
str = br.readLine();
if(str == null){
break;
}
if(str.indexOf("<file>")>=0){
flag = true;
continue;
}
if(str.indexOf("</file>")>=0){
flag = false;
break;
}
if(flag){
str2 += str;
}
}
fos.write(new BASE64Decoder().decodeBuffer(str2));
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
fos.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
这是解析的程序
<![CDATA[你的内容]]>中间试试看
<![CDATA[String audioPath = path + File.separator + "audio";
File audioFile = new File(audioPath);
if(audioFile.isDirectory()){
File[] files = audioFile.listFiles();
if(files != null && files.length > 0){
osw.write("<type>1</type>\r\n"); for(File f:files){
if(f != null && f.length() > 0){
osw.write("<name>"+f.getName()+"</name>\r\n");
osw.write("<file>\r\n"); FileInputStream isr = new FileInputStream(f);
byte[] bs = new byte[this.LENGTH];
int flag = 0;
while((flag = isr.read(bs)) > 0){
if(flag == this.LENGTH){
String str = base64.encode(bs);
osw.write(str+"\r\n");
}else if(flag > 0){
byte[] tempBs = new byte[flag];
for(int i =0;i<flag;i++){
tempBs[i] = bs[i];
}
String str = new BASE64Encoder().encode(tempBs);
osw.write(str+"\r\n");
}
}
osw.write("</file>\r\n");
isr.close();
}
}
}
}]]>
还原成图片
<![CDATA[public class MyTest {
public static void main(String[] args) {
getStr();
}
private static void getStr(){
File file = new File("update5(2).xml");
BufferedReader br = null;
FileOutputStream fos = null;
boolean flag = false;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"utf-8"));
fos = new FileOutputStream(new File("5.jpg"));
String str = null;
String str2 = "";
while(true){
str = br.readLine();
if(str == null){
break;
}
if(str.indexOf("<file>")>=0){
flag = true;
continue;
}
if(str.indexOf("</file>")>=0){
flag = false;
break;
}
if(flag){
str2 += str;
}
}
fos.write(new BASE64Decoder().decodeBuffer(str2));
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
fos.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
]]>
String audioPath = path + File.separator + "audio";
File audioFile = new File(audioPath);
if(audioFile.isDirectory()){
File[] files = audioFile.listFiles();
if(files != null && files.length > 0){
osw.write(" <type>1 </type>\r\n"); for(File f:files){
if(f != null && f.length() > 0){
osw.write(" <name>"+f.getName()+" </name>\r\n");
osw.write(" <file>\r\n"); FileInputStream isr = new FileInputStream(f);
byte[] bs = new byte[this.LENGTH]; int flag = 0;
while((flag = isr.read(bs)) > 0){
if(flag == this.LENGTH){
String str = base64.encode(bs);
osw.write(str+"\r\n");
}else if(flag > 0){
byte[] tempBs = new byte[flag];
for(int i =0;i <flag;i++){
tempBs[i] = bs[i];
}
String str = new BASE64Encoder().encode(tempBs);
osw.write(str+"\r\n");
}
}
osw.write(" </file>\r\n");
isr.close();
}
}
}
}
这个是解析的public class MyTest {
public static void main(String[] args) {
getStr();
}
private static void getStr(){
File file = new File("update5(2).xml");
BufferedReader br = null;
FileOutputStream fos = null;
boolean flag = false;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"utf-8"));
fos = new FileOutputStream(new File("5.jpg"));
String str = null;
String str2 = "";
while(true){
str = br.readLine();
if(str == null){
break;
}
if(str.indexOf("<file>")>=0){
flag = true;
continue;
}
if(str.indexOf("</file>")>=0){
flag = false;
break;
}
if(flag){
str2 += str;
}
}
fos.write(new BASE64Decoder().decodeBuffer(str2));
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
fos.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
抓狂到极点!!!!!!!!!!!!!!!!!!!!!!
不是让你把Java代码放到<![CDATA[]]>里面去!!!!!!!!!!!!!!!!!!!!!!!!!!!!!