Get Part or stream of uploading file - java

How to get part or stream of file without using request, just file in a form using hibernate .
Nb : I can't use request.getPart();

The answer is FileInputStream stream = new FileInputStream(file);

Related

how to generate the POI excel file in a specific location?

I have created a java maven programme that generate an excel file using Apache POI API, all is working correctly but when i try to put the report to a specific location doing like this
: FileInputStream inputStream = new FileInputStream(new File("C:\\Users\\C5292600\\Desktop\\report1.xlsx"));
i get the following error :
Exception in thread "main" java.lang.IllegalArgumentException: Invalid char (:) found at index (1) in sheet name 'C:\Users\C5292600\Desktop\repor'
at org.apache.poi.ss.util.WorkbookUtil.validateSheetName(WorkbookUtil.java:151)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.createSheet(XSSFWorkbook.java:873)
at com.occCheckNoData.report.Create.newSheet(Create.java:25)
at com.occCheckNoData.report.mainReport.main(mainReport.java:34)
Is it an existing path? If not use something like File file = new File(yourpath); file.mkdirs(); to create a new path. And to generate the file FileOutputStream fileOut = new FileOutputStream(new File(yourpath + filetype));

Apache Jena saving ttl file

Hi I have created a model with Apache Jena and can output it this way:
model.write(System.out, "Turtle");
Is it possible to save the turtle file as .ttl on disk?
To write your model to a file you just pass an OutputStream instead of System.out, like this:
OutputStream out = new FileOutputStream("output-model.ttl");
RDFDataMgr.write(out, model, Lang.TURTLE);
or:
OutputStream out = new FileOutputStream("output-model.ttl");
model.write(out, Lang.TURTLE);
Don't forget to close your stream once the file is fully written.

open pdf file with sessionAsSigner

I have a database where the user doesn't has access to.
Still I can go to the database and "read" the documents with for example
var db:NotesDatabase = sessionAsSigner.getDatabase("","somedir/some.nsf");
In this database there's a pdf file I would like to open or download. I have the filename and the unid . If the user had acces to the database I could do it with
http(s)://[yourserver]/[application.nsf] /xsp/.ibmmodres/domino/OpenAttachment/ [application.nsf]/[UNID|/$File/[AttachmentName]?Open
How can I do it with sessionAsSigner without putting a $PublicAccess=1 field on the form ?
edit:
the pdf file is stored as attachment in a richtextfield
second edit
I'm trying to use the XSnippet from Naveen and made some changes
The error message I get is : 'OutStream' not found
The code I tried is :
response.reset();
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline; filename=" + zipFileName);
var embeddedObj:NotesEmbeddedObject = null;
var bufferInStream:java.io.BufferedInputStream = null;
var outStream:java.io.OutputStream = response.getOutputStream();
embeddedObj = downloadDocument.getAttachment(fileName);
if (embeddedObj != null) {
bufferInStream = new java.io.BufferedInputStream(embeddedObj.getInputStream());
var bufferLength = bufferInStream.available();
var data = new byte[bufferLength];
bufferInStream.read(data, 0, bufferLength); // Read the attachment data
ON THE NEXT LINE IS THE PROBLEM
OutStream.write(data); // Write attachment into pdf
bufferInStream.close();
embeddedObj.recycle();
}
downloadDocument.recycle();
outStream.flush();
outStream.close();
facesContext.responseComplete();
Create an XAgent (= XPage without rendering) which takes datebase + documentid + filename as URL parameters and delivers the file as response OutputStream.
The URL would be
http(s)://[yourserver]/download.nsf/download.xsp?db=[application.nsf]&unid=[UNID]&attname=[AttachmentName]
for an XAgent download.xsp in a database download.nsf.
The code behind the XAgent runs as sessionAsSigner and is able to read the file even the user itself has no right to access file's database.
Use Eric's blog (+ Java code) as a starting point. Replace "application/json" with "application/pdf" and stream pdf file instead of json data.
As an alternative you can adapt this XSnippet code from Thomas Adrian. Use download() together with grabFile() to write your pdf-File to OutputStream.
Instead of extracting attachment file to path and reading it from there you can stream the attachment right from document to response's OutputStream. Here is an XSnippet from Naveen Maurya as a good example.
If you can get the PDF file as a stream, you should be able to use the OutputStream of the external context's response.
Stephan Wissel has a blog posting about writing out an ODF file so you should be able to cut that up as a starting point.
http://www.wissel.net/blog/d6plinks/SHWL-8248MT
You already have the db so, you will just need to know the UNID of the document.
var doc = db.getDocumentByUNID(unid) 'unid is a supplied param
var itm:RichTextItem = doc.getFirstItem("Body") 'assuming file is in body field
Once you have the itm, you can loop round all of the embeddedObjects and get the pdf file. At this point, I don't know if you can stream it directly or if you have to detach it, but assuming you detach it, you will then use something like this.
File file = new File("path to file");
FileInputStream fileIn = new FileInputStream(file);
Don't forget to clean up the temporarily detached file

FileInputStream vs ClassPathResource vs getResourceAsStream and file integrity

I have a weird problem :
in src/main/resources i have a "template.xlsx" file.
If i do this :
InputStream is = new ClassPathResource("template.xlsx").getInputStream();
Or this :
InputStream is = ClassLoader.getSystemResourceAsStream("template.xlsx");
Or this :
InputStream is = getClass().getResourceAsStream("/template.xlsx");
When i try to create a workbook :
Workbook wb = new XSSFWorkbook(is);
I get this error :
java.util.zip.ZipException: invalid block type
BUT, when i get my file like this :
InputStream is = new FileInputStream("C:/.../src/main/resources/template.xlsx");
It works !
What is wrong ? I can't hardcode the fullpath to the file.
Can someone help me with this ?
Thanks
I had the same issue, you probably have a problem with maven filtering.
This code load the file from source, unfiltered
InputStream is = new FileInputStream("C:/.../src/main/resources/template.xlsx");
This code load the file from the target directory, after maven has filtered the content
InputStream is = getClass().getResourceAsStream("/template.xlsx");
You should not filter binary files like excel and use two mutually exclusive resource sets as described at the bottom of this page maven resources plugin
haven't you try accessing it like
InputStream is = new FileInputStream("/main/resources/template.xlsx");
?

How to print the content of a tar.gz file with Java?

I have to implement an application that permits printing the content of all files within a tar.gz file.
For Example:
if I have three files like this in a folder called testx:
A.txt contains the words "God Save The queen"
B.txt contains the words "Ubi maior, minor cessat"
C.txt.gz is a file compressed with gzip that contain the file c.txt with the words "Hello America!!"
So I compress testx, obtain the compressed tar file: testx.tar.gz.
So with my Java application I would like to print in the console:
"God Save The queen"
"Ubi maior, minor cessat"
"Hello America!!"
I have implemented the ZIP version and it works well, but keeping tar library from apache ant http://commons.apache.org/compress/, I noticed that it is not easy like ZIP java utils.
Could someone help me?
I have started looking on the net to understand how to accomplish my aim, so I have the following code:
GZIPInputStream gzipInputStream=null;
gzipInputStream = new GZIPInputStream( new FileInputStream(fileName));
TarInputStream is = new TarInputStream(gzipInputStream);
TarEntry entryx = null;
while((entryx = is.getNextEntry()) != null) {
if (entryx.isDirectory()) continue;
else {
System.out.println(entryx.getName());
if ( entryx.getName().endsWith("txt.gz")){
is.copyEntryContents(out);
// out is a OutputStream!!
}
}
}
So in the line is.copyEntryContents(out), it is possible to save on a file the stream passing an OutputStream, but I don't want it! In the zip version after keeping the first entry, ZipEntry, we can extract the stream from the compressed root folder, testx.tar.gz, and then create a new ZipInputStream and play with it to obtain the content.
Is it possible to do this with the tar.gz file?
Thanks.
surfing the net, i have encountered an interesting idea at : http://hype-free.blogspot.com/2009/10/using-tarinputstream-from-java.html.
After converting ours TarEntry to Stream, we can adopt the same idea used with Zip Files like:
InputStream tmpIn = new StreamingTarEntry(is, entryx.getSize());
// use BufferedReader to get one line at a time
BufferedReader gzipReader = new BufferedReader(
new InputStreamReader(
new GZIPInputStream(
inputZip )));
while (gzipReader.ready()) { System.out.println(gzipReader.readLine()); }
gzipReader.close();
SO with this code you could print the content of the file testx.tar.gz ^_^
To not have to write to a File you should use a ByteArrayOutputStream and use the public String toString(String charsetName)
with the correct encoding.

Categories

Resources