Transfer a file using a servlet
I need to transfer a file from a users PC to our application/web server and have that file associate with the record they are editing.
I've begun with the following HTML:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body>
<form method="POST" action="http://localhost:8080/rpswq/UploadTest" name="ProcessAccountForm"">
<input type=file name="filetoload">
<input type="submit">
</form>
</body>
</html>
In my servlet, I can get the file name from the request (req) but I do not know how to proceed. I've tried copying the file from the filename to the destination machin, but the filename is always "C:\somehtin\file.ext" and that does not exist on the server so it errors out.
Can anyone provide some guidance here? I am very lost.
Thanks!
Servlet code:
Code:
import java.io.*;
import com.sprint.pcssbs.rfsa.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class UploadTest extends HttpServlet
{
/******************************************************************************
NAME: doPost
PURPOSE: This is the method that is called by KWAI to run the servlet
ACCEPTS: HttpServletRequest req - the request
HttpServletResponse res - the response
RETURNS: Nothing
REVISIONS:
Ver Date Author Description
---------- ----------- ------------- ---------------------------------------
1.0 20040421 merick01 Initial Creation of Method
******************************************************************************/
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
//Validate that they came from KWAI
HttpSession session = req.getSession(true);
//out.println(req.getParameter("filetoload"));
try
{
moveFile(req.getParameter("filetoload") ,"z:\\testfile.123" );
}
catch(IOException ioe)
{
ioe.printStackTrace();
//System.out.println("Unable to open '" + strFileToLoad + "' for processing");
return;
}
catch(Exception e)
{
e.printStackTrace();
}
out.close();
}//End doPost
/******************************************************************************
NAME: doGet
PURPOSE: This is the method that is called when the user types the url
ACCEPTS: HttpServletRequest req - the request
HttpServletResponse res - the response
RETURNS: Nothing
REVISIONS:
Ver Date Author Description
---------- ----------- ------------- ---------------------------------------
1.0 20040219 merick01 Initial Creation of Method
******************************************************************************/
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
//Send them to the doPost
doPost(req, res);
}//End doGet
private static void moveFile(String localFile, String remoteFile) throws Exception
{
System.out.println("Moving file " + localFile);
try
{
System.out.println("Remote File: " + remoteFile);
System.out.println("Local File: " + localFile);
FileManager.fileMove(localFile, remoteFile);
System.out.println("Done Moving");
}
catch (Exception ex)
{
System.out.println("Error Moving File: " + ex.toString());
throw new Exception("Error Moving File:" + ex.toString());
}
}//End moveFile
}//The End