下面是一段简单的示例代码,有利用价值么? //send document on the application server to client browser
public void sendFile2Client(String fileURL,HttpServletRequest req,HttpServletResponse res) throws IOException
{
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
ServletOutputStream out = res.getOutputStream ();
int pos=fileURL.indexOf(".");
String fileext="";
String mime="";
if(pos!=-1)
fileext=fileURL.substring(pos+1);
mime=getmimetype(fileext); //---------------------------------------------------------------
// Set the output data's mime type
//---------------------------------------------------------------
//res.setContentType("charset=gb2312");
//res.setContentType( "application/vnd.ms-excel;charset=gb2312" ); // MIME type for excel doc
if(!mime.equals(""))
res.setContentType(mime+";charset=gb2312");
//---------------------------------------------------------------
// create an input stream from fileURL
//---------------------------------------------------------------
// String fileURL =req.getParameter("file"); //------------------------------------------------------------
// Content-disposition header - don't open in browser and
// set the "Save As..." filename.
// *There is reportedly a bug in IE4.0 which ignores this...
//------------------------------------------------------------
pos=fileURL.indexOf("File=");
String filename=fileURL.substring(pos+1);
res.setHeader("Content-disposition",
"attachment; filename=" +
filename ); // Use Buffered Stream for reading/writing.
// System.out.println("file ="+fileURL);
FileInputStream fileIn = new FileInputStream(fileURL);
bis = new BufferedInputStream(fileIn);
bos = new BufferedOutputStream(out); byte[] buff = new byte[2048];
int bytesRead; // Simple read/write loop.
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
System.out.println("get document ok");
} catch(Exception e) {
System.out.println (e);
}
finally
{
if(bis!=null) bis.close();
if(bos!=null) bos.close() ;
} }
public void sendFile2Client(String fileURL,HttpServletRequest req,HttpServletResponse res) throws IOException
{
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
ServletOutputStream out = res.getOutputStream ();
int pos=fileURL.indexOf(".");
String fileext="";
String mime="";
if(pos!=-1)
fileext=fileURL.substring(pos+1);
mime=getmimetype(fileext); //---------------------------------------------------------------
// Set the output data's mime type
//---------------------------------------------------------------
//res.setContentType("charset=gb2312");
//res.setContentType( "application/vnd.ms-excel;charset=gb2312" ); // MIME type for excel doc
if(!mime.equals(""))
res.setContentType(mime+";charset=gb2312");
//---------------------------------------------------------------
// create an input stream from fileURL
//---------------------------------------------------------------
// String fileURL =req.getParameter("file"); //------------------------------------------------------------
// Content-disposition header - don't open in browser and
// set the "Save As..." filename.
// *There is reportedly a bug in IE4.0 which ignores this...
//------------------------------------------------------------
pos=fileURL.indexOf("File=");
String filename=fileURL.substring(pos+1);
res.setHeader("Content-disposition",
"attachment; filename=" +
filename ); // Use Buffered Stream for reading/writing.
// System.out.println("file ="+fileURL);
FileInputStream fileIn = new FileInputStream(fileURL);
bis = new BufferedInputStream(fileIn);
bos = new BufferedOutputStream(out); byte[] buff = new byte[2048];
int bytesRead; // Simple read/write loop.
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
System.out.println("get document ok");
} catch(Exception e) {
System.out.println (e);
}
finally
{
if(bis!=null) bis.close();
if(bos!=null) bos.close() ;
} }
private String getmimetype(String fileext)
{
String mime="";
if(fileext.equalsIgnoreCase("zip") || fileext.equalsIgnoreCase("pdf") ||fileext.equalsIgnoreCase("jpeg"))
{
mime = "application/" + fileext;
return mime;
}
if(fileext.equalsIgnoreCase("doc"))
{
mime = "application/msword";
return mime;
}
if(fileext.equalsIgnoreCase("xls"))
{
mime = "application/vnd.ms-excel";
return mime;
}
if(fileext.equalsIgnoreCase("exe"))
{
mime="application/octet-stream";
return mime;
}
if(fileext.equalsIgnoreCase("txt")||fileext.equalsIgnoreCase("log"))
{
mime = "text/plain";
return mime;
}
if(fileext.equalsIgnoreCase("html"))
{
mime = "text/html";
return mime;
}
if(fileext.equalsIgnoreCase("jpg"))
{
mime = "Image/JPG";
return mime;
}
if(fileext.equalsIgnoreCase("gif"))
{
mime = "image/gif";
return mime;
}
return "application/octet-stream";
}
(java抽取word,pdf的四种武器)http://www-900.ibm.com/developerWorks/cn/java/l-java-tips/index.shtml
(技巧:将Word 文档转换成Simple Docbook XML)http://www-900.ibm.com/developerWorks/cn/xml/tips/x-tipword/index.shtml
2、excel,使用jxl:(利用JAVA操作EXCEL文件)
http://www-900.ibm.com/developerWorks/cn/java/l-javaExcel/index.shtml
支持中文非常好,jacob不支持非windows平台
stonewang(类中有笑) ,我会试一下,谢谢你