在struts中jsp页面点击个链接直接打开文件(不是下载文件),action后调用一个方法
downLoadFile(String filePath,HttpServletResponse response)//filePath是传过来的路径+文件名
File f = new File(filePath);
.....中间的操作
URL u = new URL("file:///"+filePath);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename="+f.getName());
....后续操作,读、写文件但是如果打开的是txt文件的话就可以直接打开,是doc或者xls文件的话就不能直接打开,而是弹出"保存、打开、取消"的窗口
调试发现u.openConnection().getContentType()得到的值
如果是txt文件的话就可以得到是Conten type是text/plain
但是其他文件得到的Conten type就是content/unknown
所以不能直接打开,在conf/web.xml都设置了mime类型了
所以想请各位帮我解决下这个问题。
thank!

解决方案 »

  1.   

    response.setContentType("application/msword");
    response.setContentType("application/vnd.ms-excel");类型 含意
    application/msword Microsoft Word document
    application/octet-stream Unrecognized or binary data
    application/pdf Acrobat (.pdf) file
    application/postscript PostScript file
    application/vnd.ms-excel Excel spreadsheet
    application/vnd.ms-powerpoint Powerpoint presentation
    application/x-gzip Gzip archive
    application/x-java-archive JAR file
    application/x-java-vm Java bytecode (.class) file
    application/zip Zip archive
    audio/basic Sound file in .au or .snd format
    audio/x-aiff AIFF sound file
    audio/x-wav Microsoft Windows sound file
    audio/midi MIDI sound file
    text/css HTML cascading style sheet
    text/html HTML document
    text/plain Plain text
    text/xml XML document
    image/gif GIF image
    image/jpeg JPEG image
    image/png PNG image
    image/tiff TIFF image
    video/mpeg MPEG video clip
    video/quicktime QuickTime video clip
      

  2.   

    这个mime的设置我知道,但是为什么url没有取得正确的content-type的类型,只得到了txt文本文件的类型呢,他不是根据要下载的文件的类型自动取的类型呢,因为要下载的文件不一定是word和xls,有可能还是ppt,chm,pdf啊,难道要按后缀名来写conten-type?那tomcat的web.xml里面的mime-mapping设置还有什么用呢?