I'm facing performance issue while opening pdf files in browser.It is taking very huge to time to open.Our pdf files are scanned images. So is there any alternative solution for this. Here is my sample code.
response.setContentType("application/pdf");
response.setContentLength((int) file.length());
FileInputStream in = new FileInputStream(file);
OutputStream out = response.getOutputStream();
// Copy the contents of the file to the output stream
byte[] buf = new byte[1024];
int count = 0;
while ((count = in.read(buf)) >= 0) {
out.write(buf, 0, count);
}
in.close();
out.close();
Here i'm rendering outstream to browser.
Related
I have a servlet that queries the bytes of a file, reads and responds to the content.
OutputStream out = response.getOutputStream();
FileInputStream in = new FileInputStream(my_file);
byte[] buffer = new byte[4096];
int length;
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.flush();
I need to know what the extension of the read file is, so I can name the file with the extension correctly.
Searching I found the following:
URLConnection.guessContentTypeFromStream(new ByteArrayInputStream(bytes));
This doesn't support all file types, like pdf for example. I need a more assertive solution
Suppose I have the following code that will download something from my website.
URL website = new URL(url);
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream("something.zip");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
I want to make a JProgressBar that would display the progress of the file that is being downloaded. To achieve this, I know that one way is to get the total size of the file and the currently downloaded bytes of the file, then get the percent. Here is the following code for getting the size of the file:
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("HEAD");
long fileSize = conn.getContentLengthLong();
However, getting the downloaded bytes of FileOutputStream is difficult, because you have to make loop that keeps track of the current bytes that are being downloaded. How would you be able to achieve this then?
Note: The algorithm should be suitable for very large files, as the files I am downloading from the internet are one gigabyte in size.
I provide below the code snippet you can look into it. Basically, you have to calculate the number of bytes and then make a calculation by diving into 100 to make some percentage.
InputStream inputStream = util.getInputStream();
// opens an output stream to save into file
FileOutputStream outputStream = new FileOutputStream(saveFilePath);
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = -1;
long totalBytesRead = 0;
int percentCompleted = 0;
long fileSize = util.getContentLength();
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
totalBytesRead += bytesRead;
percentCompleted = (int) (totalBytesRead * 100 / fileSize);
setProgress(percentCompleted);
For more details, refer this link
i try to download a pdf file from url using the code below. It works fine when the file size is under 8k and if the file is up to 8k, it downloads the pdf file but the file is not readable.
Code
InputStream in = new BufferedInputStream(url.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while (-1!=(n=in.read(buf)))
{
out.write(buf, 0, n);
}
out.close();
in.close();
byte[] response = out.toByteArray();
FileOutputStream fos = new FileOutputStream(new File("C:\\temp\\TEST.pdf"));
fos.write(response);
fos.close();
System.out.println("Download Finished");
You may try using the IOUtils.copy(InputStream input, OutputStream output)
You can minimize the lines of code.
Apache IOUtils
I want to use java(httpurlconnection class) to download a pdf file,but have some thing puzzle,the core code is:
BufferedInputStream inputStream = new BufferedInputStream(httpConn.getInputStream());
OutputStream output = new FileOutputStream("F:/httpclient-tutorial.pdf");
byte[] infoBytes = new byte[BUFFER];
while(inputStream.read(infoBytes) != -1){
output.write(infoBytes, 0, BUFFER);
}
Question:when the BUFFER is 1024,get the pdf file is larger than real one;but the BUFFER is 8,then it's ok. I don't know why it is? Has anyone know about that?
You're unconditionally writing out the whole of the buffer on each iteration. You should only write out as many bytes as you've just read:
int bytesRead;
while((bytesRead = inputStream.read(infoBytes)) != -1){
output.write(infoBytes, 0, bytesRead);
}
Im receiving a file trough this code and the "bos.write" are saving it o to my HDD.
Everything working good.
Since im sending the file in a few second i thought i could store the file in memory
instead of HDD.
Now how do i do this?
File path = new File("C://anabella//test1.txt");
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path));
int size = 1024;
int val = 0;
byte[] buffer = new byte[1024];
while (fileSize >0) {
val = in.read(buffer, 0, size);
bos.write(buffer, 0, val);
fileSize -= val;
if (fileSize < size)
size = (int) fileSize;
}
Presumably bos is a FileOutputStream? To use an in-memory buffer use a ByteArrayOutputStream instead.
If you know the size in advance you don't even need a ByteArrayOutputStream
InputStream is = socket.getInputStream(); // or where ever the inputstream comes from.
DataInputStream in = new DataInputStream(is);
byte[] bytes = new byte[fileSize];
in.readFully(bytes);
to send the bytes to any OutputStream like
OutputStream os = ...
os.write(bytes);
The bytes will contain the contents of the file.