i'm able to upload files using jsp into my server.Now i want to download those files with a popup window showing open save or cancel options.can anyone tell me the code for it.

10 Years
Discussion Span
Last Post by cuddlefish

You want to download Files from the server using jsp page ? If You want a file browser in jsp than you should search on google for Browser.jsp created by Taylor Bastien, David Levine, David Cowan
It has all the thing you require. I cant paste its code here because it is too long search for browser.jsp + its authors name u will find it. I hope this have solved your query partially other pop up window stuff yet there.

<!-- upload.jsp -->
<%@ page import="java.io.*" %>
String contentType = request.getContentType();
System.out.println("Content type is :: " +contentType);
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) 
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength) 
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
String file = new String(dataBytes);
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
//out.print("FileName:" + saveFile.toString());
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
//out.print("FileName:" + saveFile.toString());
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
//out.print("FileName:" + saveFile.toString());
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
saveFile = "D:\\java\tomcat5\\webapps\\ROOT\\images\\" + saveFile;
FileOutputStream fileOut = new FileOutputStream(saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));
out.println("File saved as " +saveFile);

this it the jsp code.In this u can only upload .doc & image files.

Html file for this is

<body background="bgcircle.gif">
<form method="post" action="products.jsp" name="upform" enctype="multipart/form-data">
  <table width="60%" border="0" cellspacing="1" cellpadding="1" align="center" class="style1">
      <td align="left">
	  <b><font color="white">Select a file to upload :</b></td>
      <td align="left">
        <input type="file" name="uploadfile" size="50">
      <td align="left">
		<input type="hidden" name="todo" value="upload">
        <input type="submit" name="Submit" value="Upload">
        <input type="reset" name="Reset" value="Cancel">

Hi mahesh i just went through ur code,for downloading you can just save the filepath in the database and display witihn the links wen you click theh link you will get the download dialog box..../

Votes + Comments
don't revive dead threads with scriptkiddie 3227 zp3ak

Hello everyone, thanx for the responses you give to this post. I still have a problem with downloading, when i download the files they are 1kb in size and yet where they are stored in a folder they are the right size...Please help...


This is my code, it is working fine with the downloading but i think problem is with reading and writing the files before and after download...

<%@ page import = "java.sql.*"%>
<%@ page import = "java.util.*" %>
<%@ page import= "java.io.*" %>


String txtFileNameVariable="";
String locationVariable="";
String PathVariable="";

txtFileNameVariable = request.getParameter("fileVariable");
locationVariable = request.getParameter("locationVariable");

PathVariable = locationVariable+txtFileNameVariable; 

BufferedReader bufferedReader = null;

			 bufferedReader = new BufferedReader(new FileReader(PathVariable));


		catch(FileNotFoundException fnfe){
		File f=new File(locationVariable, txtFileNameVariable);
        String fileType = txtFileNameVariable.substring(txtFileNameVariable.indexOf(".")+1,txtFileNameVariable.length());

        if (fileType.trim().equalsIgnoreCase("txt")) {
            response.setContentType( "text/plain" );
        } else if (fileType.trim().equalsIgnoreCase("doc")) {
            response.setContentType( "application/msword" );
        } else if (fileType.trim().equalsIgnoreCase("xls")) {
            response.setContentType( "application/vnd.ms-excel" );
        } else if (fileType.trim().equalsIgnoreCase("pdf")) {
            response.setContentType( "application/pdf" );
        } else {
            response.setContentType( "application/octet-stream" );

String original_filename = txtFileNameVariable;
response.setHeader( "Content-Disposition", "attachment; filename=\"" + original_filename + "\"" );

	int anInt=0;

}catch(IOException ioe){

}catch(Exception e){

out.println("This is the Error " +e.getMessage());



Edited by peter_budo: Keep It Clear - Do use code tags when posting any code

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.