Home > Blockchain >  How to create in memory a xml file from a json object and put it to a ftp adres in java
How to create in memory a xml file from a json object and put it to a ftp adres in java

Time:01-27

I want to put a file to ftp adres automatically in a scheduler. I have a json object so I can create a xml from this. Also I can create a xmlString with a code below. So I want to xml file abc.xml with this xmlString and then put it to a ftp adres. How I can do.



       private static String objToXml(Object object) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(object.getClass());
        Marshaller marshallerObj = context.createMarshaller();
        marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        StringWriter sw = new StringWriter();
        marshallerObj.marshal(object, sw);
        return sw.toString();
    }

    String xmlString = "";
    try {
        xmlString = objToXml(anObject);
        } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }

CodePudding user response:

If I understand your question correctly, you want to store a string to a file on an FTP server.

byte[] bytes = xmlString.getBytes(StandardCharsets.UTF_8);
InputStream inputStream = new ByteArrayInputStream(bytes);
ftpClient.storeFile(remotePath, inputStream);

Having that said, most XML libraries are be able to write directly to an OutputStream, sparing the necessity (and memory waste) of the intermediate String object.

  •  Tags:  
  • Related