Errors in printing - java

To show the printable document directly with out saving that file, I set the response header as "Content-Disposition","inline". Now it is showing the file but some times that file not being opened. It is showing some error messages like
"There was an error in opening this document. The file is damaged and can not be repaired"
"Adobe Reader cannot open this file. It might not be supported file type or that file was damaged".
and If click on print button again it working fine It is showing the document. So what I have to do to avoid those error messages. Can you please suggest me.
Thanks,
Vara Kumar

Check how you build your stream of bytes on server. Those errors indicates truncated stream (=incomplete pdf) or extra data before or after the end of right content (=invalid pdf).

Related

Execution of program stops and no errors are generated

I got this code
// "status" is a JLabel field
status.setText(" Download data ");
url = new URL( baseURL );
FileUtils.copyURLToFile( url, tempFile );
status.setText(" Download done... ");
When code is run, the "status" gets updated with: Download data
The file downloads, and I can see it when I open the "tempFile" in notepad. (tempFile.txt)
The next status update i.e "Download Done", never gets executed, and all the code that follows also doesn't get executed...
The Exception e.printStackTrace() is also empty...
What is goin gon here?
PS, the file downloaded is just a plain text file with about 2000 lines of text in it...
And I can see all of it in the temporary file I created tempFile (tempFile.txt)
I also commented all the code that follows after the second update, but still nothing.
Currently I'm downloading the file from localhost/data.txt
See copyUrlToFile(url,file)
Warning: this method does not set a connection or read timeout and thus might block forever. Use copyURLToFile(URL, File, int, int) with reasonable timeouts to prevent this.
There could be several explanations for why the download doesn't complete. Perhaps there is a step in the protocol that triggers at the end of the transfer that requires the use of a port your system is currently blocking.

Android - Replace a text file from a server with an extended line

I have a text file on a server, now if the user presses a button, the text file should get a new line .
My idea was to create a text file on the device...to read the actual text file of the server...to append 'this new line' and upload it to the server, but this way isn't safe, are there other ways to do this ?
Thanks in advance !!
if the server is a Web Server, you can simply send an Http request (Using HttpUrlConnection for example) from your application to a specific PHP file on the server, and put in this file (PHP file) the code to append a new line to the file or event to append a custom text content which you send it with the request.
No need to read the existing text from the server unless you need them on the device, if you you need is to add a new line of text, you just need to send the text from the device to the server and and append the text to the text file on the server.

java.io.IOException: File Not Found

I have uploaded a MultipartFile which is a video, and am trying to use capture a frame of it using some code. However, when it tries to open the file, it always gives the error java.io.IOException: File Not Found.
First I extract the multipart file to a normal file like so:
File convertedFile = new File(multipartFile.getOriginalFilename());
multipartFile.transferTo(convertedFile);
Then I set the file name of the video to the code to capture the frame like this:
"file://" + convertedFile.getName()
then it fails when it tries to create a URL out of the file name:
new URL(_videoFilename));
How can I make it find the file?
I haven't worked with MultipartFile but you can find this in the Javadoc
The file contents are either stored in memory or temporarily on disk.
In either case, the user is responsible for copying file contents to a
session-level or persistent store as and if desired. The temporary
storages will be cleared at the end of request processing.
Therefore, you certainly have to usetransferTo(File dest) to be able to handle your file on a determined place.

Input Stream from ZipResourceFile talking lot of time to read data

I have successfully implemented Apk Expansion Files for my project.
Problem:In my .obb i have a folder which has 100 xml files in it.Now the problem is i am using the below code to read the data directly from .obb files without extracting the data.
this is code given in the offical doc here http://developer.android.com/google/play/expansion-files.html under the topic Reading from a ZIP file
ZipResourceFile expansionFile = APKExpansionSupport.getAPKExpansionZipFile(MyActivity.this, 1, 0);
String pathToFileInsideZip = "main.1.com.my.expansionfiles.obb/data/" +filename;
InputStream fileStream = expansionFile.getInputStream(pathToFileInsideZip);
i have a for loop in that i am calling writing this code so that it will read all the xml one by one and make the data ready for me to display.
The above will read the data directly from .obb file, but the problem is its talking lot of time to extract the data?
Why so? i am doing any mistake here?
I believed that the pathToFileInsideZip would not be
main.1.com.my.expansionfiles.obb/data/[files].
I think it's just
"data/[files]"

Apache POI HSSF XLS reading error

Using the following code while reading in a .xls file, where s is the file directory:
InputStream input = new FileInputStream(s);
Workbook wbs = new HSSFWorkbook(input);
I get the following error message:
Exception in thread "main" java.io.IOException: Invalid header signature; read 0x0010000000060809, expected 0xE11AB1A1E011CFD0
I need a program that is able to read in either XLSX or XLS, and using the exact same code just adjusted for XSSF it has no problem at all reading in the XLSX file.
The Exception you're getting is one telling you that the file you're supplying isn't a valid Excel binary file, at least not a valid Excel file produced since about 1990. The exception you're getting tells you what POI expects, and that it found something else instead which wasn't a valid .xls file, and wasn't anything else POI can detect.
One thing to be aware of is that Excel opens a wide variety of different file formats, including .csv and .html. It's also not very picky about the file extension, so will happily open a CSV file that has been renamed to a .xls one. However, since renaming a .csv to a .xls doesn't magically change the format, POI still can't open it!
.
From the exception, I can tell what's happening, and I can also tell you're using an ancient version of Apache POI! A header signature of 0x0010000000060809 corresponds to the Excel 4 file format, from about 25 years ago! If you use a more recent version of Apache POI, it'll give you a helpful error message telling you that the file supplied is an old and largely unsupported Excel file. New versions of POI do include the OldExcelExtractor tool which can pull out some information from those ancient formats.
Otherwise, as with all exceptions of this type, try opening the file in Excel and doing a save-as. That will give you an idea of what the file currently is (eg .html saved as .xls, .csv saved as .xls etc), and will also let you re-save it as a proper .xls file for POI to load and work with.
If the file is in xlsx format instead of xls you might get this error. I would try using the generic Workbook object (Also called the SS Usermodel)
Check out the Workbook interface and the WorkbookFactory object. The factory should be able to create a generic Workbook for you out of either xlsx or xls.
I thought I had a good tutorial on this, but I can't seem to find it. I'll keep looking though.
Edit
I found this little tiny snippet from Apache's site about reading and rewriting using the SS Usermodel.
I hope this helps!
Invalid header signature; read 0x342E312D46445025, expected 0xE11AB1A1E011CFD0
Well I got this error when I uploaded corrupted xls/xlsx file(to upload corrupt file I renamed sample.pdf to sample.xls). Add validation like :
Workbook wbs = null;
try {
InputStream input = new FileInputStream(s);
wbs = new HSSFWorkbook(input);
} catch(IOException e) {
// log "file is corrupted", show error message to user
}

Categories