This is not the only way,But its one of the way to create an application using which you can upload your file into Server.
The idea is very simple,we will create 2 folders in the server "temp1","temp2".
"temp1" will act as a buffer where the files will be stored temporarily,Once its uploaded successfully,The file will then be store on "temp2" permanently.
Steps
*****
1.create following 2 folder in ur c:
c:\temp1
c:\temp2
2.Prepare the directory structure
welcome.html
***********
<center><h1><br><p>Upload Your File</P></h1><hr>
<form action="mypattern" enctype="multipart/form-data" method="post">
<input type="file"name="file1"><br>
<input type="submit" value="upload file"><br>
</form>
web.xml
*******
<web-app>
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>FileUploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/mypattern</url-pattern>
</servlet-mapping>
</web-app>
FileUploadServlet.java
********************************************************************************
import java.io.*;//File,PrintWriter,IOEXception
import java.util.*; //Iterator,List
import javax.servlet.*;//ServletConfig,ServletException
import javax.servlet.http.*;//HttpServlet.HttpServletRequest,HttpServletResponse
import org.apache.commons.fileupload.*; //FileItem,FileUploadException
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class FileUploadServlet extends HttpServlet
{
private static final String TMP_DIR_PATH="c:\\temp1";
private File tmpDir;
private static final String DESTINATION_DIR_PATH="c:\\temp2";
private File destinationDir;
public void init(ServletConfig config)throws ServletException
{
super.init(config);
tmpDir=new File(TMP_DIR_PATH);
if(!tmpDir.isDirectory())
{
System.out.println("A");
throw new ServletException(TMP_DIR_PATH+"is not a directory");
}
System.out.println("Source Directory..."+TMP_DIR_PATH);
destinationDir=new File(DESTINATION_DIR_PATH);
if(!destinationDir.isDirectory())
{
throw new ServletException(DESTINATION_DIR_PATH+"is not a directory");
}
System.out.println("Destination directory..."+DESTINATION_DIR_PATH);
}//INIT()
protected void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
PrintWriter out=response.getWriter();
response.setContentType("text/plain");
out.println("****File UPload status**");
out.println();
DiskFileItemFactory fileItemFactory=new DiskFileItemFactory();
//set the size theshold ,aboutwhich content will be stored on disk
fileItemFactory.setSizeThreshold(1*1024*1024);
//set the temporary directory to store the uplaoded files of sze abouve threshold
fileItemFactory.setRepository(tmpDir);
File file=null;
ServletFileUpload uploadHandler=new ServletFileUpload(fileItemFactory);
try
{
System.out.println("A");
//parse the request
List items=uploadHandler.parseRequest(request); //throws some exception
System.out.println("B");
System.out.println("request items");
System.out.println(items);
Iterator itr=items.iterator();
while(itr.hasNext())
{
FileItem item=(FileItem)itr.next();
if(item.isFormField())
{
out.println("FileName=" +item.getFieldName()+ ",value="+item.getString());
}
else
{
/* String fileName=null;
String s1=item.getName();
int index=s1.lastIndexOf("\\") +1;
String s2=s1.substring(index);
if(s1!=null)fileName=s2;
else fileName="";"*/
System.out.println("0");
String fileName= (item.getName()!=null)? item.getName().substring(item.getName().lastIndexOf("\\")+1):"";
out.println("FileName="+fileName+"\n Content type="+item.getContentType()+"\n FileSeze="+item.getSize());
System.out.println("1");
//write file into ultimate location
if(fileName!=null && fileName.length()>0)
{
file=new File(destinationDir,fileName);
item.write(file);
out.println("Loaded successfully");
}
}
out.close();
}
}//try
catch(FileUploadException ex)
{
ex.printStackTrace();
out.println("Error encountered while parsing the request");
log("Error encountered while parsing the request",ex);
}
catch(Exception ex)
{
System.out.println("3");
out.println("Error encountered while parsing the request");
log("Error encountered while uploading file",ex);
}
}
}
***************************************************************************
Note:Collect those jars from struts software