1.11M Members

How to upload and read file content using JSP?

 
0
 

i have these two separate files:

html form:

<form name="form" action="1.jsp" method="post">

<b>File Upload:</b>
	<input type=file name="seqFile"> 
	<input type=submit name="submit" value="submit" >
	</form>

and JSP file 1.jsp:

<%@ page language="java" import="java.io.*" errorPage="" %>

<%
  
 if(request.getParameter("seqFile")!=null){

//"C:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/ROOT/test/seq.txt"

			String file = request.getParameter("seqFile");
			FileReader testRead;
			testRead = new FileReader(file);
			int tempChar;
			String tempStr = "";
			String seque = "";
			
			
			try {
			do {
			tempChar = testRead.read();
			if (tempChar == -1) {
			break;
			}
			tempStr = tempStr + (char)tempChar;
			} while(true);
			
		seque = tempStr.toString();
			}
			catch (Exception e) {
			out.println("Error Message : " + e.toString());
		}
		finally {
			
		testRead.close();
			}
			
			out.print(seque);
			out.print(file);
}
else
out.print("no file");	
        %>

i want to upload file and print out or read the content of file but something is wrong with my code, it has something to do with the file, like it cannot find the source file.

can someone please help me
plz plz plz
thanks ^^

You
Post:
Start New Discussion
View similar articles that have also been tagged: