我的应用程序需要UI中的下载按钮,点击后可以让用户将数据下载为csv文件。 我有工作实现这样做,服务器端代码是这样的:
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
int BUFFER = 1024;
resp.setContentType("application/octet-stream");
resp.setHeader("Content-Disposition:", "attachment;filename=" + "\"" + "sample_csv_filename" + ".csv\"");
resp.setBufferSize(BUFFER);
StringBuilder str = new StringBuilder();
String comma = "";
List<HashMap<String,Object>> tempList = //method call to get from database
for(HashMap<String,Object> mp : tempList){comma = "";
str.append("\n");
for(String attr://Array of key values in HashMap){
str.append(comma);
comma = ",";
str.append((String)mp.get(attr));
}
}
byte bytes[] = str.toString().getBytes();
resp.getOutputStream().write(bytes);
resp.setContentLength(bytes.length);
}
我对此实施有以下问题: