您好,<!-- 文件下载,支持中文附件名 -->
<action name="fileDownload"
class="comtestactionfiledownFileDownloadAction">
<result name="success" type="stream">
<!-- 动态文件下载的,事先并不知道未来的文件类型,那么我们可以把它的值设置成为:application/octet-stream;charset=ISO8859-1 ,注意一定要加入charset,否则某些时候会导致下载的文件出错; -->
<param name="contentType">
application/octet-stream;charset=ISO8859-1
</param>
<param name="contentDisposition">
attachment;filename="${downloadFileName}"
</param>
<!-- 使用经过转码的文件名作为下载文件名,downloadFileName属性
对应action类中的方法 getDownloadFileName() 其中特殊的代码就是${downloadFileName},它的效果相当于运行的时候将action对象的属性的取值动态的填充在${}中间的部分,我们可以认为它等价于+action getDownloadFileName()。 -->
<param name="inputName">inputStream</param>
<param name="bufferSize">4096</param>
</result>
</action>
----------------------------------------------------------
action中
----------------------------------------------------------
private String fileName;// 初始的通过param指定的文件名属性 set get
/ 文件名 转换编码 防止中文乱码/
public String getDownloadFileName() {
String fileName=ServletActionContextgetRequest()getParameter("fileName");
String downFileName = fileName;
try {
downFileName = new String(downFileNamegetBytes(), "ISO8859-1");
} catch (Exception e) {
eprintStackTrace();
}
return downFileName;
}
//下载的流
public InputStream getInputStream() {
String name=thisgetDownloadFileName();
// String realPath=ServletActionContextgetServletContext()getRealPath("/uploadImages")+ "/"+name; 路径错误
String realPath="/uploadImages/"+name;
InputStream in=ServletActionContextgetServletContext()getResourceAsStream(realPath);
if(null==in){
Systemoutprintln("Can not find a javaioInputStream with the name [inputStream] in the invocation stack Check the <param name=\"inputName\"> tag specified for this action检查action中文件下载路径是否正确");
}
return ServletActionContextgetServletContext()getResourceAsStream(realPath);
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
package comtest;
/
需要的jar包:
poi-302-FINAL-20080204jar
poi-contrib-302-FINAL-20080204jar
poi-scratchpad-302-FINAL-20080204jar
poi-35-beta6-20090622jar
geronimo-stax-api_10_spec-10jar
ooxml-schemas-10jar
openxml4j-bin-betajar
poi-ooxml-35-beta6-20090622jar
xmlbeans-230jar
dom4j-161jar
/
import javaioByteArrayInputStream;
import javaioFileInputStream;
import javaioFileOutputStream;
import javaioIOException;
import javaioInputStream;
import javaioStringWriter;
import orgapachepdfboxpdmodelPDDocument;
import orgapachepdfboxutilPDFTextStripper;
import orgapachepoiPOIOLE2TextExtractor;
import orgapachepoiPOITextExtractor;
import orgapachepoiPOIXMLDocument;
import orgapachepoiPOIXMLTextExtractor;
import orgapachepoiextractorExtractorFactory;
import orgapachepoihssfusermodelHSSFCell;
import orgapachepoihssfusermodelHSSFRow;
import orgapachepoihssfusermodelHSSFSheet;
import orgapachepoihssfusermodelHSSFWorkbook;
import orgapachepoihwpfextractorWordExtractor;
import orgapachepoiopenxml4jexceptionsOpenXML4JException;
import orgapachepoiopenxml4jopcOPCPackage;
import orgapachepoipoifsfilesystemDirectoryEntry;
import orgapachepoipoifsfilesystemDocumentEntry;
import orgapachepoipoifsfilesystemPOIFSFileSystem;
import orgapachepoixslfextractorXSLFPowerPointExtractor;
import orgapachepoixssfusermodelXSSFCell;
import orgapachepoixssfusermodelXSSFRow;
import orgapachepoixssfusermodelXSSFSheet;
import orgapachepoixssfusermodelXSSFWorkbook;
import orgapachepoixwpfextractorXWPFWordExtractor;
import orgapachexmlbeansXmlException;
public class WordAndExcelExtractor {
public static void main(String[] args) {
try {
// 读取word
String wordFile = "D:/1doc";
//String wordText2007 = WordAndExcelExtractorextractTextFromDOC2007(wordFile);
//Systemoutprintln("wordText2007=======" + wordText2007);
InputStream isword = new FileInputStream(wordFile);
WordExtractor wordExtractor = new WordExtractor(isword);
Systemoutprintln("word========" + wordExtractorgetText());
// 读取 Excel
InputStream is = new FileInputStream("D:/测试xls");
String excelText = WordAndExcelExtractorextractTextFromXLS(is);
Systemoutprintln("text2003==========" + excelText);
String excelFile = "D:/test2xlsx";
String excelText2007 = WordAndExcelExtractor
extractTextFromXLS2007(excelFile);
Systemoutprintln("excelText2007==========" + excelText2007);
// 读取 PPT
PowerPointExtractor ppe = new PowerPointExtractor("D:/testppt");
Systemoutprintln("ppt2003===============" + ppegetText());
// Systemoutprintln("###############################");
// Systemoutprintln(ppegetText(true, true, true, true));
//
// InputStream is = new FileInputStream("D:/testppt");
// PowerPointExtractor ppt2003 = new PowerPointExtractor(is);
// Systemoutprintln(ppt2003getText());
Systemoutprintln("");
XSLFPowerPointExtractor ppt = new XSLFPowerPointExtractor(
POIXMLDocumentopenPackage("D:/test2pptx"));
Systemoutprintln("ppt2007============================="
+ pptgetText());
/ 读取PDF /
InputStream in = new FileInputStream("D:/testpdf");
PDDocument pdfDocument = PDDocumentload(in);
if (pdfDocumentisEncrypted()) {
// 仅仅尝试使用默认密码打开加密的PDF
pdfDocumentdecrypt("");
}
PDFTextStripper stripper = null;
// 创建一个writer用来作来存储文件正文
StringWriter writer = new StringWriter();
if (stripper == null) {
stripper = new PDFTextStripper();
} else {
stripperresetEngine();
}
stripperwriteText(pdfDocument, writer);
String contents = writergetBuffer()toString();
Systemoutprintln("pdfd===" + contents);
/ 向Word中写入数据 /
byte[] a = contentsgetBytes();
ByteArrayInputStream bs = new ByteArrayInputStream(a);
POIFSFileSystem fs = new POIFSFileSystem();
// /////////////////////////////////
DirectoryEntry directory = fsgetRoot();
DocumentEntry de = directorycreateDocument("WordDocument", bs);
// 以上两句代码不能省略,否则输出的是乱码
FileOutputStream fos = new FileOutputStream("D:\\dddoc");
fswriteFilesystem(fos);
bsclose();
fosflush();
fosclose();
Systemoutprintln("写入成功");
} catch (Exception e) {
eprintStackTrace();
}
}
/
@Method: extractTextFromXLS
@Description: 从excel 2003档中提取纯文本
@param
@return String
@throws
/
@SuppressWarnings("deprecation")
private static String extractTextFromXLS(InputStream is) throws IOException {
StringBuffer content = new StringBuffer();
HSSFWorkbook workbook = new HSSFWorkbook(is); // 创建对Excel工作簿文件的引用
for (int numSheets = 0; numSheets < workbookgetNumberOfSheets(); numSheets++) {
if (null != workbookgetSheetAt(numSheets)) {
HSSFSheet aSheet = workbookgetSheetAt(numSheets); // 获得一个sheet
contentappend(aSheetgetSheetName());
contentappend("\r\n-----------------------\r\n");
for (int rowNumOfSheet = 0; rowNumOfSheet <= aSheet
getLastRowNum(); rowNumOfSheet++) {
if (null != aSheetgetRow(rowNumOfSheet)) {
HSSFRow aRow = aSheetgetRow(rowNumOfSheet); // 获得一行
for (short cellNumOfRow = 0; cellNumOfRow <= aRow
getLastCellNum(); cellNumOfRow++) {
if (null != aRowgetCell(cellNumOfRow)) {
HSSFCell aCell = aRowgetCell(cellNumOfRow); // 获得列值
if (aCellgetCellType() == HSSFCellCELL_TYPE_NUMERIC) {
contentappend(aCellgetNumericCellValue());
} else if (aCellgetCellType() == HSSFCellCELL_TYPE_BOOLEAN) {
contentappend(aCellgetBooleanCellValue());
} else {
contentappend(aCellgetStringCellValue());
}
contentappend("\t");
}
}
contentappend("\r\n");
}
}
}
}
return contenttoString();
}
/
@Method: extractTextFromXLS2007
@Description: 从excel 2007文档中提取纯文本
@param
@return String
@throws
/
private static String extractTextFromXLS2007(String fileName)
throws Exception {
StringBuffer content = new StringBuffer();
// 构造 XSSFWorkbook 对象,strPath 传入文件路径
XSSFWorkbook xwb = new XSSFWorkbook(fileName);
// 循环工作表Sheet
for (int numSheet = 0; numSheet < xwbgetNumberOfSheets(); numSheet++) {
XSSFSheet xSheet = xwbgetSheetAt(numSheet);
if (xSheet == null) {
continue;
}
// 循环行Row
for (int rowNum = 0; rowNum <= xSheetgetLastRowNum(); rowNum++) {
XSSFRow xRow = xSheetgetRow(rowNum);
if (xRow == null) {
continue;
}
// 循环列Cell
for (int cellNum = 0; cellNum <= xRowgetLastCellNum(); cellNum++) {
XSSFCell xCell = xRowgetCell(cellNum);
if (xCell == null) {
continue;
}
if (xCellgetCellType() == XSSFCellCELL_TYPE_BOOLEAN) {
contentappend(xCellgetBooleanCellValue());
} else if (xCellgetCellType() == XSSFCellCELL_TYPE_NUMERIC) {
contentappend(xCellgetNumericCellValue());
} else {
contentappend(xCellgetStringCellValue());
}
}
}
}
return contenttoString();
}
}
这是POI jar包的下载地址,我下载的是39版本的
>
首先这个List尽量是Object类型的,也就是:Listlist其次就是开始遍历这个listintcurrentRowNum=startRowNum-1;//从第几行开始写intcurrentColNum=startColNum-1;//从第几列开始写for(Object[]objects:list){//遍历listXSSFRowrow=sheetcreateRow(currentRowNum);//一个list就是一行,创建一个行for(Objectobject:objects){//遍历每一个list的Object[]数组if(object==null){XSSFCellcell=rowcreateCell(currentColNum++,XSSFCellCELL_TYPE_BLANK);//如果该单元格为空,则创建一个空格子,不然数据会移位cellsetCellValue("");//将这个空格设置为空字符串}elseif(objectinstanceofDouble||objectinstanceofFloat){//判断这个格子放的数据的类型,其他的同理XSSFCellcell=rowcreateCell(currentColNum++,XSSFCellCELL_TYPE_NUMERIC);//如果是Double或者Float类型的,则用这个方式cellsetCellValue(DoubleparseDouble(objecttoString()));}elseif(objectinstanceofLong||objectinstanceofInteger){XSSFCellcell=rowcreateCell(currentColNum++,XSSFCellCELL_TYPE_NUMERIC);cellsetCellValue(DoubleparseDouble(objecttoString()));}elseif(objectinstanceofDate){XSSFCellcell=rowcreateCell(currentColNum++,XSSFCellCELL_TYPE_STRING);cellsetCellValue(newSimpleDateFormat("dd-MM-yyyy")format((Date)object));}elseif(objectinstanceofBoolean){XSSFCellcell=rowcreateCell(currentColNum++,XSSFCellCELL_TYPE_BOOLEAN);cellsetCellValue((Boolean)object);}else{XSSFCellcell=rowcreateCell(currentColNum++,XSSFCellCELL_TYPE_STRING);cellsetCellValue(StringvalueOf(object));}}currentRowNum++;//写完第一个list,跳到下一行currentColNum=startColNum-1;}这种是poi *** 作EXCEL的方法哈。。jxl *** 作略有不同。
以上就是关于没分了求教急急急!!!Can not find a java.io.InputStream with the name [inputStream]全部的内容,包括:没分了求教急急急!!!Can not find a java.io.InputStream with the name [inputStream]、谁能给我一个详细的Java通过Apache POI导出Excel方法,最好能给完整代码、easyexcel导出excel,传入的list集合是空,导出失败等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)