/**xml * String类型转为xml */ SAXReader saxReader = new SAXReader(); org.dom4j.Document document; document = saxReader.read(new ByteArrayInputStream(xml.getBytes())); org.dom4j.Element incomingForm = document.getRootElement(); /** * 创建XML文件 */ OutputFormat format= OutputFormat.createPrettyPrint();//美化格式 format.setEncoding("UTF-8");//设置字符编码 XMLWriter write = new XMLWriter(new FileWriter(new File("src/createhouse.xml")),format); write.write(incomingForm); write.flush(); write.close(); System.out.println("创建createhouse.xml完毕!");
服务端接收string类型 生成xml文件
服务Element housesub1=item.getChild("housePictureslist").getChild("housePicture").getChild("housePicInfo"); String housePic=housesub1.getText(); byte[] imgByte = hex2byte(housePic); InputStream in = new ByteArrayInputStream(imgByte); byte[] b = new byte[1024]; FileOutputStream fos=new FileOutputStream("Webroot/upload/pictures/house/"+housePicTimeName+".jpg"); int nRead1 = 0; while( ( nRead1 = in.read(b) ) != -1 ){ fos.write( b,nRead1 ); } fos.close();
服务端读取生成的xml里面图片信息 生成图片
File file=new File("Webroot/upload/pictures/house/" + housePic.getPicTimeName()); System.out.println(file+"图片路径"); if(file.exists() && file.isFile()) file.delete();
housePic是根据某个ID查出的那条图片信息
删除图片
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; } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。