java web 导出csv文件——文件内容追加

public void wenJianPLCsv(List list, String name) {
        try {
            File file = new File(url + name + ".csv");
            if(!file.exists()){
                System.out.println("创建文件!!!");
                file.createNewFile();
            }
             writeFileContent(file, list);
        } catch (IOException e1) {
            e1.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
 public static boolean writeFileContent(File file,List list) throws IOException{
            Boolean bool = false;
            FileWriter fileWriter = null;
            try {
                fileWriter = new FileWriter(file, true); //应用fileWrite流  
                StringBuffer buffer = new StringBuffer();
                for (int q = 0; q < list.size(); q++) {
                    buffer.append(list.get(q).getId()+",");
                    buffer.append(list.get(q).getTime_point()+",");
                    buffer.append(list.get(q).getAqi()+",");
                    buffer.append(list.get(q).getArea()+",");
                    buffer.append(list.get(q).getPosition_name()+",");
                    buffer.append(list.get(q).getStation_code()+",");
                    buffer.append(list.get(q).getSO2()+",");
                    buffer.append(list.get(q).getNO2()+",");
                    buffer.append(list.get(q).getCO()+",");
                    buffer.append(list.get(q).getO3()+",");
                    buffer.append(list.get(q).getPM2_5()+",");
                    buffer.append(list.get(q).getPM10()+",");
                    buffer.append(list.get(q).getPrimary_pollutant().replaceAll(",", ",")+",");
                    buffer.append(list.get(q).getQuality()+" \r\n");
                }
                bool = true;
                fileWriter.append(buffer);//使用文本追加,不覆盖文件原有内容
                //fileWriter.write(buffer);//write将覆盖文件原有内容
                buffer.delete(0, buffer.length());//删除字符串内容
                System.out.println("buffer.toString():"+buffer.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                if( fileWriter!=null){
                    fileWriter.close();
                }
            }
            return bool;
        }

jsp页面将服务器上的文件下载到本地电脑上

<%@page contentType="text/html;charset=utf-8" %>
<%@page import="com.jspsmart.upload.*" %>
<%@page import="java.io.*"%>
<%@page import="java.io.File"%>
<%@page import="java.net.URLEncoder"%>

<%
     if(request.getAttribute("wenjian")!=null){
            String dizhi=request.getAttribute("wenjian").toString();

             String name = dizhi.split("/")[4];//文件名

                    InputStream ins = new FileInputStream(dizhi);//加

                    BufferedInputStream bins = new BufferedInputStream(ins);//  
                    OutputStream outs = response.getOutputStream();// 
                    BufferedOutputStream bouts = new BufferedOutputStream(outs); 

                    response.reset();
                    response.setContentType("application/x-msdownload"); 
                    response.setHeader(  
                            "Content-Disposition",  
                            "attachment;filename="  
                                    + URLEncoder.encode(name, "UTF-8").replaceAll("\\+", "%20").replaceAll("%28", "\\(").replaceAll("%29", "\\)").replaceAll("%3B", ";").replaceAll("%40", "@").replaceAll("%23", "\\#").replaceAll("%26", "\\&")); 

                    int bytesRead = 0;  
                    byte[] buffer = new byte[8192];  
                    // 
                    while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) {  
                        bouts.write(buffer, 0, bytesRead);  
                    } 

                    bouts.flush();// 

                    out.clear();  
                    out= pageContext.pushBody(); 
                    ins.close();  
                    bins.close();  
                    outs.close();  
                    bouts.close();  
                    File file = new File(dizhi);//加 
                    file.delete();


    } 
%>

你可能感兴趣的:(file导出)