2 qq 24534807 qq_24534807 于 2017.09.19 16:04 提问

Java 下载文件,浏览器报错

这是下载的部分的代码,传过来的参数是正确的

 package com.hkt.bpu.asd.noss.pcms.admin_portal.common;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CSVWriterUtils {

    @SuppressWarnings("unchecked")
    public void creatcsv(String name,String[] header, HttpServletRequest request, HttpServletResponse response, List list
            ) throws Exception {

        response.setContentType("text/csv");

        java.util.Date date = new java.util.Date();
        java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyyMMddHHmmss");
        String strDate = df.format(date);

        response.addHeader("Content-Disposition","attachment; filename=\"" +name+"_"+strDate + ".csv\"");
        response.setContentType("text/csv;charset=UTF-8");

        PrintWriter out = response.getWriter();

        List title = new ArrayList();
        for (int i = 0; i < header.length; i++) {
            title.add(header[i]);
        }

        List datalist = new ArrayList();

        CsvWriter cw = null;
        try {
            cw = new CsvWriter(out);
            cw.writeLine(title);
//          if (list == null || list.size() == 0)
//              return;
//          for (Object o : list) {
//              if (o != null) {
//                  Field[] fields = o.getClass().getDeclaredFields();
//                  for (Field f : fields) {
//                      f.setAccessible(true);
//                      String objectname = f.getName();
//                      Object value = f.get(o);
//                      
//                      for(int i=0;i<header.length;i++){
//                          if(objectname.equals(header[i])){
//                              datalist.add(value.toString());
//                          }
//                      }
//                      
//                      if(datalist.size()==header.length){
//                          System.out.println("datalist---"+datalist);
//                          cw.writeLine(datalist);
//                          datalist = new ArrayList();
//                      }
//                      
//                  }
//              }
//          }
            cw.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (cw != null) {
                    cw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

然后在谷歌浏览器实现下载文件时,就报错了。

 POST http://localhost:8080/MonitorCircuitManipulation/download net::ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION

请问这是什么问题?

1个回答

u010715243
u010715243   2017.09.19 16:11
Csdn user default icon
上传中...
上传图片
插入图片