在 FTP 中读取 file.xml

我在 FTP 中读取 .xml 文件时出现问题,出现以下消息:“ftp:\cifpag\FilesNotes\Maxdata_Venda_209016.XML(文件名、目录名或卷标的语法不正确)”,在该代码对已经完成的测试以及每个步骤中发生的情况进行了评论,它遵循以下代码:


公共类 ImportXmlFromFTP {


private static Connection conexao;


private String endereco_ftp; 

private String usuario;

private String senha;

private String caminho_ftp;

private String caminho_local;

private String valor;


public ImportXmlFromFTP(){


}


public void inicia() throws SocketException, IOException, SQLException {


    //DADOS CONEXAO

    this.conexao = ConexaoBancoDeDados.getConexao();  


    String sql = "SELECT daea_endereco_ftp, daea_usuario, daea_senha, daea_caminho_ftp, daea_caminho_local FROM sistema.dados_envio_arquivos";

    PreparedStatement stmtSelect = this.conexao.prepareStatement(sql);

    ResultSet rs = stmtSelect.executeQuery();

    List<DadosEnviaArquivos> listaCliente = new ArrayList<DadosEnviaArquivos>();


    while(rs.next()){

        DadosEnviaArquivos dados = new DadosEnviaArquivos();


        endereco_ftp = rs.getString("daea_endereco_ftp");

        usuario = rs.getString("daea_usuario");

        senha = rs.getString("daea_senha");

        caminho_ftp = rs.getString("daea_caminho_ftp");

        caminho_local = rs.getString("daea_caminho_local");


    }    


拉丁的传说
浏览 306回答 2
2回答

HUX布斯

我能够通过将 XML 文件转换为字符串然后返回为 xml 来执行代码,我希望它对某人有所帮助。public class MyFTPClass {private static FTPFile[] obterArquivosDiretorio(FTPClient ftp, String dirPath) throws IOException {&nbsp; &nbsp; String cwd = ftp.printWorkingDirectory();&nbsp; &nbsp; ftp.changeWorkingDirectory(dirPath);&nbsp; &nbsp; FTPFile[] files = ftp.listFiles();&nbsp; &nbsp; ftp.changeWorkingDirectory(cwd);&nbsp; &nbsp; return files;}public static void main(String args[]) throws SAXException, ParserConfigurationException {&nbsp; &nbsp; // Create an instance of FTPClient&nbsp; &nbsp; FTPClient ftp = new FTPClient();&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; // Establish a connection with the FTP URL&nbsp; &nbsp; &nbsp; &nbsp; ftp.connect("caminho_ftp");&nbsp; &nbsp; &nbsp; &nbsp; // Enter user details : user name and password&nbsp; &nbsp; &nbsp; &nbsp; boolean isSuccess = ftp.login("usuario", "senha");&nbsp; &nbsp; &nbsp; &nbsp; if (isSuccess) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Fetch the list of names of the files. In case of no files an&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // empty array is returned&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String path = "ArquivosNotas";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FTPFile[] listedDirectories = obterArquivosDiretorio(ftp, path);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int countXml = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Iterate on the returned list to obtain name of each file&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (FTPFile file : listedDirectories) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (file.getName().toLowerCase().contains("xml")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Lendo " + countXml + " xml");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InputStream stream = ftp.retrieveFileStream("ArquivosNotas/" + file.getName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // xml vai virar uma string para depois fazer o parse para o document&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StringBuilder sb = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String inline = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ((inline = reader.readLine()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.append(inline);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DocumentBuilder builder = factory.newDocumentBuilder();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // convert string do xml para xml em document&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Document doc = (Document) builder.parse(new ByteArrayInputStream(sb.toString().getBytes()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NodeList listaDeVenda = doc.getElementsByTagName("venda");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int tamanhoDaLista = listaDeVenda.getLength();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NodeList listaDeProdutos = doc.getElementsByTagName("item");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int tamanhoDaListaDeProdutos = listaDeProdutos.getLength();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Qtde itens xml " + tamanhoDaListaDeProdutos);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Finalizado " + countXml + " xml");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; countXml++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stream.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reader.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ftp.completePendingCommand();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ftp.logout();&nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; } finally {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ftp.disconnect();&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java