jsp中使用文件流输出下载时的问题
下载txt,rar文件时都没有问题,而下载doc,xls,jpg等文件时,下载下来的都是空文件。
俺是新手,请各位帮帮俺啦~
下面是我的代码:
[code=Java][/code]<%@ page language="java" import="java.io.*,zyc.chstr.*"
     pageEncoding="gbk"%><%
       //关于文件下载时采用文件流输出的方式处理:
    		String path=(String)request.getParameter("path");//这是我的文件路径
		String name=(String)request.getParameter("name");//这是我的文件名
		String filepath=path+"/"+name;
       	response.reset();
   	response.setContentType("application/x-download;charset=gbk");
       	String filedownload = filepath;
      	String filedisplay =new Chstr().chStr(name);
  	filedisplay=new String(filedisplay.getBytes("gbk"),"iso8859-1");
          response.addHeader("Content-Disposition","attachment;filename=" + filedisplay);          
       OutputStream outp = null;
       FileInputStream in = null;
       try
       {
           outp = response.getOutputStream();
           in = new FileInputStream(filedownload);
           byte[] b = new byte[1024];
           int i = 0;
           while((i = in.read(b)) > 0)
           {
               outp.write(b, 0, i);
           }
           outp.flush();
       }
       catch(Exception e)
       {
           System.out.println("Error!");
           e.printStackTrace();
       }
       finally
       {
           if(in != null)
           {
               in.close();
               in = null;
           }
           if(outp != null)
           {
               outp.close();
               outp = null;
           }
       }
%>
------解决方案--------------------
楼主,这里设置问题吧:不同文件,设置文件类型不一样啊。
response.setContentType("application/x-download;charset=gbk");
比如xls:response.setContentType("application/vnd.ms-excel");
网上查下不用文件的类型的声明。