• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Error calling Servlet from java file, while Servlet downloading file from website

Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am trying to write a code that
calls servlet running on the local server from the java class using URL
I used the following code for it.

DataInputStream dis;
try {
String urlString = "http://localhost/Project/downloadServlet?fileName=hello.txt";
URL url = new URL(urlString);


dis = new DataInputStream(url.openConnection().getInputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(dis));

String total = "";
String line = "";
while ((line = in.readLine()) != null) {
total += line;

} catch (Exception e) {

In the URL I am passing the request to servlet to download this file from a particular loaction for ex: "www.ibm.com" and store it into local file system.

My Servlet Code is like this:

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(CONTENT_TYPE); //Set the content type
PrintWriter out = response.getWriter(); //Get the writer to the output stream

System.out.println("FileDownload Servlet fetching file...");
fileName = request.getParameter("fileName");
if ( fileName == null ) {
out.println("ERR: No fileName parameter given.");

try {
System.out.println(" fileName: " + fileName);
URL url = new URL(www.ibm.com + "/"+fileName); //Open the URL

InputStream stream = url.openStream(); //Get the Input Stream for the URL
byte[] bytes = new byte[this.MAX_FILE_SIZE]; //Set max file size
int bytesRead = 0; //Variable for the file size
int b = 0; //Variable to hold the bytes read from the stream

while((b = stream.read()) != -1 && bytesRead <= this.MAX_FILE_SIZE) {
bytes[bytesRead++] = (byte)b; //Put the bytes into the array

//The byte array has a size of 2MB now this has to be trimmed to the
//actual file size before being written on to the filesystem.
byte[] outputBytes = new byte[bytesRead];
// Copy the relevant bytes to the output array
System.arraycopy(bytes, 0, outputBytes, 0, bytesRead);

// Get the path name from the URL. This value is the String
// that follows the hostname ort number
String file = url.getFile();

// Get the file name from the URL path name.
String fileName = file.substring(file.lastIndexOf("/") + 1, file.length());

// Write the bytes to the destination directory
FileOutputStream fos = new FileOutputStream("C:\"+fileName);

// Close the stream

// Output Servlet response
out.println("OK: " + fileName + "<br>");

System.out.println(" " + bytesRead + " bytes read");

} catch(Exception e) {
out.println("ERR: " + e); // For the log
System.out.println(" ERROR: " + e); // For the console
throw new ServletException(e.toString());

However when I run this code it gives me fileNotFoundException (assuming hello.txt is present in the webserver with url www.ibm.com/hello.txt)

Also If I comment the Italicized portion of the code, and uncomment the code written in the quoates, it gives me no error and also doesnot download the file into the local file system.

Please Help
Posts: 67446
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read this.
I child proofed my house but they still get in. Distract them with this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic