下载文件时,弹出保存框时 文件名是乱码 如何解决 
显示的是这种样子的名字 C4%BF%CA% 

解决方案 »

  1.   

    new string().getbytes();进行强制转码  统一编码没有用 必须进行强制转 
      

  2.   

    File file = new File("文件路径");
    HttpServletResponse response=ServletActionContext.getResponse();
    OutputStream out = response.getOutputStream();
    InputStream in = new FileInputStream(file);
    DataInputStream din = new DataInputStream(new BufferedInputStream(in));
    DataOutputStream dout = new DataOutputStream(new BufferedOutputStream(out));
    //response.setContentType("application/octet-stream");

    String displayname = new String("新建文本文档 (2).txt".getBytes("GBK"),"ISO-8859-1");

    response.setContentType("application/x-download");
    response.setHeader("Content-Disposition", "attachment;filename="+ displayname);在IE下,用utf-8好像还是乱码,所以就用GBK,就可以了
      

  3.   

    C4%BF%CA% 这是URL编码百度 URL 编码
      

  4.   

    filename = URLEncoder.encode(filename, "GBK");
        
        //弹出下载对话框的关键代码
        response.setContentType("application/x-download");
        response.setHeader("Content-Disposition","attachment;filename="+ filename);设置了编码方式没?
      

  5.   

    中文编码的三种方式:1 、GBK
                     2、gb2312
                     3、UTF-8
      

  6.   

    LZ  我把代码贴给你 要结贴哦 
    public class util2 {
    public static void excel(HttpServletResponse response, List list,
               String[] firstLine, String sheetName, String fileName) {
           String[] array1 = null;
           try {
            //格式化时间
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            String  nowDate=sdf.format(new Date());
            String filename=fileName+nowDate+".xls";
            short i = 0;// row行标
               response.setContentType("application/vnd.ms-excel");//设置生成的文件类型
               response.setHeader("Content-Disposition", "filename="+ new String(filename.getBytes("gb2312"), "iso8859-1"));//设置文件头编码方式和文件名
               HSSFWorkbook wb = new HSSFWorkbook();//excel文件,一个excel文件包含多个表
               HSSFSheet sheet = wb.createSheet();//表,一个表包含多个行
               wb.setSheetName(0, sheetName, HSSFWorkbook.ENCODING_UTF_16);// 设置sheet中文编码;
               //设置字体等样式
               HSSFFont font = wb.createFont();
               font.setFontHeightInPoints((short) 12);
               font.setFontName("Courier New");
               HSSFCellStyle style = wb.createCellStyle();
               style.setFont(font);
               style.setWrapText(true);
               style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
               style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
               HSSFRow row;//行,一行包括多个单元格
               HSSFCell cell;//单元格
               row = sheet.createRow(i);//由HSSFSheet生成行
               row.setHeightInPoints((float) 30);
               //生成首行
               for (short j = 0; j < firstLine.length; j++) {               cell = row.createCell(j);//由行生成单元格               cell.setCellStyle(style);               cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell中文编码;               cell.setCellValue(firstLine[j]);
                  sheet.setColumnWidth(j, (short) (5000));            }
               //生成所有行的单元格内容,如果测试list设为null即可,或者将这一段代码注释掉
               if (null == list || list.size() == 0) {
                  // do nothing
               } else {
                  for (int k = 0; k < list.size(); k++) {
                      row = sheet.createRow(++i);
                      row.setHeightInPoints((float) 20);
                      array1 = (String[]) list.get(k);
                      if (null != array1 && array1.length != 0) {
                         for (int f = 0; f < array1.length; f++) {
                             cell = row.createCell((short) f);
                             cell.setCellStyle(style);
                             cell.setEncoding(HSSFCell.ENCODING_UTF_16);
                             System.out.println("==========="+array1[f]);
                             cell.setCellValue(array1[f]);
                         }
                      }
    //                 row = sheet.createRow((int)++i);
    //                   Student stu = (Student) list.get(k);
    //                   //第四步,创建单元格,并设置值
    //                   cell = row.createCell((short) k);
    //                   cell.setCellStyle(style);
    //                   row.createCell((short)0).setCellValue((double)stu.getId());
    //                   cell.setEncoding(HSSFCell.ENCODING_UTF_16);
    //                   row.createCell((short)1).setCellValue(stu.getName());
    //                   row.createCell((short)2).setCellValue((double)stu.getAge());
    //                   row.createCell((short)3).setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu.getBrith()));
                  }
               }
               //输出
               OutputStream out = response.getOutputStream();
               wb.write(out);
               out.close();
           } catch (Exception ex) {
               ex.printStackTrace();
           }
           return;
        }
    //导出Excel文件
    public static List importExcels(String filename) {   
    List list=new ArrayList();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");   
        File f = new File(filename);   
        FileInputStream is;
        LvyouTopic emp=null;
        int count = 0;   
        try {   
            is = new FileInputStream(new File(filename));   
            HSSFWorkbook wb = new HSSFWorkbook(is);   
            int sheetNum = wb.getNumberOfSheets();   
            for (int i = 0; i < sheetNum; i++) {   
                HSSFSheet childSheet = wb.getSheetAt(i);   
                int rowNum = childSheet.getLastRowNum();   
                if (rowNum > 0) {   
                    for (int j = 1; j <= rowNum; j++) {   
                        for (int k = 0; k < 1; k++) {   
                         emp=new LvyouTopic();   
                            HSSFCell b = childSheet.getRow(j).getCell(   
                                    (short) (k+0));//No   
                            if (b != null) {   
                                emp.setId(Integer.parseInt(b.toString()));   
                            } else {   
                                count++;   
                                continue;   
                            }   
                           b = childSheet.getRow(j).getCell((short) (k+1));// Name   
                           if (b != null) {   
                               emp.setStuName(b.toString());   
                            } else {   
                                count++;   
                               continue;   
                            }   
                           b=childSheet.getRow(j).getCell((short)(k+2));
                           if(b!=null){
                            try {
                            emp.setStuAddress(b.toString());
    } catch (Exception e) {
    e.printStackTrace();
    }
                           }else{
                            count++;
                            continue;
                           }
    //                      b = childSheet.getRow(j).getCell((short) (k+2));// Birthday   
    //                        if (b != null) {   
    //                           try{   
    //                             Date  date=sdf.parse(b.toString());   
    //                            if(date!=null){   
    //                            emp.setBirthday(date);   
    //                            }else{   
    //                                 count++;   
    //                                continue;   
    //                             }   
    //                             }catch(Exception e){   
    //                                 count++;   
    //                                continue;   
    //                           }   
    //                         } else {   
    //                             count++;   
    //                             continue;   
    //                         }   
    //                         b = childSheet.getRow(j).getCell((short) (k+3));// CardType   
    //                        if (b != null) {   
    //                             emp.setCardType(b.toString());   
    //                         } else {   
    //                            emp.setCardType("");   
    //                         }   
    //   
    //                         b = childSheet.getRow(j).getCell((short) (k+4));// CardNO   
    //                        if (b != null) {   
    //                             emp.setCardNo(b.toString());   
    //                         } else {   
    //                             emp.setCardNo("");   
    //                         }   
    //                         b = childSheet.getRow(j).getCell((short) (k+5)); //Sex   
    //                         if (b != null&&!"".equals(b.toString().trim())) {   
    //                             if(b.toString().equals("男")){   
    //                                 emp.setSex(SexEnum.Man.getValue());   
    //                             }else if(b.toString().equals("女")){   
    //                                 emp.setSex(SexEnum.Woman.getValue());   
    //                             }else{   
    //                                 emp.setSex(SexEnum.Unknow.getValue());   
    //                             }   
    //                         } else {   
    //                             emp.setSex(0);   
    //                         }   
    //                         b = childSheet.getRow(j).getCell((short) (k+6));// Telַ   
    //                         if (b != null) {   
    //                             emp.setTel(b.toString());   
    //                         } else {   
    //                             emp.setTel("");   
    //                         }   
      
    //                         b = childSheet.getRow(j).getCell((short) (k+7));//Mobile   
    //                         if (b != null) {   
    //                             emp.setMobile(b.toString());   
    //                         } else {   
    //                             count++;   
    //                             continue;   
    //                         }   
    //                         if(emp.getNo()!=null&&!"".equals(emp.getNo().trim())){   
    //                             if(this.isHasNo(emp.getNo())){   
    //                                 count++;   
    //                                 continue;   
    //                             }   
    //                         }else{   
    //                             count++;   
    //                             continue;   
    //                         }   
    //                         emp.setDepartment(dep.getUid());   
    //                         emp.setExpertType("");   
    //                         emp.setStatex(EmployeeStateEnum.Normal.getValue());   
    //                        emp.setUser(GeneralUtil.getLoginUser().getUid());   
    //                            
    //                         ServiceUtil.getInstance().getEmployeeService().create(emp);   
                            }   
                        }   
                  }
                  list.add(emp);
                }   
      
        } catch (FileNotFoundException e) {   
            e.printStackTrace();   
        } catch (IOException e) {   
            e.printStackTrace();   
        }    
    return list;   
      }
      

  7.   

    ervletOutputStream os = response.getOutputStream();// 获取输出流
    response.reset();
    response.setHeader("Content-Disposition", "attachment; filename=\""
    + URLEncoder.encode("名称", "UTF8") + ".xls"
    + "\"");// 设定输出文件头
    response.setContentType("application/msexcel");// 设定输出类型
    out.flush();
    out.close();试试