1. 程式人生 > >從客服端傳送XML文件,服務端接收並存儲到資料庫對應的表單中練習題

從客服端傳送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();

                            //獲得檔案字尾名的第一個索引

                            int index = fileName.lastIndexOf(".")+1;

                            //擷取檔名獲得字尾名

                            String postfixName = fileName.substring(index);

                            setPostfix.add(postfixName);

                     }

              }

              return setPostfix;

       }

       /**

        * 通過所有後綴名的集合獲取到字尾名的數量

        * @param file

        * @return

        */

       public static int getPostfixCount(File file) {

              return getPostfixSet(file).size();

       }

       /**

        * 根據字尾名獲得所有同字尾名的檔案物件集合

        * @param file

        * @param postfixName

        * @return

        */

       public static List<File> getPostfixFile(File file, String postfixName) {

              //建立儲存檔案的集合

              List<File> fileList = new ArrayList<File>();

              if(!file.exists()) {

                     return fileList;

              }

              if(file.isFile()) {

                     if(postfixName.equals(getPostfixName(file))) {

                            fileList.add(file);

                     }

                     return fileList;

              }

              File[] files = file.listFiles();

              for (File f : files) {

                     if(f.isDirectory()) {

                            fileList.addAll(getPostfixFile(f, postfixName));

                     }else {

                            if(postfixName.equals(getPostfixName(f))) {

                                   fileList.add(f);

                            }

                     }

              }

              return fileList;

       }

       /**

        * 通過檔案物件返回檔案的字尾名

        * @param file 通過檔案物件返回檔案的字尾名

        * @return

        */

       public static String getPostfixName(File file) {

              String postfixName = null;

              if(file.isFile() && file.exists()) {

                     String str = file.getName();

                     postfixName = str.substring(str.lastIndexOf(".")+1);

              } else {

                     System.out.println("請檢查你的檔案是否有誤");

              }

              return postfixName;

       }

       /**

        * 獲得file檔案內指定字尾名的所有檔案物件的數量

        * @param file

        * @param postfixName

        * @return

        */

       public static int getPostfixFileCount(File file, String postfixName) {

              return getPostfixFile(file,postfixName).size();

       }

       /**

        * 獲得檔案的上級檔案路徑

        * @param file

        * @return

        */

       public static String getParent(File file) {

              String str =  file.getAbsolutePath().substring(0,file.getAbsolutePath().lastIndexOf("\\")+1);

              return str.replace("\\", "/");

       }

       /**

        * 獲得檔案的最後修改時間,並按指定的格式返回字串

        * @param file

        * @return

        */

       public static String getLastModified(File file) {

              long time = file.lastModified();

              Date date = new Date(time);

              SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

              return sdf.format(date);

       }

       /**

        * 返回去掉字尾名的檔名

        * @param file

        * @return

        */

       public static String getFileName(File file) {

              String name = file.getName();

              int index = name.lastIndexOf(".");

              if(file.isFile() && index > 0) {

              return name.substring(0,name.lastIndexOf("."));

              } else {

                     return "";

              }

       }

}