• Post Reply Bookmark Topic Watch Topic
  • New Topic

Download File using JSP Code

 
Jay Shukla
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone please let me know the code / hint , reference to download file from server using JSP code?

Thanks in Advance.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using JSP? Just place a HTML <a> element with a valid URL to the file in question to represent a download link. If the file isn´t exposed in public, let the URL point to a servlet which streams the file to the response.

Edit: the darn forum engine was parsing the <a> element in the message &%"#$ .. Disabling HTML in this message.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSPs are for generating text, not binary content. If you want to stream a file then you need to use a servlet.
 
Venkat Sadasivam
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is possible. The tips is you shouldn't leave any page break between jsp tags.

For example:
<%@ page import="java.util.*" %><%
response.setContentType("application/pdf");
out.write(byte[]);
out.close();
%>
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65516
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[quote=Venkat Sadasivam]It is possible.[/quote]
It's also possible to drive a screw using a hammer. Doesn't make it a good idea.

Use a servlet not a JSP for such matters.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Venkat Sadasivam wrote:It is possible. The tips is you should leave any page break between jsp tags.

For example:
<%@ page import="java.util.*" %><%
response.setContentType("application/pdf");
out.write(byte[]);
response.completeResponse();
%>

The code at its own makes also no sense. response.completeResponse?
Your favourite site is undoubtely roseindia.com?
 
Venkat Sadasivam
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code at its own makes also no sense. response.completeResponse?

I have edited now.

It's also possible to drive a screw using a hammer. Doesn't make it a good idea.

Yes, it is not good idea you already communicated in the thread. But each and every project has its own challenge and reasoning. There is no harm in giving possible ways.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65516
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see that the code snippet in question has been modified. It doesn't matter. Doing any Java in a JSP, especially something like this, is a bad, bad, and did I mention BAD idea. It will be fragile and error-prone. Just don't do it.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65516
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Venkat Sadasivam wrote:There is no harm in giving possible ways.

Showing code that demonstrates poor practices may lead people, especially novices, to think that such code is ok to emulate. You can expect that the more senior developers will point out when such code is posted.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!