怎样将服务器上的excel文件,在客户端用excel打开而不是嵌在浏览器里? UP一下我也被这个问题困扰,唯一能做的就是让客户点击右键-->另存为^-^ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <%@ page import="java.io.*,cn.com.ahead.A6.Mail.*"%> <%@ page language="java" session="true"%> <% String fileName=request.getParameter("file"); response.setHeader("Content-disposition", "attachment; filename=" + fileName); ExtName extname=new ExtName(); String type=extname.getTypeByFileName(fileName); if(type==null) type=new String("*.*"); response.setContentType(type); InputStream inputstream=(InputStream)session.getAttribute(fileName); BufferedInputStream bufferedinputstream =new BufferedInputStream(inputstream); PrintWriter fileout = response.getWriter(); int n; while ((n=bufferedinputstream.read())!=-1){ fileout.write((char)n); } bufferedinputstream.close(); fileout.flush();%> -------------------ExtName.java-------------------package cn.com.ahead.A6.Mail;import java.util.*;public class ExtName{ //Hash表,存一般文件类型 Map type; public ExtName(){ this.type=new HashMap(); fillType(); } /* *根据文件扩展名从哈希表中得到文件类型 *@parm 文件扩展名,如(.doc,.exe等) */ public String getTypeByExtName(String fileExtName){ String fileType; if(type.isEmpty()){ fileType="text/html"; }else{ fileType=(String)type.get(fileExtName); } return fileType; } /* *根据文件名从哈希表中得到文件类型 *@parm 文件名,如(1.doc,fill.exe等) */ public String getTypeByFileName(String fileName){ String fileType; String extName; if(type.isEmpty()||(fileName.indexOf(".")==-1)){ fileType="text/html"; }else{ extName=fileName.substring(fileName.indexOf(".")); fileType=(String)type.get(extName); } return fileType; } /* *把基本文件类型保存到Hash表中 */ public void fillType(){ type.clear(); //Word Processing Document type.put(".doc","application/msword"); type.put(".rtf","application/rtf"); type.put(".wri","application/mswrite"); type.put(".tex","application/latex"); //Web Page type.put(".htm","text/html"); type.put(".html","text/html"); //Video type.put(".avi","video/msvideo"); type.put(".mov","video/quicktime"); type.put(".mpe","video/mpeg"); type.put(".mpeg","video/mpeg"); type.put(".mpg","video/mpeg"); type.put(".qt","video/quicktime"); type.put(".rm","application/x-pn-realaudio"); //Spreadsheet type.put(".xls","application/excel"); //Software Source Code type.put(".c","text/txt"); type.put(".java","text/x-java-source"); type.put(".php","text/txt"); type.put(".h","text/txt"); type.put(".cpp","text/txt"); type.put(".js","application/x-javascript"); //type.put(".vbp","application/"); type.put(".vjp","text/txt"); //type.put(".ebp","application/"); type.put(".pl","text/txt"); //Presentation type.put(".pot","application/vnd.ms-powerpoint"); type.put(".pps","application/x-mspowerpoint"); type.put(".ppt","application/ms-powerpoint"); type.put(".ppz","application/mspowerpoint"); //Portable Document type.put(".pdf","application/pdf"); type.put(".ps","application/postscript"); type.put(".eps","application/postscript"); type.put(".dvi","application/postscript"); //Math Application type.put(".mcd","application/x-mathcad"); type.put(".mws","application/maple"); type.put(".m","application/matlab"); type.put(".nb","application/mathmatica"); //Java Applet type.put(".class","application/x-java-class"); //Image type.put(".bmp","image/x-windows-bmp"); type.put(".gif","image/gif"); type.put(".jpeg","iamge/jpeg"); type.put(".jpg","image/jpeg"); type.put(".tif","iamge/tiff"); type.put(".tiff","image/tiff"); //Executeable type.put(".bin","application/octet-stream"); type.put(".dll","application/octet-stream"); type.put(".exe","application/octet-stream"); //type.put(".js","application/x-javascript"); type.put(".ls","application/x-javascript"); type.put(".mocha","application/x-javascript"); type.put(".tbk","application/toolbook"); //Chemical Structure type.put(".csm","chemical/x-csml"); type.put(".emb","chemical/x-embl-dl-nucleotide"); type.put(".gau","chemical/x-gaussian-input"); type.put(".mol","chemical/x-mdl-molfile"); type.put(".mop","chemical/x-mopac-input"); type.put(".pdb","chemical/x-pdb"); type.put(".rxn","chemical/x-mol-rxnfile"); //Audio type.put(".aif","audio/basic"); type.put(".au","audio/basic"); type.put(".mid","audio/basic"); type.put(".mp2","audio/mpeg"); type.put(".mp3","audio/mpeg"); type.put(".ra","application/x-pn-realaudio"); type.put(".ram","audio/x-pn-realaudio"); //type.put(".rm","application/x-pn-realaudio"); type.put(".rpm","audio/x-pn-realaudio"); type.put(".snd","audio/basic"); type.put(".vox","audio/voxware"); type.put(".wav","audio/wav"); }}-----应该没有问题,我试过的。 楼上,你的ExtName.java完全可以不写,直接在web.xml里写就可以了 在web.xml写<web-app><mime-mapping> <extension>doc</extension> <mime-type>application/msword</mime-type> </mime-mapping> <mime-mapping> <extension>xls</extension> <mime-type>application/msexcel</mime-type> </mime-mapping> <mime-mapping> <extension>pdf</extension> <mime-type>application/pdf</mime-type> </mime-mapping> </web-app> java数据结构的问题 配置Struts2核心控制器完后服务器启动就报错,不能初始化,求解? 请问tld 文件中 tei-class 指向的类是干嘛用的? javamail接收出错!! 如何实现固定时间内容转换 关于数据库修改数据问题 急,急,急,希望RESIN或是SPRING框架的高手帮助!!!! 数据库的一个简单问题 ★如何重启一个类 分页怎么做? 帮我解释一下,入门问题,在线等待!window2000server+weblogic7.0+oracle9.02 局域网中如何通过解析好的域名来访问jsp站点??(请大虾们帮忙解决!!!在线等待!!)
<%@ page language="java" session="true"%>
<%
String fileName=request.getParameter("file");
response.setHeader("Content-disposition", "attachment; filename=" + fileName); ExtName extname=new ExtName();
String type=extname.getTypeByFileName(fileName);
if(type==null) type=new String("*.*");
response.setContentType(type); InputStream inputstream=(InputStream)session.getAttribute(fileName);
BufferedInputStream bufferedinputstream =new BufferedInputStream(inputstream);
PrintWriter fileout = response.getWriter();
int n;
while ((n=bufferedinputstream.read())!=-1){
fileout.write((char)n);
}
bufferedinputstream.close();
fileout.flush();
%> -------------------
ExtName.java
-------------------
package cn.com.ahead.A6.Mail;import java.util.*;public class ExtName{ //Hash表,存一般文件类型
Map type; public ExtName(){
this.type=new HashMap();
fillType();
} /*
*根据文件扩展名从哈希表中得到文件类型
*@parm 文件扩展名,如(.doc,.exe等)
*/
public String getTypeByExtName(String fileExtName){
String fileType;
if(type.isEmpty()){
fileType="text/html";
}else{
fileType=(String)type.get(fileExtName);
}
return fileType;
} /*
*根据文件名从哈希表中得到文件类型
*@parm 文件名,如(1.doc,fill.exe等)
*/
public String getTypeByFileName(String fileName){
String fileType;
String extName;
if(type.isEmpty()||(fileName.indexOf(".")==-1)){
fileType="text/html";
}else{
extName=fileName.substring(fileName.indexOf("."));
fileType=(String)type.get(extName);
}
return fileType;
} /*
*把基本文件类型保存到Hash表中
*/
public void fillType(){
type.clear();
//Word Processing Document
type.put(".doc","application/msword");
type.put(".rtf","application/rtf");
type.put(".wri","application/mswrite");
type.put(".tex","application/latex"); //Web Page
type.put(".htm","text/html");
type.put(".html","text/html"); //Video
type.put(".avi","video/msvideo");
type.put(".mov","video/quicktime");
type.put(".mpe","video/mpeg");
type.put(".mpeg","video/mpeg");
type.put(".mpg","video/mpeg");
type.put(".qt","video/quicktime");
type.put(".rm","application/x-pn-realaudio"); //Spreadsheet
type.put(".xls","application/excel"); //Software Source Code
type.put(".c","text/txt");
type.put(".java","text/x-java-source");
type.put(".php","text/txt");
type.put(".h","text/txt");
type.put(".cpp","text/txt");
type.put(".js","application/x-javascript");
//type.put(".vbp","application/");
type.put(".vjp","text/txt");
//type.put(".ebp","application/");
type.put(".pl","text/txt"); //Presentation
type.put(".pot","application/vnd.ms-powerpoint");
type.put(".pps","application/x-mspowerpoint");
type.put(".ppt","application/ms-powerpoint");
type.put(".ppz","application/mspowerpoint"); //Portable Document
type.put(".pdf","application/pdf");
type.put(".ps","application/postscript");
type.put(".eps","application/postscript");
type.put(".dvi","application/postscript"); //Math Application
type.put(".mcd","application/x-mathcad");
type.put(".mws","application/maple");
type.put(".m","application/matlab");
type.put(".nb","application/mathmatica"); //Java Applet
type.put(".class","application/x-java-class"); //Image
type.put(".bmp","image/x-windows-bmp");
type.put(".gif","image/gif");
type.put(".jpeg","iamge/jpeg");
type.put(".jpg","image/jpeg");
type.put(".tif","iamge/tiff");
type.put(".tiff","image/tiff"); //Executeable
type.put(".bin","application/octet-stream");
type.put(".dll","application/octet-stream");
type.put(".exe","application/octet-stream");
//type.put(".js","application/x-javascript");
type.put(".ls","application/x-javascript");
type.put(".mocha","application/x-javascript");
type.put(".tbk","application/toolbook"); //Chemical Structure
type.put(".csm","chemical/x-csml");
type.put(".emb","chemical/x-embl-dl-nucleotide");
type.put(".gau","chemical/x-gaussian-input");
type.put(".mol","chemical/x-mdl-molfile");
type.put(".mop","chemical/x-mopac-input");
type.put(".pdb","chemical/x-pdb");
type.put(".rxn","chemical/x-mol-rxnfile"); //Audio
type.put(".aif","audio/basic");
type.put(".au","audio/basic");
type.put(".mid","audio/basic");
type.put(".mp2","audio/mpeg");
type.put(".mp3","audio/mpeg");
type.put(".ra","application/x-pn-realaudio");
type.put(".ram","audio/x-pn-realaudio");
//type.put(".rm","application/x-pn-realaudio");
type.put(".rpm","audio/x-pn-realaudio");
type.put(".snd","audio/basic");
type.put(".vox","audio/voxware");
type.put(".wav","audio/wav");
}
}-----
应该没有问题,我试过的。
<extension>doc</extension>
<mime-type>application/msword</mime-type>
</mime-mapping>
<mime-mapping>
<extension>xls</extension>
<mime-type>application/msexcel</mime-type>
</mime-mapping>
<mime-mapping>
<extension>pdf</extension>
<mime-type>application/pdf</mime-type>
</mime-mapping> </web-app>