Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return String in DataHandler

I've created a web service in Java that returns a DataHandler. This has to be able to return a File, which works fine. But it should also be able to return a String. Any idea how I can transfer a String with a DataHandler?

like image 374
dumazy Avatar asked Mar 05 '13 10:03

dumazy


1 Answers

JavaMail has a ByteArrayDataSource that you can use for this purpose:

DataSource ds = new ByteArrayDataSource(theString, "text/plain; charset=UTF-8");
DataHandler handler = new DataHandler(ds);

The charset in the mime type determines what encoding it will use to convert the string to bytes.

like image 118
Ian Roberts Avatar answered Sep 30 '22 17:09

Ian Roberts