Hey guys, I'm having a problem with uploading files to a remote server. I just want the visitor to be able to upload a file, and have it go into the uploads directory of my website. My website root is "C:\tomcat\webapps\ROOT", and the uploads directory is at "C:\tomcat\webapps\ROOT\uploads". Both "upload.html" and "upload.jsp" are located in "C:\tomcat\webapps\ROOT". I've been playing around with this for a while and can't get it to work. Can someone please point me in the right direction?

Upload.html:

<html>
<head>
<title>File Upload</title>
<body>
<form action="upload.jsp" method="post" name="uploadform" id="uploadform" enctype="multipart/form-data">  
  <p><strong>Upload file</strong><br>
  	<input type="file" name="uploadfile" size="50"></p>
  <p>&nbsp;&nbsp;&nbsp;<input type="image" name="Submit" value="Upload" src="images/button2.gif"></p>
</form>  
</body>
</html>

Upload.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.*" import="java.io.*"%>

<%
String path=request.getParameter("filename");
String newPath="";
int count=0;

if(path!=null)
{
ArrayList arr=new ArrayList();
StringTokenizer st=new StringTokenizer(path,"\\");
while(st.hasMoreTokens())
{
arr.add(count,st.nextToken());
count++;
}
// create ur own path

newPath="C:\\tomcat\\webapps\\ROOT\\uploads"+arr.get(count-1);
int c;
FileInputStream fis=new FileInputStream(path);
FileOutputStream fos=new FileOutputStream(newPath);
while((c=fis.read())!=-1)
{
fos.write((char)c);
}
}

out.println("Upload Successful!");
out.println("<br>");
out.println("1.File1 Uploaded from :: "+path);
out.println("<br>");
out.println("2.Uploaded File1 is Saved in :: "+newPath);
%>

Anyone? I really need some help on this. I've searched through four pages of JSP file upload threads and haven't found a single consummate solution in any of them.

hi

I also wanted to do the same thing ,and not being able to fix code i went for quick option (for now) which is to go to servletsuite.com and get uploadservlet ,which is then just a case of sticking jar file in WEB-INF/lib and putting a couple of lines of servlet name and servlet mapping into web.xml . i have tried on tomcat7 and works well

also if you can access o.reilly java servlet & jsp cookbook it also has some code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.