2017-06-15 77 views
-1
String fileName="raj.doc"; 
ServletOutputStream stream=null; 
BufferedInputStream buf=null; 
stream=res.getOutputStream(); 
String s1=getServletContext().getRealPath("/web-inf/lib/raj.doc"); 
File doc=new File(s1); 

res.setContentType("application/vnd.ms-word"); 
res.addHeader("Content-Disposition","attachment;filename= "+fileName); 
res.setContentLength((int)doc.length()); 
FileInputStream input=new FileInputStream(doc); 
buf=new BufferedInputStream(input); 
int readBytes=0; 
while((readBytes=buf.read())!=-1) 
stream.write(readBytes); 

给我一个在java中下载MS-word文件的例子。告诉我需要的jar文件。如何在java下载MS-word文件?

+0

你想*从服务器上下载*文件,或者*阅读*你已有的文件?如果是后者,请查看[Apache Poi](https://poi.apache.org/)。 –

+0

我有预定义的模板。我想以MS-word类型下载该模板。 –

回答

0

如果您只想下载该文件并且无法使用该文件,则不需要任何jar文件。 只需使用this代码并将URL替换为文档的URL即可。然后,您应该能够创建一个新的文件,并提供您从文件的输出流中的URL中读取的所有内容。

+0

感谢Andy指出我之前的链接是错误的 – PLEXATIC