English 中文(简体)
make document available for download through java/servlet
原标题:

I need to know if there is a way in java/servlet to make documents(doc,pdf) stored in database available for download to users in requested way(please see below),

for example there is a web page and the link for document in it

right now it is done this way: if the user clicks that link than a new blank window opens and there the download dialog box is shown and the user is able to download the document but that blank window stays open and the user have to close it manually

but wish to do it this way: If the User clicks that link than directly staying on that page a download dialog box should show up asking them to save the file

a servlet url handles the download of the document which is responsible for extracting the doc form database and makes available for download to users

thank you for your time and effort

最佳回答

You need to add following headers in your servlet to make it a downloadable content so browsers don t try to display it,

String value = "attachment;filename="" + URLEncoder.encode(filename, "UTF-8") + " ;
response.setHeader("Content-Disposition", value);
response.setHeader("Content-Transfer-Encoding", "binary");

The filename is proposed filename and user can change it.

问题回答

I wonder if your link html doesn t have something like:

<a href="/foo" **target="_blank"** ....>download</href>

Otherwise, it should work as you want.

This is a bug in IE which depends on several things, the content type is one of them. We had the same problem a few years ago but I don t remember the correct solution anymore, only that we struggled with this for quite some time. Try this:

  • Use the correct content type (application/pdf)
  • If that doesn t work, use a wrong file type (like application/octet-stream) which should tell IE to leave the file alone. You may have problems with the file extension, though.
  • Send or don t send the correct file size
  • Check which chunking mode you re using.

One of these things made IE behave. Good luck.

You need to remove target="_blank" from your <a> element.

Edit: as per your comments: you need to set Content-Disposition header to attachment. You can find here examples of a simple fileservlet and an advanced fileservlet to get some insights.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签