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

java code to download a file not working..

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

please any one help me..its emergency

Dec 27 '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.