---------------------------------------从客服端发送XML文档,服务端接收并存储到数据库对应的表单中练习题----------------------------------------
*<?xml version="1.0"?> * *<fileset fileNum="12122" dirNum="78812" extendNameTypeNum="772"> * <fileGroup extendType="bmp" fileNum="233"> * <file length="212" lastModified="2008-12-12 21:12:32" readonly="true" parent="c:/a/d/b/d">mypic</file> * <file length="2123" lastModified="2008-12-12 21:12:32" readonly="false" parent="c:/a/d/b/c">yourpic</file> * ... * </fileGroup> * ... *</fileset>
|
题目:通过客服端讲文件夹中所有文件的信息以xml文档传入服务端,再从服务端解析到数据库各个表单中
花了一天完成的结果大家不要笑我,看是简单其实很费时间。
客服端
客服端需要使用文件的操作和IO流、TCP网络编程、多线程的技术。
第一步创建Customer类
Customer类是将文件夹中所有的文件信息写入到项目的src中,存储在xml文档中。代码如下:
package cn.tx.customer.file;
import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.TreeSet;
import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter;
/** * 客服端中将文件中所有文件的信息写入xml文档中的类 * @author 李昆鹏 * */ public class Customer {
/** * 给出一个文件对象将文件对象中的所有子文件信息写入xml中 * @param file */ public static File writerXML(File file) { File returnFile = new File("src/XMLFile.xml"); //创建xml文档对象 Document doc = DocumentHelper.createDocument(); Element root = doc.addElement("feliSet"); //获得文件内的总文件数量并且添加给feliset元素的属性 root.addAttribute("fileNum",getFileCount(file)+""); //获得文件内的总文件夹数量并且添加给feliset元素的属性 root.addAttribute("dirNum", String.valueOf(getDirCount(file))); //添加文件后缀名的数量属性 root.addAttribute("extendNameTypeNum", getPostfixCount(file)+""); //获得所有后缀名的集合 TreeSet<String> postfixSet = getPostfixSet(file); for (String postfix : postfixSet) { Element fileGroup = root.addElement("fileGroup") .addAttribute("extendType", postfix) .addAttribute("fileNum", Integer.toString(getPostfixFileCount(file, postfix))); //获得指定后缀名的文件集合 List<File> fileList = getPostfixFile(file, postfix); for (File f : fileList) { fileGroup.addElement("file") .addAttribute("length", Long.toString(f.length())) .addAttribute("lastModified", getLastModified(f)) .addAttribute("redonly", isReadonly(f)) .addAttribute("parent", getParent(f)) .setText(getFileName(f)); } } FileOutputStream out = null; XMLWriter xw = null; try { //创建字节输出流 out = new FileOutputStream(returnFile); //创建写入xml文件对象 xw = new XMLWriter(out,OutputFormat.createPrettyPrint()); xw.write(doc); System.out.println("xml文件创建成功!"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { if(xw != null) { xw.close(); } if(out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } return returnFile; }
/** * 返回文件是否是仅读的 * @param file * @return */ public static String isReadonly(File file) { boolean isReadonly = file.canRead()&&!file.canWrite(); return Boolean.toString(isReadonly); } /** * 返回一个有多少文件 * @param file 要查询的文件对象 * @return 返回file中所有的文件数量 */ public static int getFileCount(File file) { //记录file中所有文件的变量 int fileCount = 0; if(file.isFile() || !file.exists()) { return 0; } //获得file中所有的文件 File[] files = file.listFiles(); //遍历file中所有的文件 for (File f : files) { if(f.isDirectory()) { fileCount +=getFileCount(f); } else { fileCount++; } } return fileCount; }
/** * * @param file 查询指定文件对象下移动有多少目录 * @return 返回file下所有目录的数量 */ public static int getDirCount(File file) { //记录file中所有文件目录的变量 int dirCount = 0; //记录file中所有文件目录的变量 if(file.isFile() || !file.exists()) { return 0; } //获得file中所有的文件 File[] files = file.listFiles(); //遍历file中所有的文件 for (File f : files) { if(f.isDirectory()) { dirCount++; dirCount+=getDirCount(f); } } return dirCount; }
/** * 获得文件内所有文件的后缀名 * @param file * @return 返回文件中所有文件后缀名的集合 */ public static TreeSet<String> getPostfixSet(File file) { //建立存储后缀名的集合 TreeSet<String> setPostfix = new TreeSet<String>(); if(file.isFile()) { //获得文件名包含后缀名 String fileName = file.getName(); //获得文件后缀名的第一个索引 int index = fileName.lastIndexOf(".")+1; //截取文件名获得后缀名 String postfixName = fileName.substring(index); setPostfix.add(postfixName); return setPostfix; } if(!file.exists()) { return setPostfix; } File[] files = file.listFiles(); for (File f : files) { if(f.isDirectory()) { setPostfix.addAll(getPostfixSet(f)); }else { //获得文件名包含后缀名 String fileName = f.getName(); //获得文件后缀名的第一个索引
|