views:

197

answers:

2

I want to know how can I download any file from JSP page based on content disposition as an attachment from mail server.

I want to create a link on JSP page, and by clicking on that link user can download file from mail server. The link should be for content dispostion's attachment type. How can I do that in JSP?

A: 

I suggest you break this question down a bit.

Do you know how to access the attachments from within a regular java program? How to interface with the mail-server etc? If you know that, it should be an easy exercise to provide the attachment in a downloadable format through jsp. Although, I would strongly recommend you to do a regular servlet, since you would probably not have much use of the extra machinery around jsp.

Just make sure you set the content type according to what's being downloaded:

In jsp: <%@page contentType="image/png" %>

In a servelt: response.setContentType("image/png");

aioobe
+1  A: 

Don't use a JSP for this, it's recipe for trouble if use it to stream binary files. It's a view technology, all you need to do is to just place a HTML link like <a href="fileservlet/file.ext"> in. Use a servlet class to do all the processing and streaming task. To set a response header, just use HttpServletResposne#setHeader().

response.setHeader("Content-Disposition", "attachment;filename=name.ext");

You can find here a basic servlet example which does exactly this: FileServlet.

BalusC