Writing XML Document to InputStream using dom4j

When tring to upload a XML file to AWS S3, I found that it’s not allowed to create a tmp file in server local, so I had to transfer the file as InputStream at first and upload it later.

This is the code of transfering the XML Document instance to InputStream and Byte array:

As dom4j is been used to generate the XML, some classes from which (e.g. Document, XMLWriter) can be found in the code.

Uploading files to AWS S3 By Java SpringBoot

This is a demo of uploading files to AWS S3 using Java SpringBoot.

First, we need to add the dependency of AWS S3:

Then add these configs in application.yml:

The accessKey, secretKey and bucket is nesscessary which can be found in aws’ admin dashboard. Continue reading

Reading files from SpringBoot resources folder

Reading files from a SpringBoot jar is something a little complex.

I had tried to read a file from springboot resources folder using java.io.File. This is the code:

And I got the FileNotFoundException :

Since there is no path like file:/data/www/htdocs/my-app/lib/my-app.jar!/BOOT-INF/class existed in my computer, I got the exception. Continue reading

Package all files in Java resources’ subdirectories

Maybe mvn clean package is one of the easiest operation in java programing. Howerver, I recently encountered a problem of packaging, which took up some of my time.

This was the initial package config in maven pom:

And this is the path of the file I wanted to package and read:

Executing the package command (mvn clean package) and opening the jar with compression software (e.g. WinRAR), the file was expected to locate at BOOT-INFO/class/template. However I could not find it. Though all files in resources folder directly existed, the others in subdirectories was not there. Continue reading

How to make Jackson to avoid Escaping Backslash

As I tried to deserialize a json string from database with jackson, I received error message like this:

The json string is:

It’s the Backslash in the source json string which lead to the error. Continue reading