Loading
View RSS Feed

Angad Kumar Shukla's blog

Upload and Download Code in J2EE

Rating: 10 votes, 4.60 average.
MainPage.html

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<a href="ExecDownload.jsp">Download</a>
	<a href="Upload.jsp">Upload</a>
</body>
</html>


ExecDownload.jsp


Code:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page  import="java.io.FileInputStream" %>
<%@ page  import="java.io.BufferedInputStream"  %>
<%@ page  import="java.io.File"  %>
<%@ page import="java.io.IOException" %>

    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
	String base="e1";
	String parent="e2";   
	String filename="magic.txt";
	//you can  write http://localhost
	String filepath=getServletContext().getInitParameter("Path");

	BufferedInputStream buf=null;
	ServletOutputStream myOut=null;

	try{

			myOut = response.getOutputStream( );
  			File myfile = new File(filepath+filename);
  
  			//set response headers
  			response.setContentType("text/plain");
  
  			response.addHeader("Content-Disposition","attachment; filename="+filename );

  			response.setContentLength( (int) myfile.length( ) );
  
  			FileInputStream input = new FileInputStream(myfile);
  			buf = new BufferedInputStream(input);
  			int readBytes = 0;

  			//read from the file; write to the ServletOutputStream
  			while((readBytes = buf.read( )) != -1)
    			myOut.write(readBytes);

		} catch (IOException ioe){
  
     		throw new ServletException(ioe.getMessage( ));
      
  		} finally {
      
  			//close the input/output streams
   			if (myOut != null)
		          myOut.close( );
       		if (buf != null)
       			buf.close( );
      
  		}


%>
</body>
</html>


Upload.jsp

Code:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
     
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form action="ExecUpload.jsp" enctype="multipart/form-data" method="post">
<p>
Type some text (if you like):<br>
<input type="text" name="textline" size="30">
</p>
<p>
Please specify a file, or a set of files:<br>
<input type="file" name="datafile">
</p>
<div>
<input type="submit" name="submit" value="Send">
</div>
</form>
</body>
</html>
ExecUpload.jsp


Code:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="java.io.*" %>
<%@ page import="javax.servlet.*" %>     
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
	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);
	saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
	saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
	saveFile=getServletContext().getRealPath("/")+saveFile;
	out.print(saveFile);
	System.out.println(saveFile);
	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;

	FileOutputStream fileOut = new FileOutputStream(saveFile);


	//fileOut.write(dataBytes);
	fileOut.write(dataBytes, startPos, (endPos - startPos));
	fileOut.flush();
	fileOut.close();

	out.println("File saved as " +saveFile);

	}

%>
</body>
</html>

You can also download the whole executing project form here: DowmLoad.rar

Submit "Upload and Download Code in J2EE" to Digg Submit "Upload and Download Code in J2EE" to del.icio.us Submit "Upload and Download Code in J2EE" to StumbleUpon Submit "Upload and Download Code in J2EE" to Google

Categories
Java , Web Design

Comments




Disclaimer: Users of techforum4u.com are responsible for ensuring that any material they post (article, blog posts, images or other mulitimedia content) does not violate or infringe upon the copyright, patent, trademark, or any personal or proprietary rights of any third party, and is posted with the permission of the owner of such rights.Anyone who violates these rules may have their access privileges removed without warning.