存信息:
import
java.io.FileOutputStream;
import
java.util.Iterator;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
org.jdom.
*
;
import
org.jdom.output.XMLOutputter;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
filemanager.FileBase64;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
/** */
/**
* @author Develop
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
BildSpeichern
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public BildSpeichern() ...{
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void makeImageXml(DicTemp dic) throws Exception...{
// datei definition
String srcpath = "c:/baidu.gif";
String targpath = "c:/imgspeichern.xml";
String li = "Welcome";
String ni = "Test Nachricht";
// Base64 kodierung
FileBase64 fb = new FileBase64();
long start = System.currentTimeMillis();
String f = fb.fileToString(srcpath);
long stop = System.currentTimeMillis();
System.out.println("die kodierte Bild (binär) zeigt:");
System.out.println(f);
Iterator dicIt = dic.getIterator();
// Head-Informationen erstellen
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// Root erzeugen
Document xmlDoc = new Document();
Element content=new Element("Content");
xmlDoc.setRootElement(content);
//Knote erzeugen
Element wa = new Element("WebAdmin");
content.addContent(wa);
Element lb = new Element("Label");
wa.addContent(lb);
String LabelInfo = new String(li.getBytes("UTF-8"));
lb.addContent(LabelInfo);
Element wd = new Element("WebDe");
content.addContent(wd);
Element nt = new Element("Nachricht");
wd.addContent(nt);
Element bd = new Element("Bild");
wd.addContent(bd);
String NtInfo = new String(ni.getBytes("UTF-8"));
String BdInfo = new String(f.getBytes("UTF-8"));
nt.addContent(NtInfo);
bd.addContent(BdInfo);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//*
* xmlPhoto.setAttribute("name",srcpath);
xmlPhoto.setAttribute("src","http://bw-shanghai.de/baidu.gif");*/
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
XMLOutputter outp = new XMLOutputter(" ",true,"UTF-8");
outp.output(xmlDoc, new FileOutputStream(targpath));
System.out.println("XML Created!!");
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//*
* XMLOutputter outp=new XMLOutputter();
Format format=Format.getPrettyFormat();
format.setEncoding("GBK");
outp.setFormat(format);
outp.output(doc,System.out);
*
* */
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static void main(String[] args) ...{
BildSpeichern bs = new BildSpeichern();
DicTemp dic = new DicTemp();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
try...{
bs.makeImageXml(dic);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
}catch (Exception ex)...{System.out.println("Fehler");};
}
}
读出信息中的图片:
import
java.io.File;
import
java.io.IOException;
import
org.jdom.Document;
import
org.jdom.Element;
import
org.jdom.JDOMException;
import
org.jdom.input.SAXBuilder;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
filemanager.FileBase64;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
/** */
/**
* @author Develop
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
BildAuslesen
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public BildAuslesen()...{
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void InfoExtrahieren() throws JDOMException, IOException...{
String xmlfile = "c:/imgspeichern.xml";
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new File(xmlfile));
Element content=doc.getRootElement();
Element wd = content.getChild("WebDe");
String bd = wd.getChildText("Bild");
FileBase64 fb = new FileBase64();
long start = System.currentTimeMillis();
fb.stringToFile("c:/x.jpg", bd);
long stop = System.currentTimeMillis();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static void main(String[] args) ...{
BildAuslesen ba = new BildAuslesen();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
try...{
ba.InfoExtrahieren();
System.out.println("Doc Created!!");
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
}catch (Exception ex)...{System.out.println("Fehler: Content auslesen");};
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
}
说明:
其中主要用两个方法:filetostring(),和stringtofile()
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.FileOutputStream;
import
java.io.InputStream;
import
java.io.OutputStream;
import
java.security.MessageDigest;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
sun.misc.BASE64Decoder;
import
sun.misc.BASE64Encoder;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
/** */
/**
* Converts file to string with base64encoder and string to file with base64decoder.
*
* The MAX_BUFFER defines the speed of fileToString function!
* Bigger Value -> faster, more than filesize -> max. speed.
*
* Generates MD5 checksumms!
*
* MAX_BUFFER_SIZE -> used by stream operations.
*/
public
class
FileBase64
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
public static int MAX_BUFFER_SIZE = 524288; // default 512 kB
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private int maxBufferSize = MAX_BUFFER_SIZE;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private String md5sum = null;
private int fileLength = -1;
private FileManager fm = new FileManager();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//**
* Convert File to Base64 Encoded String.
*
* Generate MD5 Checksum and fileLenght.
*
* @param path Filepath.
* @param name Filename.
* @return Returns Base64 Encoded String, else null.
*/
public String fileToString(String path, String name)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return fileToString(fm.makeAbstractPath(path, name));
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//**
* Convert File to Base64 Encoded String.
*
* Generate MD5 Checksum and fileLenght.
*
* @param path Abstract Filepath.
* @return Returns Base64 Encoded String, else null.
*/
public String fileToString(String path)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (fm.isFile(path))
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
InputStream in = null;
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
String str = new String();
File f = new File(path);
in = new FileInputStream(f.getPath());
MessageDigest md = MessageDigest.getInstance("MD5");
byte buffer[] = new byte[maxBufferSize];
int read = 0;
fileLength = 0;
while ((read = in.read(buffer)) != -1)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
fileLength += read;
md.update(buffer, 0, read);
if (read < maxBufferSize) // trick 17, if buffer size < max. size :-)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
byte[] buf = new byte[read];
System.arraycopy(buffer, 0, buf, 0, read);
buffer = buf;
}
str += new BASE64Encoder().encode(buffer) + " ";
}
md5sum = new BASE64Encoder().encode(md.digest());
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
return str;
}
catch (Exception e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
e.printStackTrace();
}
finally
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (in != null) in.close();
}
catch (Exception e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
e.printStackTrace();
}
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
return null;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//**
* Convert Base64 String to File. Overwrites destination!
*
* @param path Filepath.
* @param name Filename.
* @param data Base64 Encoded String.
* @return Returns true if ok, else false.
*/
public boolean stringToFile(String path, String name, String data)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return stringToFile(fm.makeAbstractPath(path, name), data);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//**
* Convert Base64 String to File. Overwrites destination!
*
* @param path Abstract Filepath.
* @param data Base64 Encoded String.
* @return Returns true if ok, else false.
*/
public boolean stringToFile(String path, String data)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
OutputStream out = null;
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
out = new FileOutputStream(path);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] buf = new BASE64Decoder().decodeBuffer(data);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
fileLength = -1;
md5sum = null;
if (buf != null)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
out.write(buf);
fileLength = buf.length;
md5sum = new BASE64Encoder().encode(md.digest(buf));
}
else return false;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
return true;
}
catch (Exception e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
e.printStackTrace();
}
finally
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (out != null) out.close();
}
catch (Exception e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
e.printStackTrace();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
return false;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// getter and setter
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//**
* @return Returns the maxBufferSize.
*/
public int getMaxBufferSize()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return maxBufferSize;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//**
* @param maxBufferSize The maxBufferSize to set.
*/
public void setMaxBufferSize(int maxBufferSize)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (maxBufferSize > 0) this.maxBufferSize = maxBufferSize;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//**
* @return Returns the fileLength.
*/
public int getFileLength()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return fileLength;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//**
* @return Returns the md5sum.
*/
public String getMd5sum()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return md5sum;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// main for testing
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public static void main(String[] args)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
FileBase64 fb = new FileBase64();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//fb.setMaxBufferSize(FileBase64.MAX_BUFFER_SIZE*20);
//String f = fb.fileToString("C:/modem", "gnupg.doc");
long start = System.currentTimeMillis();
String f = fb.fileToString("C:/modem/JProjectNico.zip");
long stop = System.currentTimeMillis();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
double t1 = (stop - start) / 1000.0;
System.out.println("# fileToString -> " + (t1) + " s");
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//System.out.println(f);
System.out.println(f.length());
System.out.println(fb.getFileLength());
System.out.println(fb.getMd5sum());
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
start = System.currentTimeMillis();
fb.stringToFile("C:/modem/xxxxx.doc", f);
stop = System.currentTimeMillis();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
double t2 = (stop - start) / 1000.0;
System.out.println("# stringToFile -> " + (t2) + " s");
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
System.out.println(fb.getFileLength());
System.out.println(fb.getMd5sum());
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
System.out.println("## f2s = " + (fb.getFileLength() / 1048576.0) / t1 + " MB/s # s2f = " + (fb.getFileLength() / 1048576.0) / t2 + " MB/s");
}
}