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?


<title>File Upload</title>
<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>


<%@ 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;

ArrayList arr=new ArrayList();
StringTokenizer st=new StringTokenizer(path,"\\");
// create ur own path

int c;
FileInputStream fis=new FileInputStream(path);
FileOutputStream fos=new FileOutputStream(newPath);

out.println("Upload Successful!");
out.println("1.File1 Uploaded from :: "+path);
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.


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 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.