首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

开发顶用到过的UTIL类

2012-08-21 
开发中用到过的UTIL类1,加密的Util类????2,解析xml里面的util类????3,ftp上传下载的util类???4,sftp协议的

开发中用到过的UTIL类

1,加密的Util类

?

?

?

?

2,解析xml里面的util类

?

?

?

?

3,ftp上传下载的util类

?

?

?

4,sftp协议的上传下载

?

?

?

?

?

5,生成校验码的util类

?

?

?

?

8,ftp ,sftp上传下载的开关控制类

?

package com.zte.aspportal.comm.util;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.List;import org.apache.poi.POIXMLDocument;import org.apache.poi.POIXMLTextExtractor;import org.apache.poi.hwpf.HWPFDocument;import org.apache.poi.hwpf.model.PicturesTable;import org.apache.poi.hwpf.usermodel.CharacterRun;import org.apache.poi.hwpf.usermodel.Picture;import org.apache.poi.hwpf.usermodel.Range;import org.apache.poi.openxml4j.opc.OPCPackage;import org.apache.poi.xwpf.extractor.XWPFWordExtractor;import org.apache.poi.xwpf.usermodel.XWPFDocument;import org.apache.poi.xwpf.usermodel.XWPFPictureData;import org.apache.struts2.ServletActionContext;import com.zte.aspportal.capability.bean.CapaBilityMethod;import com.zte.aspportal.capability.dao.impl.CapaBilityMethodDaoImpl;import com.zte.toolstore.tools.InitConstants;public class WordParser {/** * 读取word2003文本 */  public String extractMSWordText(File file) {    try {    InputStream input = new FileInputStream(file);HWPFDocument msWord=new HWPFDocument(input);Range range = msWord.getRange();String msWordText = range.text();return msWordText;} catch (IOException e) {e.printStackTrace();}return null;    }/** * 读取word2003图片 */  public void extractImagesIntoDirectory(String directory,File file) throws IOException {    InputStream input = new FileInputStream(file);    HWPFDocument msWord=new HWPFDocument(input);        PicturesTable pTable = msWord.getPicturesTable();        int numCharacterRuns = msWord.getRange().numCharacterRuns();        for (int i = 0; i < numCharacterRuns; i++) {            CharacterRun characterRun = msWord.getRange().getCharacterRun(i);            if (pTable.hasPicture(characterRun)) {                System.out.println("have picture!");                Picture pic = pTable.extractPicture(characterRun, false);                String fileName = pic.suggestFullFileName();                OutputStream out = new FileOutputStream(new File(directory                        + File.separator + fileName));                pic.writeImageContent(out);            }        }    }/*** 读取word2007文本*/public String extractContent(File document){String contents="";String wordDocxPath=document.toString();try {OPCPackage opcPackage=POIXMLDocument.openPackage(wordDocxPath);XWPFDocument xwpfd=new XWPFDocument(opcPackage);POIXMLTextExtractor ex = new XWPFWordExtractor(xwpfd);//读取文字contents= ex.getText().trim();} catch (IOException e) {e.printStackTrace();}return contents;}/*** 读取word2007图片*/public String[] extractImage(File document,String imgPath){//document获取word路径,imgPath图片保存路径String wordDocxPath = document.toString();String[] photopath = null;try {//载入文档OPCPackage opcPackage= POIXMLDocument.openPackage(wordDocxPath);XWPFDocument xwpfd = new XWPFDocument(opcPackage);//建立图片文件目录File file = new File(imgPath);if(!file.exists()){file.mkdir();}//获取所有图片List<XWPFPictureData> piclist=xwpfd.getAllPictures();photopath = new String[piclist.size()];for (int i = 0; i < piclist.size(); i++) {XWPFPictureData pic = (XWPFPictureData)piclist.get(i);Integer index = Integer.parseInt(pic.getFileName().substring(5,pic.getFileName().lastIndexOf(".")));//确定图片顺序//获取图片数据流byte[] picbyte = pic.getData();//将图片写入本地文件String path ="/uploadfiles/cap"+File.separator+UUIDGenerate.getUUID()+".jpg";FileOutputStream fos = new FileOutputStream(imgPath+path);fos.write(picbyte);photopath[index-1]=path;}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return photopath;}/** * 测试 */public static void main(String[] args) {    WordParser word = new WordParser();    File file = new File("d://word/sample1.docx");    String[] photoPath = word.extractImage(file,"D:\\workSpace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp1\\wtpwebapps\\aspportal");    String wordText=word.extractContent(file);    System.out.println(wordText);    wordText = wordText.replaceAll("\n", "<br/>");    wordText = wordText.replaceAll("\t", "&nbsp;");    String[] wordArray = wordText.split("--------");    Integer methodSize = wordArray.length/6;    for(int i=0;i<methodSize;i++)    {    CapaBilityMethod method = new CapaBilityMethod();    method.setSrvtypeid("2");    method.setMethodname(wordArray[6*i+1].replaceAll("<br/>", ""));    method.setWebdesc(wordArray[6*i+2].replaceFirst("<br/>", ""));    method.setParams(wordArray[6*i+3].replaceFirst("<br/>", ""));    method.setAuth(wordArray[6*i+4].replaceFirst("<br/>", ""));    method.setSample(photoPath[i]);    method.setErrors(wordArray[6*i+6].replaceFirst("<br/>", ""));    CapaBilityMethodDaoImpl methodDao = new CapaBilityMethodDaoImpl();    methodDao.addMethod(method);    }}}

?

热点排行