String output = "qwerty123";
Then how to export that string to
StreamedContent in PrimeFaces ?
you can see the example here : https://www.primefaces.org/showcase/ui/file/download.xhtml?jfwid=e2fc5
In that link, the StreamedContent exported from .jpg collected from resources in Web Pages Directory.
But in my case, I just want to convert the string above output to .txt then stream it in StreamedContent without Files.write();.
Thanks in advance.
You can use a ByteArrayInputStream to stream the contents of your string:
DefaultStreamedContent.builder()
.name("your.txt")
.contentType("text/plain")
.stream(() -> new ByteArrayInputStream("qwerty123".getBytes(StandardCharsets.UTF_8)))
.build();
See also:
How to convert Java String into byte[]?
Can we convert a byte array into an InputStream in Java?
Related
This question already has answers here:
Convert a string representation of a hex dump to a byte array using Java?
(25 answers)
Closed 1 year ago.
I'm working on a Word file manipulator (DOCX format to be specific) and it is working fine but at this phase I'm expected to take a file from SAP software, I take the file in the form of bytes that look something like 504B030414000600080000002100DFA4D26C5A0100002005000013000.
However I try to use this code to read the bytes received, put them in an input stream and open them with Apache POI's functions:
byte[] byteArr = "504B030414000600080000002100DFA4D26C5A01000020050000130008025B436F6E74656E745F54797065735D2E786D6C20A2040228A0000200000000000000".getBytes();
InputStream fis = new ByteArrayInputStream(byteArr);
return new XWPFDocument(OPCPackage.open(fis));
The last line brings me an error that the file gives isn't OOXML.
How to transform my received bytes to something relevant in Java?
Using getBytes is for the String type. Because this is hexadecimal, you will have to use DatatypeConverter.parseHexBinary.
This question has more information, and even more options to choose from:
Convert a string representation of a hex dump to a byte array using Java?
Now, having said that, I have not been able to convert the hex string provided from your question into a good document.
Running this function:
try (final FileOutputStream fos = new FileOutputStream(new File("C:/", "Test Document.docx")))
{
final byte[] b = DatatypeConverter.parseHexBinary(
"504B030414000600080000002100DFA4D26C5A01000020050000130008025B436F6E74656E745F54797065735D2E786D6C20A2040228A0000200000000000000");
fos.write(b);
}
... results in the file below:
The [Content_Types].xml in there is promising (if you open other valid documents with 7-Zip you will see that in the archive). However, I cannot open this file with MS-Office, LibreOffice, or 7-Zip.
If I had to guess, I would say this particular file has become corrupted, or parts of it gone missing.
I'm trying to create an Android app able to upload and download PDF files to a server, I've already included in my code a method able to download a PDF from an URL such as "http://www.aaaa.com/myfile.pdf" and display it by using intent.
But I want to know if there is a way to encode a Pdf into a byte array and viceversa decode a byte array to PDF file (I'm using mongoDB)
Thanks to everyone :)
import java.nio.file.*;
Path pdfFilePath = Paths.get("/file/path/your_file.pdf");
// Read file to byte array
byte[] pdfByteArray = Files.readAllBytes(pdfFilePath );
// Write byte array to file
Files.write(pdfFilePath , pdfByteArray);
Given a string:
String exampleString = "example";
How do I convert it to an InputStream?
Like this:
InputStream stream = new ByteArrayInputStream(exampleString.getBytes(StandardCharsets.UTF_8));
Note that this assumes that you want an InputStream that is a stream of bytes that represent your original string encoded as UTF-8.
For versions of Java less than 7, replace StandardCharsets.UTF_8 with "UTF-8".
I find that using Apache Commons IO makes my life much easier.
String source = "This is the source of my input stream";
InputStream in = org.apache.commons.io.IOUtils.toInputStream(source, "UTF-8");
You may find that the library also offer many other shortcuts to commonly done tasks that you may be able to use in your project.
You could use a StringReader and convert the reader to an input stream using the solution in this other stackoverflow post.
There are two ways we can convert String to InputStream in Java,
Using ByteArrayInputStream
Example :-
String str = "String contents";
InputStream is = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
Using Apache Commons IO
Example:-
String str = "String contents"
InputStream is = IOUtils.toInputStream(str, StandardCharsets.UTF_8);
You can try cactoos for that.
final InputStream input = new InputStreamOf("example");
The object is created with new and not a static method for a reason.
I have to upload a file attachment and send it via an HTTP GET request to a server location. I am using IBM websphere integration designer to implement this functionality.
I have the attachment as a byte array and it has to be converted to an Excel .csv file.
Please help me understand how to write a .csv file using a byte array.
If you have a byte array. Let's say.
byte[] byteArray = ThisIsWhereMyByteArrayIsComingFrom.getArray();
String convertedString = new String(byteArray);
That gets the string value of whatever your byte array is. Then you can either, use a csv library to output whatever it is to a file.
I've tried this approach:
http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800620/How_To_-_Add_plain_text_or_binary_files_to_an_application.html?nodeid=800687&vernum=0
But it's REALLY slow for slightly large text files. Does anyone know of a better way of reading a plain text file that is included in the project? Is there a way to use FileConnection?
Figured it out using a combination of information:
IOUtilities.streamToBytes(is);
Directly on the input stream. So a more complete example would be as follows:
Class classs = Class.forName("com.packagename.stuff.FileDemo");
InputStream is = classs.getResourceAsStream("/test");
byte[] data = IOUtilities.streamToBytes(is);
String result = new String(data);
Deal? Deal.