2016-04-14 10 views
1

現在、SQLテーブルのすべてのデータを取得してユーザーのダウンロード用にCSVファイルに保存するために、以下のJavaクラスにファイルのダウンロードプロセスがあります。しかし、ファイルをダウンロードすると、ランダムな点でカットされる点を除いて、すべてのデータが良好です(少なくとも100行以上のデータがある場合、通常はデータの20行目付近)。私は尋ねたい、カットオフを作る場合はどうしますか?それはセッション時間に関連しているのですか?Eclipse CSVダウンロードでデータが削除されます

public String processFileDownload() { 

    DataBaseBean ckear = new DataBaseBean(); 
    ckear.clearContens(); 
    FacesContext fc = FacesContext.getCurrentInstance(); 
    ExternalContext ec = fc.getExternalContext(); 
    Map<String, Object> m = fc.getExternalContext().getSessionMap(); 
    dbase = (DbaseBean) m.get("dbaseBean"); 
    message = (MessageBean) m.get("messageBean"); 
    dataBean = (DataBean) m.get("dataBean"); 
    dbmsUser = (DbmsUserBean) m.get("dbmsUserBean"); 
    FileOutputStream fos = null; 

    String path = fc.getExternalContext().getRealPath("/temp"); 
    String tableName = dbmsUser.getTableName(); 
    String fileNameBase = tableName + ".csv"; 
    java.net.URL check = getClass().getClassLoader().getResource(
      "config.properties"); 
    File check2 = new File(check.getPath()); 
    path = check2.getParent(); 
    String fileName = path + "/" + dbmsUser.getUserName() + "_" 
      + fileNameBase; 

    File f = new File(fileName); 
    try { 
     f.createNewFile(); 
    } catch (IOException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
    dbase.connect(); 
    dbase.setQueryType("SELECT"); 
    dbase.executeSQL("select * from " + tableName); 
    if (dbase.getResultSet() == null) { 
     FacesContext.getCurrentInstance().addMessage("myForm3:errmess", 
       new FacesMessage("Table doesn't exist!")); 
     return "failed"; 
    } 
    Result result = ResultSupport.toResult(dbase.getResultSet()); 
    downlaodedrows = result.getRowCount(); 
    Object[][] sData = result.getRowsByIndex(); 
    String columnNames[] = result.getColumnNames(); 
    StringBuffer sb = new StringBuffer(); 
    try { 
     fos = new FileOutputStream(fileName); 

     for (int i = 0; i < columnNames.length; i++) { 
      sb.append(columnNames[i].toString() + ","); 
     } 

     sb.append("\n"); 

     fos.write(sb.toString().getBytes()); 

     for (int i = 0; i < sData.length; i++) { 
      sb = new StringBuffer(); 
      for (int j = 0; j < sData[0].length; j++) { 
       sb.append(sData[i][j].toString() + ","); 
      } 

      sb.append("\n"); 
      fos.write(sb.toString().getBytes()); 
     } 

     fos.flush(); 
     fos.close(); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    String mimeType = ec.getMimeType(fileName); 
    FileInputStream in = null; 
    byte b; 

    ec.responseReset(); 
    ec.setResponseContentType(mimeType); 
    ec.setResponseContentLength((int) f.length()); 
    ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" 
      + fileNameBase + "\""); 

    try { 
     in = new FileInputStream(f); 
     OutputStream output = ec.getResponseOutputStream(); 
     while (true) { 
      b = (byte) in.read(); 
      if (b < 0) 
       break; 
      output.write(b); 
     } 
    } catch (NullPointerException e) { 
     fc.responseComplete(); 
     return "SUCCESS"; 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } finally { 
     try { 
      in.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    fc.responseComplete(); 
    return "SUCCESS"; 

} 

答えて

0

問題は、あなたは、単に値の間にカンマを追加していると、それはおそらくあなたが書いている値の一つだがない場合はCSV形式を「壊す」になる区切り、行区切りまたは引用符文字が含まれていることのようです正しくエスケープされました。

これは、CSVライブラリを使用する方が簡単で高速になります。 uniVocity-parsersには、結果セットを適切にフォーマットされたCSVにダンプするための事前構築ルーチンが付属しています。あなたのケースでは、あなたは次のようにこのライブラリを使用することができます。

ResultSet resultSet = dbase.getResultSet(); 

    // Configure the output format as needed before actually dumping the data: 
    CsvWriterSettings writerSettings = new CsvWriterSettings(); //many settings here, check the tutorials & examples. 
    writerSettings.getFormat().setLineSeparator("\n"); 
    writerSettings.setHeaderWritingEnabled(true); // we want the column names to be printed out as well. 

    // Then create a routines object: 
    CsvRoutines routines = new CsvRoutines(writerSettings); 

    // The write() method takes care of everything. The resultSet and any other resources required are closed by the routine. 
    routines.write(resultSet, new File(fileName), "UTF-8"); 

希望これは

免責事項を支援します。私は、このライブラリの作者です。オープンソースで無料(Apache 2.0ライセンス)

+0

お返事ありがとうございます。しかし、フレームワークなしで「UTF-8」を追加する方法があるかどうかは分かりますか?限られたリソースを持つ特定のプロジェクトであるため、私はそれを使用できません。 – jni4

+0

うれしい私は助けることができます。 「UTF-8」の部分を省略し、代わりに「routines.write(resultSet、new File(fileName))」と書いてください。 FileOutputStreamまたはjava.io.Writerの任意のインスタンスを使用することもできます。 –

関連する問題