UP一下
我也被这个问题困扰,唯一能做的就是让客户点击右键-->另存为^-^

解决方案 »

  1.   

    <%@ 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");
    }
    }-----
    应该没有问题,我试过的。
      

  2.   

    楼上,你的ExtName.java完全可以不写,直接在web.xml里写就可以了
      

  3.   

    在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>