标签:命令 logs add eth tco extc 刷新 set opera
1、创建zy.xml
<?xml version="1.0" encoding="UTF-8"?> <zy> <person> <name>sqx</name> <age>21</age> <img>C:/Users/qiuxiang/Desktop/codeStyle.jpg</img> </person> <person> <name>tyh</name> <age>23</age> <img>C:/Users/qiuxiang/Desktop/c1.jpeg</img> </person> <person> <name>zzj</name> <age>20</age> <img>C:/Users/qiuxiang/Desktop/c2.jpeg</img> </person> </zy>
第一题:遍历目录,将图片存入数据库(数据库中包含大对象字段)
(1)创建数据库
(2)DButil.java(数据库连接)导入mysql连接jar包
public class DButil { private static String Driver = "com.mysql.jdbc.Driver"; private static String url = "jdbc:mysql://数据库地址?useUnicode=true&characterEncoding=UTF-8"; private static String userName = "用户名"; private static String passWord = "密码"; private static Connection conn = null; public DButil(){ try { Class.forName(Driver); conn = DriverManager.getConnection(url, userName, passWord); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } public Connection getconn() { return conn; } public void closeconn() throws SQLException{ conn.close(); } }
(3)XML2DB.java
public class XML2DB { /** * 将目录中的图片存入数据库 */ public void ins(InputStream sq,File f){ DButil db = new DButil(); Connection conn = db.getconn(); try { String sql = "insert into imf(img) values(?)"; PreparedStatement st = conn.prepareStatement(sql); st.setBinaryStream(1, sq,(int)f.length()); System.out.println(sq); st.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } } public void readxml(){ try{ File f = new File("C:/Users/qiuxiang/Desktop/zy.xml"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(f); NodeList List = doc.getElementsByTagName("person"); for (int i = 0; i < List.getLength(); i++) { String img = doc.getElementsByTagName("img").item(i).getFirstChild().getNodeValue(); File fi = new File(img); FileInputStream fis = new FileInputStream(fi); ins(fis,fi); } }catch(Exception e){ } } public static void main(String[] args) { XML2DB rx = new XML2DB(); rx.readxml(); } }
第二题:遍历目录,将文件信息形成XML文件(包含文件名、创建时间、文件位置、文件大小)
(1)Data2XML.java(为了方便,这里直接读取zy.xml中的图片信息)
public class Data2XML { /** * 读取文件信息并生成xml */ List<String> filelist; public void readxml(){ filelist = new ArrayList<>(); try{ File f = new File("C:/Users/qiuxiang/Desktop/zy.xml"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(f); NodeList List = doc.getElementsByTagName("person"); for (int i = 0; i < List.getLength(); i++) { String img = doc.getElementsByTagName("img").item(i).getFirstChild().getNodeValue(); filelist.add(img); } }catch(Exception e){ } } public void createXMLByDOM(File dest) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); readxml(); try { DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.newDocument(); Element bookstore = document.createElement("fileinfo"); for (int i = 0; i < filelist.size(); i++) { File f = new File(filelist.get(i)); Element file = document.createElement("file"); file.setAttribute("id", "1"); Element fname = document.createElement("name"); fname.setTextContent(f.getName()); file.appendChild(fname); Element createtime = document.createElement("createtime"); createtime.setTextContent(new Date(f.lastModified()).toString()); file.appendChild(createtime); Element path = document.createElement("path"); path.setTextContent(f.getPath()); file.appendChild(path); Element size = document.createElement("size"); size.setTextContent(f.length()/1024+"kb"); file.appendChild(size); bookstore.appendChild(file); } document.appendChild(bookstore); TransformerFactory tff = TransformerFactory.newInstance(); Transformer tf = tff.newTransformer(); tf.setOutputProperty(OutputKeys.INDENT, "yes"); tf.transform(new DOMSource(document), new StreamResult(dest)); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { Data2XML dx = new Data2XML(); File f = new File("C:/Users/qiuxiang/Desktop/fi.xml"); dx.createXMLByDOM(f); } }
第三题:构建Webservices,并获取某XML文件中某元素内容(参数为文件名、字段名,返回元素内容)
(1)GetMsgService.java
@WebService public class GetMsgService { /** * */ static List<String> list; @WebMethod(operationName="getinfo") public @WebResult(name="info")List<String> getinfo(@WebParam(name="filepath")String filepath,@WebParam(name="itemname")String itemname){ list = new ArrayList<>(); try{ File f = new File(filepath); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(f); NodeList List = doc.getElementsByTagName(itemname); for (int i = 0; i < List.getLength(); i++) { slist(List.item(i)); } for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } }catch(Exception e){ } return list; } public static void slist(Node node){ if(node.getNodeType()==node.TEXT_NODE){ node.getTextContent().replaceAll(" ", ""); if (!node.getTextContent().trim().equals("")) { list.add(node.getTextContent()); } } NodeList lists=node.getChildNodes(); for(int i=0;i<lists.getLength();i++){ Node child=lists.item(i); slist(child); } } public static void main(String[] args) { Endpoint.publish("http://localhost:8888/sqx/GetMsgService", new GetMsgService()); System.out.println("start success"); } }
测试
1、在命令行中输入
wsimport -s D:\web\lanqiao\lanqiao\Webclient\\src -p com.itim.webservice -keep http://localhost:8888/sqx/GetMsgService?wsdl
其中D:\web\lanqiao\lanqiao\Webclient\\src 为输出路径 com.itim.webservice为包名
刷新后可见
创建test.java
public class test { /** * */ public static void main(String[] args) { GetMsgServiceService gmss = new GetMsgServiceService(); GetMsgService gi = gmss.getGetMsgServicePort(); List<String> gif = gi.getinfo("C:/Users/qiuxiang/Desktop/zy.xml", "person"); for (int i = 0; i < gif.size(); i++) { System.out.println(gif.get(i)); } } }
控制台输出结果
标签:命令 logs add eth tco extc 刷新 set opera
原文地址:http://www.cnblogs.com/quinnsun/p/work_webservice.html