小第有这样一个问题要请教大家。
现在我要从服务器上,写一个流,把数据读到用户的本地以excel形式显示出来。
我用sql查出了数据,写在一个Stringbuffer里面了。
现在想生成一个CSV文件,现在问题是,生成CSV文件,然后怎么才能在客户端下载,这块怎么写?是用FileOutputStream还是用什么别的来做这个操作呢?
现在我要从服务器上,写一个流,把数据读到用户的本地以excel形式显示出来。
我用sql查出了数据,写在一个Stringbuffer里面了。
现在想生成一个CSV文件,现在问题是,生成CSV文件,然后怎么才能在客户端下载,这块怎么写?是用FileOutputStream还是用什么别的来做这个操作呢?
public int downFile(HttpServletResponse response, String filePathName) {
String saveUploadFilePath="d:";
if (filePathName.indexOf("/") != 1 && filePathName.indexOf("\\") != 1) {
filePathName = "/" + filePathName;
}
java.io.File downfile = new java.io.File(fileName);
InputStream is = null;
try {
if (downfile.exists()) {
is = new FileInputStream(downfile);
}
else {
throw new IOException("文件不存在1!");
}
}
catch (IOException ie) {
System.out.println("文件不存在2!" + saveUploadFilePath + ie.getMessage());
return -100;
} long fileLeng = 0;
try {
fileLeng = is.available();
}
catch (IOException ex) {
System.out.println("获得文件长度失败!" + ex.getMessage());
return 1;
}
if (fileLeng >= 0) {
// 向客户端发出附件输出流
String filename = filePathName.substring(filePathName.lastIndexOf("/") + 1,filePathName.length());
//String filename="maillist.txt";
response.setContentType("application/x-msdownload");
try {
response.setHeader("Content-Disposition", "attachment;filename=" + "maillist.txt");
OutputStream os = response.getOutputStream();
int byteSum = 0;
int bytesRead = 0;
byte[] buffer = new byte[10 * 1024];
while ( (bytesRead = is.read(buffer)) != -1) {
byteSum += bytesRead;
os.write(buffer, 0, bytesRead);
}
is.close();
is = null;
}
catch (Exception ex) {
System.out.println("向客户端发出附件输出流出错!" + ex.getMessage());
return 1;
}
}
return 0;
}
设置contentType为"application/octet-stream";
StringBuffer usersb = new StringBuffer();
String username = request.getParameter("username");
String opyear = request.getParameter("opyear");
DBManager dm = new DBManager();
list = dm.searchFromDB(username, opyear); for (int i = 0; i < list.size(); i++) {
md = (Model) list.get(i);
String name = md.getUser_name();
usersb.append(md.getUser_name());
usersb.append(",");
usersb.append(md.getUser_id());
usersb.append(",");
usersb.append(md.getAction_time());
usersb.append(",");
usersb.append(md.getBuy_price());
usersb.append(",");
usersb.append(md.getSell_acount());
usersb.append(",");
usersb.append(md.getSell_price());
usersb.append(",");
usersb.append(md.getDeal_amount());
usersb.append(",");
usersb.append(md.getRelate_tax());
usersb.append(",");
usersb.append(md.getOption_income());
usersb.append("\r\n");
// fos.write(arg0);
System.out.println("######################="+name);
} FileOutputStream fos = new FileOutputStream("a.csv");
fos.write(usersb.toString().getBytes());
fos.close();
System.out.println("!!!!!!!!!!!!!!!!!!!!=" + fos); FileInputStream fileIn = new FileInputStream("a.csv");
response.reset();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition",
"attachment;filename=a.csv");
response.setContentLength(fileIn.available());
ServletOutputStream out = response.getOutputStream();
byte buffer[] = new byte[1024];
while (fileIn.read(buffer) != -1) {
out.write(buffer);
}