您的位置:首页 > 编程语言 > Java开发

Java将数据写入csv文件使用FTP上传

2017-11-13 13:03 2291 查看
 前段时间遇到一个项目,需要些一个备份方案,将数据写入csv文件使用FTP上传,因为以前没遇到过,所以通过看一些资料学习完成,下列提供简单的方案供新手参考。
逻辑:先创建文件,然后将内容写入文件,最后使用FTP上传。直接上代码:

//1.先创建文件

    public static boolean createFile(String path,String fileName,String fileContent,String companyId) throws IOException{

        Boolean boo=false;

        String tempFileName=path+fileName;

        File file=new File(tempFileName);

        //判断文件路径是否存在,不存在创建文件夹

        if(!file.getParentFile().exists()){

            file.getParentFile().mkdirs();

        }

        

        //判断如果文件不存在,则新建,存在 继续往里面写

        if(!file.exists()){

            file.createNewFile();

            boo=writeFileContent(tempFileName, fileContent,fileName,companyId);

        }else{

            boo=writeFileContent(tempFileName, fileContent,fileName,companyId);

        }

        return boo;

      

    }

    //2. 将内容写入文件

    public static boolean writeFileContent(String fileNamePath,String writeContent,String fileName,String companyId) throws IOException{

        boolean boo=false;

        IEmsSchemeMgr iesm=new EmsSchemeMgrImpl();

        //文件内容换行

        String fileC=writeContent+"\r\n"; //new String(writeContent.getBytes(),"UTF-8") +"\r\n";

        String temp="";

        

        FileInputStream fis=null;

        InputStreamReader isr=null;

        BufferedReader br=null;

        FileOutputStream fos=null;

        PrintWriter pw=null;

        try{

        File file=new File(fileNamePath);

        //将文件读入输入流

        fis=new FileInputStream(file);

        isr=new InputStreamReader(fis);//,""

        br=new BufferedReader(isr);

        

        StringBuffer sb=new StringBuffer();

        //文件原有内容

        for (int i = 0; (temp=br.readLine())!=null; i++) {

            sb.append(temp);

            //换行

            sb=sb.append(System.getProperty("line.separator"));

        }

        sb.append(fileC);

        fos = new FileOutputStream(file);

        pw = new PrintWriter(fos);

        pw.write(sb.toString().toCharArray());

        pw.flush();

        boo = true;

        long len=(file.length()/1024)+1; //由于整数运算省略小数部分... 故加1

        //将数据保存在数据库 3.将写入文件的大小,路径,时间,文件名称 ,推送状态  保存在数据库  --放在 写入文件之后

        EmsReserveEntity reserve=new EmsReserveEntity();

        reserve.setFileName(fileName);

        reserve.setFileSize(String.valueOf(len));

        reserve.setCompanyId(companyId);

        iesm.updateFileSize(reserve);

        

        System.out.println("文件的大小:"+len);

        }catch(Exception e){

            e.printStackTrace();

        }finally{

        //关闭流

            if (pw != null) {

                pw.close();

            }

            if (fos != null) {

                fos.close();

            }

            if (br != null) {

                br.close();

            }

            if (isr != null) {

                isr.close();

            }

            if (fis != null) {

                fis.close();

            }

        }

        return boo;

    }
使用FTP上传文件
    /**

    * Description: 向FTP服务器上传文件

    * @Version1.0

    * @param url FTP服务器hostname

    * @param port FTP服务器端口

    * @param username FTP登录账号

    * @param password FTP登录密码

    * @param path FTP服务器保存目录

    * @param filename 上传到FTP服务器上的文件名

    * @param input 输入流

    * @return 成功返回true,否则返回false

    */

    public static boolean uploadFile(String url,int port,String username, String password,

            String path, String filename, InputStream input) {

    boolean success = false;

    FTPClient ftp = new FTPClient();

    try {

        int reply;

        ftp.connect(url, port);//连接FTP服务器

        //如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器

        ftp.login(username, password);//登录

        reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {

            ftp.disconnect();

            return success;

        }

        ftp.changeWorkingDirectory(path);

        ftp.storeFile(filename, input);

        

        input.close();

        ftp.logout();

        success = true;

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

        if (ftp.isConnected()) {

            try {

                ftp.disconnect();

                } catch (IOException ioe) {

            }

        }

        }

        return success;

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: