By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,394 Members | 1,443 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,394 IT Pros & Developers. It's quick & easy.

how to download a file from a server

P: 3
Hi all,
I am new to this programing.
can any one help me to proceed with my coding - to download a file from a remote location.

i have writtent he following code:

[ jsp ]

Click <a href="/filefunctions/DownLoadServlet?file=C:/uploads/test1.txt">
here</a> to download text1.txt

[ servlet ] :

final ServletOutputStream out = response.getOutputStream();

//Get the URL
// Get the resource to view
String file = request.getPathInfo();
System.out.println("file "+file);
if (file == null) {
out.println("Extra path info was null; should be a resource to view");

// Convert the resource to a URL
URL url = getServletContext().getResource(file);
if (url == null) { // some servers return null if not found
out.println("Resource " + file + " not found");

// Serve the file
InputStream in = url.openStream();
byte[] buf = new byte[4 * 1024]; // 4K buffer
int bytesRead;
while ((bytesRead = != -1) {
out.write(buf, 0, bytesRead);

but it is printing "Extra path info was null; should be a resource to view"

can any one help how to overcome this , and download the file from remote loaction successfully..

please reply as early as possible , i have very less time to complete the coding

thanks in advance

Dec 26 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.