xml存储图片

private String readImage() {  
    BufferedInputStream bis = null;  
    byte[] bytes = null;  
    try {  
        try {  
            bis = new BufferedInputStream(new FileInputStream(ix));  
            bytes = new byte[bis.available()];  
            bis.read(bytes);  
        } finally {  
            if (bis != null) {  
                bis.close();  
            }  
 
        }  
    } catch (FileNotFoundException e) {  
        e.printStackTrace();  
    } catch (IOException e) {  
        e.printStackTrace();  
    }  
    return new BASE64Encoder().encodeBuffer(bytes);  


private String readImage() {
BufferedInputStream bis = null;
byte[] bytes = null;
try {
try {
bis = new BufferedInputStream(new FileInputStream(ix));
bytes = new byte[bis.available()];
bis.read(bytes);
} finally {
if (bis != null) {
bis.close();
}

}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return new BASE64Encoder().encodeBuffer(bytes);
}

Java代码
public void imageToXml() {  
        String xml = "" + "<image>" + "<name>" + ix + "</name>" + "<content>" 
                + readImage() + "</content></image>";  
        try {  
            XMLHelper.write(XMLHelper.parseText(xml), ix + ".xml");  
        } catch (DocumentException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    } 

public void imageToXml() {
String xml = "" + "<image>" + "<name>" + ix + "</name>" + "<content>"
+ readImage() + "</content></image>";
try {
XMLHelper.write(XMLHelper.parseText(xml), ix + ".xml");
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

从xml读取图片:

Java代码
public void xmlToImage(String rename) {  
        Document d;  
        String name = null;  
        String content = null;  
        try {  
            d = XMLHelper.parse(ix);  
            name = XMLHelper.getNodeValue(d, "/image/name");  
            content = XMLHelper.getNodeValue(d, "/image/content");  
            saveImage(rename.equals("") ? name : rename, content);  
        } catch (DocumentException e) {  
            e.printStackTrace();  
        }  
    }  
 
    public void xmlToImage() {  
        xmlToImage("");  
    }  
 
    private void saveImage(String filename, String content) {  
        try {  
            DataOutputStream dos = null;  
            try {  
                byte[] bs = new BASE64Decoder().decodeBuffer(content);  
                dos = new DataOutputStream(new BufferedOutputStream(  
                        new FileOutputStream(filename)));  
                dos.write(bs);  
            } finally {  
                dos.close();  
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    } 

你可能感兴趣的:(xml,dos)