近期项目需求:1.要用到各种文件上传,下载。

2.并对文件进行搜索。

3.仅仅要文件里包括有搜索的内容,所有显示出来。

今天正好有时间整理一下,方便以后阅读,及对须要用到的朋友提供微薄之力。首先在实现文件上传时,使用的struts2自带的文件上传功能,通过流的方式将文件保存,在下载的时候通过流的方式写出就可以。这个实现起来不是非常难,主要是对各种文件内容的提取比較麻烦,比方word,excel,pdf等文件,不能使用普通的BufferedReader,BufferedWriter等流的方式读写提取,由于这些文件的格式不是普通的文本,他们有自定义的格式,必须使用他们自己提供的jar包进行操作,Word,Excel使用的是Apache提供的poi进行操作,当然在操作的过程中要注意一些使用的方法,比方Word,Excel有不同的版本号,操作的方式也不同,这里会出现非常多问题,在上一篇中我整理了一些,我在操作过程出现的问题,并提供了解决方式,还有提供了本人操作这些文件的源代码,下载就可以使用。

一下是我在操作过程用到的具体信息:

struts.xml配置:

<action name="upload" class="lucenesAction" method="upload">

<!-- 此处能够限制上传文件类型

<interceptor-ref name="fileUpload">

上传单个文件的大小

<param name="maximumSize">500000</param>

文件的扩展名

<param name="allowedExtensions">.jsp</param>

文件的类型

<param name="allowedTypes">image/pjpeg,image/gif,text/xml,text/plain,application/msword,application/vnd.ms-excel</param>

</interceptor-ref>

<interceptor-ref name="defaultStack"/> -->

<result name="input">/demo/lucenes/upload_fail.jsp</result>

<result name="success">/demo/lucenes/upload_ok.jsp</result>

</action>

Action中用到的操作文件信息:

//下载使用的流

public InputStream tInputStream;





// 上传文件,必须的三个字段。

private File data;

// 文件名称

private String dataFileName;

// 文件类型

private String dataContentType;

//省略set/get.............

Word操作类:

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import org.apache.poi.POIXMLDocument;

import org.apache.poi.POIXMLTextExtractor;

import org.apache.poi.hwpf.HWPFDocument;

import org.apache.poi.hwpf.extractor.WordExtractor;

import org.apache.poi.hwpf.usermodel.CharacterProperties;

import org.apache.poi.hwpf.usermodel.HWPFList;

import org.apache.poi.hwpf.usermodel.ParagraphProperties;

import org.apache.poi.hwpf.usermodel.Range;

import org.apache.poi.openxml4j.opc.OPCPackage;

import org.apache.poi.xwpf.extractor.XWPFWordExtractor;

import org.junit.Test;





/**

 * 提取word内容

 * 

 * @author wangshouhai

 * @Version 2014-4-17:下午12:07:04

 */

public class ReadWord {





public static void main(String[] args) {





File file = new File("C:\\Users\\Administrator\\Desktop\\測试文档.docx");

// readWord2003(file);





readWord2007(file);



}





/**

* 支持word-2003



* @param args

*/

private static void readWord2003(File file) {

try {

FileInputStream fis = new FileInputStream(file);

// 创建WordExtractor对象

WordExtractor wordExtractor = new WordExtractor(fis);

// 取得全部文本内容

String text = wordExtractor.getText();

System.out.println("readWord2003--------------->"+text);

} catch (Exception e) {

e.printStackTrace();

}

}





@Test

// 支持word-2003

public static void readWordExtractor(File file) {

try {

FileInputStream fis = new FileInputStream(file);

// 创建WordExtractor对象

WordExtractor wordExtractor = new WordExtractor(fis);

// 通过getParagraphText()提取每一个段落

String[] paragraph = wordExtractor.getParagraphText();

System.out.println("该Word文件共同拥有" + paragraph.length + "段。");





for (int i = 0; i < paragraph.length; i++) {

System.out.println("readWordExtractor--------------->"+paragraph[i]);

}

} catch (Exception e) {

e.printStackTrace();

}

}





/**

* word 2007解决方式



* @param args

*/

@Test

public static void readWord2007(File file) {

try {

// word 2007,读取word中字符

OPCPackage opcPackage = POIXMLDocument.openPackage("D:\\apache-tomcat-6.0.18\\webapps\\GOVWBWeb\\upload\\user\\2014\\04\\18\\08\\193b299f-e8fc-4a32-a7ba-f951beeec1d9");

POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);

String text2007 = extractor.getText();

System.out.println("readWord2007--------------->"+text2007);

} catch (Exception e) {

e.printStackTrace();

}

}



}

Excel操作类:

import java.io.File;

import java.io.FileInputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.ss.usermodel.Sheet;

import org.apache.poi.ss.usermodel.Workbook;

import org.apache.poi.ss.usermodel.WorkbookFactory;

import org.apache.poi.xssf.usermodel.XSSFCell;

import org.apache.poi.xssf.usermodel.XSSFRow;

import org.apache.poi.xssf.usermodel.XSSFSheet;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;





/**

 * 读取excel内容

 * @author wangshouhai

 * @Version 2014-4-18:下午12:56:23

 */

public class ReadExcel {



// 文件上传

public static void readExcel2007(File file) {





try {

// 创建工作区,读取上传文件

XSSFWorkbook wb= new XSSFWorkbook(new FileInputStream(file));

XSSFSheet sheet =wb.getSheetAt(0); 

int rows = sheet.getPhysicalNumberOfRows();// 获取全部的行

if (rows > 0) {

for (int i = 1; i < rows; i++) {

XSSFRow row=sheet.getRow(i);

if (row == null) {

continue;

}

try {

XSSFCell idCell = row.getCell(0);

if (idCell != null) {

double id = idCell.getNumericCellValue();

//int id = Integer.parseInt(idCell.getRichStringCellValue().toString());

System.out.println("id----------->"+id);

}





// 账号

XSSFCell accountsCell = row.getCell(1);

String accounts = null;

if (accountsCell != null) {

accounts = accountsCell.getRichStringCellValue().toString();

System.out.println("accounts----------->"+accounts);

}





// password

XSSFCell passwordCell = row.getCell(2);

if (passwordCell != null) {

String password = passwordCell.getRichStringCellValue().toString();

System.out.println("password----------->"+password);

}





// 姓名

XSSFCell nameCell = row.getCell(3);

if (nameCell != null) {

String name = nameCell.getRichStringCellValue().toString();

System.out.println("name----------->"+name);

}





// 性别

XSSFCell sexCell = row.getCell(4);

if (sexCell != null) {

double sex = idCell.getNumericCellValue();

//String sex = sexCell.getRichStringCellValue().toString();

//int sexs = Integer.parseInt(sex);

System.out.println("sex----------->"+sex);

}





// 邮箱

XSSFCell emailCell = row.getCell(5);

if (emailCell != null) {

String email = emailCell.getRichStringCellValue().toString();

System.out.println("email----------->"+email);

}





// 手机

XSSFCell phoneCell = row.getCell(6);

if (phoneCell != null) {

String phone = phoneCell.getRichStringCellValue().toString();

System.out.println("phone----------->"+phone);

}





} catch (Exception e) {

throw new RuntimeException(e);

}

}

}

} catch (Exception e) {

throw new RuntimeException(e);

}

}







public static void readExcel2003(File file) {





try {

// 创建工作区,读取上传文件

Workbook wb = WorkbookFactory.create(new FileInputStream(file));

Sheet sheet = wb.getSheetAt(0);

int rows = sheet.getPhysicalNumberOfRows();// 获取全部的行





if (rows > 0) {

for (int i = 1; i < rows; i++) {

//获取每一行

HSSFRow row = (HSSFRow) sheet.getRow(i);

if (row == null) {

continue;

}

try {

//获取列数開始

HSSFCell idCell = row.getCell(0);

if (idCell != null) {

double id = idCell.getNumericCellValue();

// int id =

// Integer.parseInt(idCell.getRichStringCellValue().toString());

System.out.print("id: "+id+",");

}





// 账号

HSSFCell accountsCell = row.getCell(1);

String accounts = null;

if (accountsCell != null) {

accounts = accountsCell.getRichStringCellValue().toString();

System.out.print("accounts: "+accounts+",");

}





// password

HSSFCell passwordCell = row.getCell(2);

if (passwordCell != null) {

String password = passwordCell.getRichStringCellValue().toString();

System.out.print("password: "+password+",");

}





// 姓名

HSSFCell nameCell = row.getCell(3);

if (nameCell != null) {

String name = nameCell.getRichStringCellValue().toString();

System.out.print("name: "+name+",");

}





// 性别

HSSFCell sexCell = row.getCell(4);

if (sexCell != null) {

double sex = idCell.getNumericCellValue();

// String sex =

// sexCell.getRichStringCellValue().toString();

// int sexs = Integer.parseInt(sex);

System.out.print("sex: "+sex+",");

}





// 邮箱

HSSFCell emailCell = row.getCell(5);

if (emailCell != null) {

String email = emailCell.getRichStringCellValue().toString();

System.out.print("email: "+email+",");

}





// 手机

HSSFCell phoneCell = row.getCell(6);

if (phoneCell != null) {

String phone = phoneCell.getRichStringCellValue().toString();

System.out.println("phone: "+phone);

}

} catch (Exception e) {

throw new RuntimeException(e);

}

}

}

} catch (Exception e) {

throw new RuntimeException(e);

}

}



/**

* 读取Excel2007

* @param args

*/



public static void main(String[] args) {

File file = new File("D:\\apache-tomcat-6.0.18\\webapps\\GOVWBWeb\\upload\\user\\2014\\04\\18\\11\\adcc6bc6-bd5e-43e9-9a53-3ba879dfa62d.xlsx");

//readExcel2007(file);

readExcel2003(new File("C:\\Users\\Administrator\\Desktop\\export.xls"));

}

}

Pdf操作类:

import java.io.FileInputStream;

import org.apache.pdfbox.cos.COSDocument;

import org.apache.pdfbox.pdfparser.PDFParser;

import org.apache.pdfbox.pdmodel.PDDocument;

import org.apache.pdfbox.util.PDFTextStripper;





/**

 * 提取pdf中的内容

 * @author wangshouhai

 * @Version 2014-4-18:下午12:47:27

 */

public class ReadPdf {

public String readFdf(String file) {

try {

PDFParser parser = new PDFParser(new FileInputStream(file));

parser.parse();

COSDocument doc=parser.getDocument();

PDFTextStripper stripper = new PDFTextStripper();

String docText = stripper.getText(new PDDocument(doc));

docText= convertorSymbol(docText); 

return docText;

} catch (Exception e) {

throw new RuntimeException(e);

}

}









/**

* 处理特殊字符



* @param sub

* @param docText

*/

public static String convertorSymbol(String docText) {

StringBuilder sub = new StringBuilder();

char[] ch = docText.toCharArray();

for (int i = 0; i < ch.length; i++) {

char buf = ch[i];

if (9 == buf || 10 == buf || 13 == buf || 32 <= buf && !Character.isISOControl(buf)) {

sub.append(buf);

}

}

return sub.toString().replaceAll("\\s*", "");

}



public static void main(String args[]) {

String text =new ReadPdf().readFdf("D:\\html2.pdf");

System.out.println("ReadPdf---------->"+text);

}

}

文本操作类:

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStreamReader;





public class ReadText {

/**

* 读取文本内容



* @param dataFile

* @return

*/

public static String readText(File file) {

StringBuilder sub = new StringBuilder();

BufferedReader bufReader = null;

try {

bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));

String str;

while ((str = bufReader.readLine()) != null) {

sub.append(str);

}

return convertorSymbol(sub.toString());

} catch (Exception e) {

throw new RuntimeException(e);

} finally {

if (bufReader != null) {

try {

bufReader.close();

} catch (IOException e) {

throw new RuntimeException(e);

}

}

}

}



/**

* 处理特殊字符



* @param sub

* @param docText

*/

public static String convertorSymbol(String docText) {

StringBuilder sub = new StringBuilder();

char[] ch = docText.toCharArray();

for (int i = 0; i < ch.length; i++) {

char buf = ch[i];

if (9 == buf || 10 == buf || 13 == buf || 32 <= buf && !Character.isISOControl(buf)) {

sub.append(buf);

}

}

return sub.toString();

}







public static void main(String[] args) {

File file = new File("C:/Users/Administrator/Desktop/异常信息列表.txt");

String text = readText(file);

System.out.println("text-->"+text);

}

}

Word,Excel,pdf,txt等文件上传并提取内容的更多相关文章

  1. Struct2 csv文件上传读取中文内容乱码

    网络上搜索下,发现都不适合 最终改写代码: FileInputStream fis = null; InputStreamReader isr = null; BufferedReader br= n ...

  2. 文件上传bypass jsp内容检测的一些方法

    bx2=冰蝎2 前段时间渗透遇到了个检测jsp内容的,然后发现全unicode编码就可以绕过,但是对bx2马进行全编码他出现了一些错误,我尝试简单改了下,日站还是bx2操作舒服点 检测内容的话,这样直 ...

  3. springboot整合ueditor实现图片上传和文件上传功能

    springboot整合ueditor实现图片上传和文件上传功能 写在前面: 在阅读本篇之前,请先按照我的这篇随笔完成对ueditor的前期配置工作: springboot+layui 整合百度富文本 ...

  4. 利用百度编辑器和IIS限制文件上传的大小

    1.百度编辑器的大小限制 针对不同的文件类型限制大小 例如图片是imageMaxSize ,依次类推 /* 前后端通信相关的配置,注释只允许使用多行方式 */ {     /* 上传图片配置项 */ ...

  5. Spring MVC 学习总结(五)——校验与文件上传

    Spring MVC不仅是在架构上改变了项目,使代码变得可复用.可维护与可扩展,其实在功能上也加强了不少. 验证与文件上传是许多项目中不可缺少的一部分.在项目中验证非常重要,首先是安全性考虑,如防止注 ...

  6. Spring Boot会员管理系统——处理文件上传

    温馨提示 Spring Boot会员管理系统的中,需要涉及到Spring框架,SpringMVC框架,Hibernate框架,thymeleaf模板引擎.所以,可以学习下这些知识.当然,直接入门的话使 ...

  7. 补习系列(11)-springboot 文件上传原理

    目录 一.文件上传原理 二.springboot 文件机制 临时文件 定制配置 三.示例代码 A. 单文件上传 B. 多文件上传 C. 文件上传异常 D. Bean 配置 四.文件下载 小结 一.文件 ...

  8. JavaScript实现form表单的多文件上传

    form表单的多文件上传,具体内容如下 formData对象可以使用一系列的键值对来模拟一个完整的表单,然后使用Ajax来发送这个表单 使用<form>表单初始化FormData对象的方式 ...

  9. 文件上传下下载(不包含断点续传) Excel,Word导入导出基础

    1.文件上传下载(MVC应用) 视图:form表单,编码方式为multipart/form-data <body> <div> <form action="/D ...

随机推荐

  1. Lisp: Common Lisp, Racket, Clojure, Emacs Lisp - Hyperpolyglot

    Lisp: Common Lisp, Racket, Clojure, Emacs Lisp - Hyperpolyglot Lisp: Common Lisp, Racket, Clojure, E ...

  2. Oracle的dbms_output包的put()和put_line()的区别只是有没有回车换行吗?(转)

    答案是否 除了自动添加回车换行外,还有就是缓冲区最大容量的问题!! 无论如何设置serveroutput size,10g里 put() 最多只能输出 32767 个byte 而 put_line() ...

  3. 开源搜索引擎评估:lucene sphinx elasticsearch

    开源搜索引擎评估:lucene sphinx elasticsearch 开源搜索引擎程序有3大类 lucene系,java开发,包括solr和elasticsearch sphinx,c++开发,简 ...

  4. 我为什么要创建帮创业者找合伙人的缘创派(ycpai.com)?

    我为什么要创建帮助创业者找合伙人的缘创派(ycpai.com)? 在我发出第一条离开CSDN出来创业的微博后,感谢各位朋友的鼓励.很多朋友问我一些问题,我在这里一并回答,并简单阐述一下我的理念. 问: ...

  5. OCP读书笔记(2) - 配置恢复

    RMAN的命令类型 1. sqlplus命令 [oracle@oracle admin]$ export ORACLE_SID=orcl [oracle@oracle admin]$ rman tar ...

  6. postgresql优化数据的批量插入

    原文:http://www.cnblogs.com/mchina/archive/2012/08/11/2537393.html 有以下几种方法用于优化数据的批量插入. 1. 关闭自动提交:      ...

  7. Libevent使用样例,从简单到复杂

            转载请注明出处:http://blog.csdn.net/luotuo44/article/details/39670221 本文从简单到复杂.展示怎样使用libevent.网上的很多 ...

  8. poj2411(状压dp)

    题目链接:http://poj.org/problem?id=2411 题意:由1*2 的矩形通过组合拼成大矩形,求拼成指定的大矩形有几种拼法. 分析:如果是横着的就定义11,如果竖着的定义为竖着的0 ...

  9. 斯坦福ML公开课笔记14——主成分分析

    上一篇笔记中,介绍了因子分析模型,因子分析模型使用d维子空间的隐含变量z来拟合训练数据,所以实际上因子分析模型是一种数据降维的方法,它基于一个概率模型,使用EM算法来预计參数. 本篇主要介绍PCA(P ...

  10. Java垃圾回收机制以及内存泄露

    1.Java的内存泄露介绍 首先明白一下内存泄露的概念:内存泄露是指程序执行过程动态分配了内存,可是在程序结束的时候这块内存没有被释放,从而导致这块内存不可用,这就是内存 泄露,重新启动计算机能够解决 ...