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.

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());
//out.print(dataBytes);
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
//out.println(boundary);
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);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
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

<html>
<head></head>
<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">
    <tr>
      <td align="left">
	  <b><font color="white">Select a file to upload :</b></td>
    </tr>
    <tr>
      <td align="left">
        <input type="file" name="uploadfile" size="50">
        </td>
    </tr>
    <tr>
      <td align="left">
		<input type="hidden" name="todo" value="upload">
        <input type="submit" name="Submit" value="Upload">
        <input type="reset" name="Reset" value="Cancel">
        </td>
    </tr>
  </table>  
</form>
</body>
</html>

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..../

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.*" %>

<%
	
try{

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

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

PathVariable = locationVariable+txtFileNameVariable; 

BufferedReader bufferedReader = null;

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

		}

		catch(FileNotFoundException fnfe){
		
		fnfe.printStackTrace();
		
		}
		
		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 + "\"" );
try{

	int anInt=0;
	
	while((anInt=bufferedReader.read())!=-1)

		out.write(anInt);
		
}catch(IOException ioe){
	ioe.printStackTrace();
}


}catch(Exception e){

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

}


%>

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

This article has been dead for over six months. Start a new discussion instead.