Download servlet issue with ie 6 - java

I need to write a download servlet in java to download a file from the web server. I am setting the response parameters as follows:
resp.setContentType( (mimetype != null) ? mimetype : "application/octet-stream");
resp.setContentLength( (int)f.length() );
resp.setHeader( "Content-Disposition",
"attachment; filename=\"" + filename + "\"" );
The code seems to work fine with firefox, chrome and IE7 but with IE6 its adding "[1]" in the middle of the filename. E.g. test[1]_check.txt (instead of test_check.txt). There are no duplicate copies of the file on client side and I'm unable to understand where I'm going wrong. Is there an issue with my response parameters?
Thanks in advance

I think i understand the problem...In creating the filename of the file to be downloaded it is a concatenation of 2 strings such as : test.pdf_check.txt.
Firefox and Chrome download using the same name but IE6 inserts [1] just before the first extension it encounters (.pdf) so I get test[1].pdf_check.txt.
I removed the first extension and it seems to be working fine.

check the Temp folder:
C:\Documents and Settings\YourUserName\Local Settings\Temp
maybe there's a copy of the file from previous downloads

I don't think the problem is in setHeader(). What about the code determining the value of filename? Maybe the value is concatenated?

Related

unwanted behaviour with getParentFile() with uploading files via xp:fileUpload

We are noticing unwanted behaviour with uploading files via xp:fileUpload control. sometimes users get files from other users uploaded.
the files are named the same but the content differes.
I was using:
File correctedFile = new File(tempFile.getParentFile() + File.separator + tempClientFile);
to create a file in Notes document from uploaded file.
I noticed in some other code from others the following was used:
File correctedFile = new File( serverFile.getParentFile().getAbsolutePath() + File.separator + fileName );
Can the lacking of absolutepath can be the cause of file switch?
Ofcourse we have never noticed the occurrence under Testing in our Test environment.
.getAbsolutePath() returns the full path whereas .toString() which is implicit used in your case returns just the abstract path.
Here is a description of the difference.
I use .getAbsolutePath() in my Domino backend code and never experienced the issue you describe.

Save as of pdf in IE doesn't save in the same name as filename [duplicate]

I am outputting a PDF file in a Web browser (IE8) HttpContext.Response.writefile(fileName) and it works great. When I try to save the file, it will give me the name of the ashx handler as a default. I would like to actually pass the real name.
I tried to add header information as follow:
context.Response.AddHeader("content-disposition", "attachment; filename=" + fileInfo.Name);
And it works but I do not want the user to have to choose between open and save, i want the file to open normally and if the user chooses to save it then the dialog would give him/her the default filename.
I tried also:
context.Response.AddHeader("content-disposition", "inline; filename=" + fileInfo.Name);
Or just, like Scott Hanselman suggested in his blog.
context.Response.AddHeader("content-disposition", "filename=" + fileInfo.Name);
None of those work for me. Does anybody have any ideas?
See test cases at http://greenbytes.de/tech/tc2231/#inlwithasciifilenamepdf - it seems that this is simply a missing feature in IE.
I also came across this problem.
What helped me was to also set the contenttype to application/pdf (instead of application/x-pdf, which is outdated)
response.setContentType("application/pdf");
response.setHeader("Content-disposition", "inline; filename=\"Report.pdf\"");
In case of INLINE, it seems that Internet explorer is using the last parameter in the URL to build the filename.
For example if your url is http://doc.com/131231231
IE will prompt you to save the file as 131231231.pdf
If you need a filename for example foo_131231231.pdf
you can hack the IE by using this url: http://doc.com/foo_131231231
You may suffer to change your app a bit to expect such ugly parameter, but at the end your app
will work as you expect.

How do I check for duplicate file from URL before downloading

I have thousands of images in my folder on my computer and I am trying to find out how can I check if the file from given URL is already downloaded. Is is possible somehow?
This only give me size of the file.
URL url = new URL("http://test.com/test.jpg");
url.openConnection().getContentLength();
For duplicate file I use
FileUtils.contentEquals(file1, file2)
Thank you for your answers!
If you have a base URL and store files with the same filenames. You can ask the server if it's worth downloading the image again thanks to the file modification time and the If-Modified-Since HTTP Header.
File f = new File();// the file to download
HttpURLConnection con = (HttpURLConnection) new URL("http://www.test.com/"+f.getName()).openConnection();
// Add the IfModifiedSince HEADER
con.setIfModifiedSince(f.lastModified());
con.setRequestMethod("GET");
con.connect();
if(con.getResponseCode() == 304) {
System.out.println(f+ " : already downloaded");
} else {
// Download the content again and store the image again
}
It will work if the modification time of the local file has been left intact since the first download and if the server supports IfModifiedSince header.
If you don't know how to match the filename and the URL then there is no obvious way to it.
You could do some experiments with a fast HEAD request and extract some relevant informations like :
Content-Length
Last-Modified
ETag
Content-Length + Last-Modified could be a good match.
For ETags if you know how the http server builds the ETag you could try to build it on your side (on all your local files) and use it as a value to compare.
Some info on ETags:
http://bitworking.org/news/150/REST-Tip-Deep-etags-give-you-more-benefits
https://serverfault.com/questions/120538/etag-configuration-with-multiple-apache-servers-or-cdn-how-does-google-do-etag
Unfortunately ETag can be constructed with informations only visible to server (inode number) so it will be impossible for you to rebuild it.
It will certainly be easier/faster to download your files again.
If you don't download the file you can't compare it with another.
Otherwise you can store the content you downloaded in a temp file:
File temp = new File(FileUtils.getTempDirectory(), "temp");
FileUtils.copyURLToFile(url, temp);
then loop through your existing files and call:
FileUtils.contentEquals(temp, existingFile)
In the end you would want either to keep or delete the temp file.
Of course this is not very fast. If you have thousands of files, you could save their hashes in a file and use that instead of FileUtils.contentEquals.

Java: How to use AbsolutePath as a Link

I´m writing some Java and one of my task is to convert the Input/Output in to html. Well everything is going good, I can read some files and get the path of those files. Now I´m trying to convert the path in to a normal link so:
"<a href=" + file.getAbsolutePath() + " target=_parent>"
Works pretty good, but:
One, the explorer show an error that it can´t read the file, example:
a jpg or a word file..
Two, if the path have a blank like: "my picture.jpg"
recognize everything after the blank as a normal text...
can someone give me a Tipp, How to fix that, or I just using the wrong method?
You can use toURI() method that turns the file path into a URI:
file.getAbsoluteFile().toURI().toString()

Complete file name does not appear while downloading via servlet

I have a servlet where I have written code to download a file.
The part of code is as follows :
response.setContentType((mimetype != null) ? mimetype
: "application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename="
+ fileName);
OutputStream os = response.getOutputStream();
try {
//here getFileByte is a method for getting
byte bytes[] = getFileByte(filePath);
os.write(bytes);
}
The above works fine but the only problem is that when the filename contains more than one word then it downloads the file with the first word.
For example:- Filename is "Step by Step.pdf" then the downloaded file
will be "Step.pdf".
I even tried to print the filename before giving it to setHeader method and it was correct. Don't have any clue how to resolve this.Could anyone please check what am I doing wrong here and how to correct this bug?
If your file has spaces in it, it should be enclosed in double quotes:
Content-disposition: attachement; filename="file with spaces.whatever"
Note that double quotes will work equally well for filenames without spaces, so you might as well use them all the time
Have you try to encode your filename ? For exemple, replacing spaces by "%20" character sequence ?

Categories

Resources