• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Download File using JSP Code

 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
%>
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Stop it! You're embarassing me! And you are embarrassing this tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic