<%@ page import="java.io.*"%>
<%!
public String toUtf8String(String s)
{
StringBuffer sb = new StringBuffer()
for (int i=0i<s.length()i++) {
char c = s.charAt(i)
if (c >= 0 &&c <= 255) {
sb.append(c)
} else {
byte[] b
try {
b = Character.toString(c).getBytes("utf-8")
} catch (Exception ex) {
System.out.println(ex)
b = new byte[0]
}
for (int j = 0j <b.lengthj++) {
int k = b[j]
if (k <0) k += 256
sb.append("%" + Integer.toHexString(k).
toUpperCase())
}
}
}
return sb.toString()
}
%>
<%
String filename=new String(request.getParameter("filename").getBytes("ISO8859-1"),"GBK")
String dirName="D:/我.doc"
java.io.File ff=null
String dd=dirName+System.getProperties().getProperty("file.separator")+filename
try{
ff=new java.io.File(dd)
}
catch(Exception e){
System.out.println(e.getMessage())
e.printStackTrace()
}
if (ff!=null&&ff.exists()&&ff.isFile())
{
long filelength = ff.length()
InputStream inStream=new FileInputStream(dd)
//设置输出的格式
response.reset()
response.setContentType("application/x-msdownload")
response.setContentLength((int)filelength)
response.addHeader("Content-Disposition","attachmentfilename=\"" + toUtf8String(filename) + "\"")
//循环取出流中的数据
byte[] b = new byte[100]
int len
while((len=inStream.read(b)) >0)
response.getOutputStream().write(b,0,len)
inStream.close()
out.clear()
out = pageContext.pushBody()
}
%>
:<a href="d.jsp">aa</a>
上面的那个是用流写的 但是也可以用超链接下载
word有微软的专用格式,如果要读取其内容,可以使用jar包,如下:1。用jacob. 其实jacob是一个bridage,连接java和com或者win32函数的一个中间件,jacob并不能直接抽取word,excel等文件,需要自己写dll哦,不过已经有为你写好的了,就是jacob的作者一并提供了。 jacob下载: http://www.matrix.org.cn/down_view.asp?id=13 下载了jacob并放到指定的路径之后(dll放到path,jar文件放到classpath),就可以写你自己的抽取程序了,下面是一个例子: import java.io.Fileimport com.jacob.com.*import com.jacob.activeX.*public class FileExtracter{ public static void main(String[] args) { ActiveXComponent app = new ActiveXComponent("Word.Application")String inFile = "c:\\test.doc"String tpFile = "c:\\temp.htm"String otFile = "c:\\temp.xml"boolean flag = falsetry { app.setProperty("Visible", new Variant(false))Object docs = app.getProperty("Documents").toDispatch()Object doc = Dispatch.invoke(docs,"Open", Dispatch.Method, new Object[]{inFile,new Variant(false), new Variant(true)}, new int[1]).toDispatch()Dispatch.invoke(doc,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1])Variant f = new Variant(false)Dispatch.call(doc, "Close", f)flag = true} catch (Exception e) { e.printStackTrace()} finally { app.invoke("Quit", new Variant[] {})} } } 2。用apache的poi来抽取word,excel。 poi是apache的一个项目,不过就算用poi你可能都觉得很烦,不过不要紧,这里提供了更加简单的一个接口给你: 下载经过封装后的poi包: http://www.matrix.org.cn/down_view.asp?id=14 下载之后,放到你的classpath就可以了,下面是如何使用它的一个例子: import java.io.*import org.textmining.text.extraction.WordExtractor/** * Title: pdf extraction * Description: email:[email protected] * Copyright: Matrix Copyright (c) 2003 * Company: Matrix.org.cn * @author chris * @version 1.0,who use this example pls remain the declare */ public class PdfExtractor { public PdfExtractor() { } public static void main(String args[]) throws Exception { FileInputStream in = new FileInputStream ("c:\\a.doc")WordExtractor extractor = new WordExtractor()String str = extractor.extractText(in)System.out.println("the result length is"+str.length())System.out.println("the result is"+str)} }