• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

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
Author and ninkuma
Posts: 66424
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read this.
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!