String fileName="raj.doc";
ServletOutputStream stream=null;
BufferedInputStream buf=null;
stream=res.getOutputStream();
String s1=getServletContext().getRealPath("/web-inf/lib/raj.doc");
File doc=new File(s1);
res.setContentType("application/vnd.ms-word");
res.addHeader("Content-Disposition","attachment;filename= "+fileName);
res.setContentLength((int)doc.length());
FileInputStream input=new FileInputStream(doc);
buf=new BufferedInputStream(input);
int readBytes=0;
while((readBytes=buf.read())!=-1)
stream.write(readBytes);
Give me an example of downloading MS-word file in java. Tell me jar files which are needed.
You don't need any jars if you want to only download the file and not work with it.
Just use this code and replace the URL with the URL of your document. Then you should be able to create a new File and just feed everything you read from the URL in the outputstream of the file.
Related
I have an InputStream which I would like to convert to a PDF, and save that PDF in a directory. Currently, my code is able to convert the InputStream to a PDF and the PDF does show up in the correct directory. However, when I try to open it, the file is damaged.
Here is the current code:
InputStream pAdESStream = signingServiceConnector.getDirectClient().getPAdES(this.statusReader.getStatusResponse().getpAdESUrl());
byte[] buffer = new byte[pAdESStream.available()];
pAdESStream.read(buffer);
File targetFile = new File(System.getProperty("user.dir") + "targetFile2.pdf");
OutputStream outStream = new FileOutputStream(targetFile);
outStream.write(buffer);
Originally, the InputStream was a pAdES-file (https://en.wikipedia.org/wiki/PAdES). However, it should be able to be read as just a regular PDF.
Does anyone know how to convert the InputStream to a PDF, without getting a damaged PDF as a result?
Hello it might be a bit late but you can use PDFBOX api (or itextpdf)
https://www.tutorialkart.com/pdfbox/create-write-text-pdf-file-using-pdfbox/
here is a tuto of the process gl
I have a REST webservice built with Jersey that does OCR (Optical Character Recognition) using Tesseract via the Tess4J Java binding. Now the Tess4J library expects you to send it an image file (png, jpg, tif amongst others), but with Jersey processing I get an InputStream that contains the image.
How do I convert this InputStream to a file type that Tesseract would recognise? I've tried the following:
import org.apache.commons.io.IOUtils;
.....
private static File stream2file (InputStream in) throws IOException {
final File tempFile = File.createTempFile("stream2file", ".tmp");
tempFile.deleteOnExit();
try (FileOutputStream out = new FileOutputStream(tempFile)) {
IOUtils.copy(in, out);
}
return tempFile;
}
But then the Tesseract library throws an exception saying that it doesn't accept the file type I'm sending (Which now in this case is 'tmp'). I've tried changing that little 'tmp' to 'tif' and other supported file types but that just yielded the same results, so I'm obviously missing something here.
So how can I take an InputStream, convert it, and forward it to Tesseract as one of the supported file types that it expects?
The file extension of the temp file has to match that of the original input image file.
Besides File type, Tess4J also accepts BufferedImage as input. Just convert inputstream to it, as follows:
BufferedImage image = ImageIO.read(is);
try (FileOutputStream out = new FileOutputStream(tempFile)). You have got an error at this line.
You should use FileOutputStream (String) not FileOutputStream(File).
So it should be FileOutputStream(tempfile.getName()).
The parameter you pass to the constructor of FileOutputStream is a string that is the path to the real file or the name of the file. It's not a File object.
I have created the zip file (sample.zip) which has some files with no issues. When I open the sample.zip, it contains the file which expected.
I want to put that zip file into http response. Currently I am using the following:
response.setContentType("application/zip");
response.addHeader("Content-Disposition", "attachment; filename="+"sampleZip.zip");
response.setContentLength(2048);
response.setHeader("Set-Cookie", "fileDownload=true; path=/");
FileInputStream fileInputStream = new FileInputStream("sample.zip");
OutputStream responseOutputStream = response.getOutputStream();
int bytes;
while ((bytes = fileInputStream.read()) != -1) {
responseOutputStream.write(bytes);
}
response.flushBuffer();
Now its download the zip file in my browser default download location. But when I open that zip file it showing
Cannot open file: it does not appear to be valid archive
Kindly help me to fix this please.
This code looks good to me. But you are setting a default content length which might be the issue. Create File instanceand use the file.length() method to set the content length ans use the same file for your input stream. Also reading byte by byte is not a good idea. If possible use apache's IOUtils.copy() to copy data from your input stream to the ServletOutputStream.
I tried reading xml from windows path D:/xml/xmlfile.xml
On my webpage there is a browse button. I select file and click submit. I comes to my controller and there is code to read this file.
fileToRead variable has a value = file name. Not full directory path.
InputStream ips = this.getClass().getClassLoader().getResourceAsStream(fileToRead);
InputStreamReader ipsr = new InputStreamReader(ips);
BufferedReader br=new BufferedReader(ipsr);
String line;
while ((line=br.readLine())!=null){
System.out.println(line);
}
This did not read file. Another technique also did not work and I got File not found exception
InputStream ips=new FileInputStream(file);
You can look into this site. There is working code for your problem.
http://www.codejava.net/java-ee/servlet/eclipse-file-upload-servlet-with-apache-common-file-upload
File should be on server if you want to just give file name or relative path. You have to upload file to server then read. Else you have to give full path to read file from windows directory.
I am downloading a file from and ftp, saving it onto my local filesystem and then reading it with createXMLStreamReader. When I try and parse it I get this error:javax.xml.stream.XMLStreamException: ParseError at [row,col]:[124,316]. When I copy and paste it to another file manually everything works fine. I have tried copying the file using this, the file gets copied but I am still getting the same error. I do realize that this is caused because of binary characters before the <xml node but I am not sure on how to get rid of them.
I have no control over what is being copied from the ftp and I am using java 1.7
My code for retrieving the file:
client.connect("ftp.domain.com");
client.login("user", "password");
String filename = assetsPath + "/ftpExport.xml";
fos = new FileOutputStream(filename);
client.retrieveFile("/Export.xml", fos);
My code to create the StreamReader:
inputFactory = XMLInputFactory.newInstance();
File f = new File(Parser.class.getProtectionDomain().getCodeSource().getLocation().getPath());
assetsPath = f.toString()+"/../assets";
xmlReader = inputFactory.createXMLStreamReader(
new FileReader(assetsPath + "/Export.xml"));
don't use a FileReader for reading xml as this can corrupt the xml. use a FileInputStream.