2017-03-24 44 views
-1

我編程的都很好。所有文件從ftp服務器上傳到mysql。但我從MySQL服務器下載文件,並試圖打開它,文件損壞異常發生。 我從ftp服務器讀取文件並保存爲byte []數組。並保存。一切正常。但從MySQL服務器下載後不會打開。我再次編程了一個這樣的例子。它保存到本地驅動器。再次發生同樣的錯誤。 請問,有沒有例如「從ftp讀取文件並保存爲字節數組,然後將字節數組保存爲文件(存在)」。如何在ftp服務器中插入文件到數據庫

+1

如果你告訴我們,你寫的代碼,我們只能幫助。 – aUserHimself

+1

'我編程的一切都很好。'然後你的代碼沒有問題。那你爲什麼問一個問題? – sudo

+0

你是否以二進制或ASCII模式從ftp服務器下載它? –

回答

0

這裏是我的Java代碼

public boolean exec_blob(String sqlcmd,List<ArrayList> lst) { 
    try { 
     Class.forName("com.mysql.jdbc.Driver"); 
     Connection conn = DriverManager.getConnection(set.getSqlConnStr(),set.getDbUser(),set.getDbPassword()); 
     PreparedStatement statement = conn.prepareStatement(sqlcmd); 
     for(int i=0;i<lst.size();i++){ 
      if(lst.get(i).get(0).toString().equals("String")){ 
       statement.setString(i+1, lst.get(i).get(1).toString()); 
      } 
      else if(lst.get(i).get(0).toString().equals("Integer")){ 
       statement.setInt(i+1, Integer.parseInt(lst.get(i).get(1).toString())); 
      } 
      else if(lst.get(i).get(0).toString().equals("FileInputStream")){ 
       byte[] data=(byte[])lst.get(i).get(1); 
       ByteArrayInputStream bais = new ByteArrayInputStream(data); 
       File fl=new File("c:\\0\\1.pdf"); 
       FileOutputStream fos=new FileOutputStream(fl); 
       fos.write(data); 
       fos.close(); 
       statement.setBinaryStream(i+1,bais,8*data.length); 
      } 
     } 
     int row = statement.executeUpdate(); 
     if (row > 0) { 
      System.out.println("####\tfile uploaded to DB................."); 
     } 
     statement.close(); 
     conn.close(); 
    } catch (SQLException ex) { 
     ex.printStackTrace(); 
     System.out.println("SQL ERROR:"+ex.toString()); 
     return false; 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
     System.out.println("SQL ERROR:"+ex.toString()); 
     return false; 
    } 
    return true; 
} 

次數1.pdf是不開放

相關問題