亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java讀取ftp中TXT文件的案例

 更新時間:2020年09月23日 15:45:49   作者:zhang06105586  
這篇文章主要介紹了java讀取ftp中TXT文件的案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

最近在開發(fā)關于java讀取ftp中TXT文件,其中有些坑踩了一下,再次做個記錄

1、讀取文件時我會根據文件名稱去生成數(shù)據庫表,oracle數(shù)據庫對于表名的長度是有限制的,最多30個字符

2、對于多個文件的ftp的讀取,每次獲取文件后再次回去文件的流會為空,即在循環(huán)中多次根據ftp獲取文件的流

當出現(xiàn)這種情況時,需要在循環(huán)時每次開啟和關閉ftp的鏈接即可解決,否則在第二次獲取的時候inputsteam為null

3、讀取txt文件時,如果文件中包含中文,進行讀取時可能會出現(xiàn)亂碼,這是可設置讀取的字符集為UTF-8,如果不行,再試試

GB2312

4、java讀取TXT文件:

InputStreamReader reader = new InputStreamReader(is, "GB2312");
BufferedReader br = new BufferedReader(reader);
String lineTxt = null; //每行數(shù)據
int rowNum = 0;
while ((lineTxt = br.readLine()) != null) {}

補充知識:Java實現(xiàn)從FTP獲取文件下載到本地,并讀取文件中的內容的成功方法

我就廢話不多說了,大家還是直接看代碼吧~

package com.aof.web.servlet;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import javax.jws.WebService;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
@WebService(endpointInterface="com.aof.web.servlet.QualityComplainServices")
public class QualityComplainServicesImpl implements QualityComplainServices {
 //ftp對象
  private FTPClient ftp;
  //需要連接到的ftp端的ip
  private String ip = "10.46.249.7";
  //連接端口,默認21
  private int port = 21;
  //要連接到的ftp端的名字
  private String name = "DKEDI";
  //要連接到的ftp端的對應得密碼
  private String pwd = "P@ssw0rd";  
  
 //調用此方法,輸入對應得ip,端口,要連接到的ftp端的名字,要連接到的ftp端的對應得密碼。連接到ftp對象,并驗證登錄進入fto
  public boolean ftp1() {
    ftp = new FTPClient();
    try {
//     ftp.connect(ip, port);
     if(!ftp.isConnected()){
     ftp.connect(ip, port);
   }
      System.out.println(ftp.login(name, pwd));
//     ftp.setCharset(Charset.forName("UTF-8"));
      ftp.setControlEncoding("UTF-8");
      return true;
    } catch (IOException e) {
      e.printStackTrace();
      return true;
    }
    
  }
  
  public void disconnect() throws Exception {
 if (ftp.isConnected()) {
  ftp.disconnect();
 }
 }
  
 // 下載文件到本地
 public boolean download(FTPFile file) throws Exception {
  boolean result = true;
  // 本地文件路徑
  File f = new File("E:\\crmFiles\\");
  if (!f.exists()) {
  f.getParentFile().mkdirs();
  }
  long lRemoteSize = file.getSize();
  try {// 下載過的不在下載了
  OutputStream out = new FileOutputStream(f);
  if (f.length() >= lRemoteSize) {
   System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~本地已經存在,下載中止");
   out.flush();
   out.close();
  }
  boolean iss = ftp.retrieveFile(file.getName(), out);
  System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~下載成功\r\n");
  out.close();
  } catch (Exception ex) {
  ex.printStackTrace();
  System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~下載失敗\r\n");
  return false;
  }
  return result;
 }
 
 private InputStreamReader read;
 private BufferedReader reader;
 
 private String preRead(String filepath) throws Exception {
 File file = new File(filepath);
 String ordertype = null;
 if (file.isFile() && file.exists()) {
  try {
  read = new InputStreamReader(new FileInputStream(file), "GBK");
  reader = new BufferedReader(read);
  StringBuffer FileContent = new StringBuffer();
  String temp = null;
  while ((temp = reader.readLine()) != null) {
   FileContent.append(temp);
  }
  System.out.println("訂單內容為------------------>>>>> "+FileContent+" <<<<<------------------");
  } catch (FileNotFoundException e) {
  System.out.println("?。。。。。。。。。。。。。。。?!沒有找到合適的訂單信息!?。。。。。。。。。。。。?!");
  e.printStackTrace();
  } finally {
  reader.close();
  read.close();
//  file.delete();
  }
 }
 return ordertype;
 }
  
  public void gmRead(String remote) throws Exception {
   boolean downloadResult = false;
 try {
  ftp.changeWorkingDirectory(remote);
  System.out.println("遠程路徑為*************************"+remote);
  FTPFile[] files = ftp.listFiles(remote); // 通過路徑得到文件
  System.out.println("文件數(shù)量為*************************"+files.length);
  for (int i = 0; i < files.length; i++) {
  FTPFile file = files[i];
  if (file.isFile()) {
   downloadResult = this.download(file);// 下載文件 到本地讀取路徑
   if (downloadResult) {
   String ordertype = this.preRead("E:\\crmFiles\\");
   }
   /*//讀取文件內容,將內容存數(shù)據庫
   InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8");
   BufferedReader br = new BufferedReader(isr);
   String lineTxt = null;
   while ((lineTxt = br.readLine()) != null) {
   lineTxt+=lineTxt;
   }
   System.out.println(lineTxt);
   br.close();*/
  }else{
   System.out.println("************* 文件不存在 ************");
  }
  }
 } catch (Exception e) {
  e.printStackTrace();
 }
 }
  
 @Override
 public String threeDAndEightDReports(String orderNum, String FTPUrl, String FileType) {
 //抱怨單號、FTP地址、3D/8D文件類型
 System.out.println("1-------------"+orderNum);
 System.out.println("2-------------"+FTPUrl);
 System.out.println("3-------------"+FileType);
 if(null != orderNum && null != FTPUrl && null != FileType){
  //連接FTP
  boolean flag = this.ftp1();
  if(flag){
  try {
   //獲取文件、解析文件內容,進庫操作
   this.gmRead(FTPUrl);
   // 關閉連接
   this.disconnect();
  } catch (Exception e) {
   e.printStackTrace();
  }
  }else{
  System.out.println("?。。。。。。。。。。。。。。。?!FTP連接失?。。。。。。。。。。。。。。。。?!");
  }
  
  return "success";
 }else{
  return "fail";
 }
 } 
 
 public static void main(String[] args) {
 QualityComplainServicesImpl q = new QualityComplainServicesImpl();
 q.threeDAndEightDReports("001","/CRMINTERFACE","3D");
 } 
}

以上這篇java讀取ftp中TXT文件的案例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • 一文帶你認識Java中的Object類和深淺拷貝

    一文帶你認識Java中的Object類和深淺拷貝

    任何變成語言中,其實都有淺拷貝和深拷貝的概念,Java 中也不例外,下面這篇文章主要給大家介紹了關于Java中Object類和深淺拷貝的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-04-04
  • SpringSceurity實現(xiàn)短信驗證碼登陸

    SpringSceurity實現(xiàn)短信驗證碼登陸

    這篇文章主要介紹了SpringSceurity實現(xiàn)短信驗證碼登陸,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06
  • Spring boot 添加jsp支持配置詳解

    Spring boot 添加jsp支持配置詳解

    本篇文章主要介紹了Spring boot 添加jsp支持配置詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • SpringSecurity整合jwt權限認證的全流程講解

    SpringSecurity整合jwt權限認證的全流程講解

    這篇文章主要介紹了SpringSecurity整合jwt權限認證的全流程講解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • MyBatis學習教程(八)-Mybatis3.x與Spring4.x整合圖文詳解

    MyBatis學習教程(八)-Mybatis3.x與Spring4.x整合圖文詳解

    這篇文章主要介紹了MyBatis學習教程(八)-Mybatis3.x與Spring4.x整合圖文詳解的相關資料,需要的朋友可以參考下
    2016-05-05
  • 基于Java實現(xiàn)簡單貪吃蛇游戲

    基于Java實現(xiàn)簡單貪吃蛇游戲

    這篇文章主要為大家詳細介紹了基于Java實現(xiàn)簡單貪吃蛇,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • springboot多文件或者文件夾壓縮成zip的方法

    springboot多文件或者文件夾壓縮成zip的方法

    最近碰到個需要下載zip壓縮包的需求,于是我在網上找了下別人寫好的zip工具類,下面通過本文給大家分享springboot多文件或者文件夾壓縮成zip的方法,感興趣的朋友一起看看吧
    2024-07-07
  • 快速解決Hash碰撞沖突的方法小結

    快速解決Hash碰撞沖突的方法小結

    這篇文章主要介紹了快速解決Hash碰撞沖突的方法小結,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • 如何處理@PathVariable中的特殊字符問題

    如何處理@PathVariable中的特殊字符問題

    這篇文章主要介紹了如何處理@PathVariable中的特殊字符問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • java volatile關鍵字作用及使用場景詳解

    java volatile關鍵字作用及使用場景詳解

    在本文里我們給大家分享的是關于java volatile關鍵字作用及使用場景的相關知識點內容,需要的朋友們學習下。
    2019-08-08

最新評論