本人写了一个下载文件的类,下载文件时,文件能够被下载,但是默认的文件名不是我想要的文件名:public class DownloadCSVFileAction extends Action{
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {                try {            
            String fileName = request.getParameter("fileName");
            long maID = Long.parseLong(request.getParameter("maID"));
            String filePath = request.getSession().getServletContext().getRealPath("/")+"csv/"+maID+"/"+fileName;
            File fdown = new File(filePath);
            int filelength = Integer.parseInt(String.valueOf(fdown.length()));
           
            response.setContentType("application/text;charset=GB2312");
            response.setHeader("Content-Dispositon","attachment;filename=销售详细记录.csv");  //销售详细记录.csv是我想要的下载文件的文件名,但是下载对话框中显示的是 downLoadCSVFile.do
            
            response.setContentLength(filelength);
            byte b[]=new byte[filelength];
            FileInputStream fi=new FileInputStream(fdown);           
            
            OutputStream o=response.getOutputStream();
            int n = 0;
            while((n=fi.read(b))!=-1) {
                o.write(b,0,n);
            }
            fi.close();
            o.close();
            return null;
        }catch (Exception e) {
            request.setAttribute("ERROR", e);
            return mapping.findForward("error");
        }    }
}

解决方案 »

  1.   

    试试下面的写法
    response.setHeader("Content-disposition","attachment;filename=\""+new String(销售详细记录.csv.getBytes("GB2312"),"ISO8859_1")+"\"")
      

  2.   

    文件名filename="...."不能直接用中文的
    response.setHeader("Content-Dispositon","attachment;filename=销售详细记录.csv");
    这样就可以了:
    String title = "销售详细记录";
    response.setHeader("Content-disposition","attachment; filename="

                      +new String( title.getBytes("GBK"), "ISO8859_1" )+".csv");
    我在下载pdf和word的时候都是这么用的
      

  3.   

    已经改成如下:response.setContentType("application/text;charset=ISO8859_1");
                  String title = "销售详细记录";
                  response.setHeader("Content-Dispositon","attachment;filename="+new String(title.getBytes("GBK"),"ISO8859_1")+".csv");可是还是不行,文件名称还是downLoadCSVFile.do
      

  4.   

    有哪位有jspSmart下载文件的例子?
      

  5.   

    我以前都是用的servlet
    下面是我下载pdf文档的一段代码,希望对你有帮助,
    ----------------------------------------------------
    import ...........public class OutPutDataAction extends HttpServlet{
    ............

    public void doPost(HttpServletRequest request,HttpServletResponse response) throws java.io.IOException{ //设置类型为pdf
    response.setContentType("application/pdf;charset=gb2312");

                    //Function.randomFileName()生成一个随机的文件名,如:13059853263
                    //最好不要为中文
    String filepath = F:/pdffile/"+Function.randomFileName()+".pdf";

                    //这里设置下载框中的文件名
                    String title="下载的文件名";         response.setHeader("Content-disposition","attachment; filename="

                      +new String( title.getBytes("GBK"), "ISO8859_1" )+".pdf"); //---------------------------------
                    //下面建立pdf文档
    Document document = new Document(PageSize.A4, 36,36,36,36);
             
             java.io.FileOutputStream output = new java.io.FileOutputStream(filepath);
             
             try{
             
               PdfWriter writer = PdfWriter.getInstance(document, output);
               
               document.addTitle(title);
               
               document.addAuthor("SWOKY");
               
               document.addSubject("This example explains "+title);
               
               document.addKeywords("jishou university,swoky");
               
               document.addCreator("Powder by :SWOKY");
               
               document.open();
               
               Image img = Image.getInstance(SystemProperty.get("WEBROOT")+"/jsp/file/temp.jpg");
               
               img.setAlignment(Image.ALIGN_CENTER);
               
               document.add(img);
               
               document.close();
               ------------------------
               //以上为在服务器由filepath所指向的路径建立一个pdf文档           //downloadFile()为将刚才建立的文档输出到客户端
               downloadFile(filepath,response);
             
             }catch (DocumentException de){
               //异常
             }
               
    }

    private void downloadFile(String filepath,HttpServletResponse response) throws java.io.IOException {

    java.io.BufferedInputStream bis = 
            
                          new java.io.BufferedInputStream(
                          
                          new java.io.FileInputStream(new String( filepath.getBytes("GBK"), "ISO8859_1" )));
                          
            javax.servlet.ServletOutputStream bos = response.getOutputStream();
                          
            byte[] buff = new byte[2048];
            
            int bytesRead;
            
            while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
            
                bos.write(buff,0,bytesRead);
                
            }
            
            bos.flush();
            
            bis.close();
            
            bos.close();
            
            //timeout
            response.setStatus(response.SC_OK );
            
            response.flushBuffer();
    }
    ..................


    ---------------------------------------------------------
      

  6.   

    问题解决:使用jspSmart
    <%@ page contentType="text/html;charset=gb2312" 
    import="com.jspsmart.upload.*" %><% 
    SmartUpload su = new SmartUpload(); 
    su.initialize(pageContext); 
    su.setContentDisposition(null); 
    String maID = request.getParameter("maID");
    String fileName = request.getParameter("fileName");
    su.downloadFile("/csv/"+maID+"/"+fileName); 
    %>