hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Daniel Walsh" <daniel.wals...@verizon.net>
Subject File Upload
Date Thu, 13 Feb 2003 05:41:05 GMT
I'm trying to use HttpClient's PutMethod to transfer a file from my client application to the
associated Servlet.  There isn't actually a UI for this application, in the end I want to
automate the process.  I'm not sure if there is a better way of doing a file upload such as
this, but what I've been trying to do uses a couple of requests to the Servlet to implement
the entire file transfer:

1.  First, the client uses a POST method to send the file name (contained in a parameter)
to be transferred to the servlet.

2. The servlet then takes the file name and sets an attribute on the HttpSession object associated
with the client to be the name of the transferring file.

3. The client then uses a PUT method (via HttpClient's PutMethod object) to transfer the file
contents.

4. The servlet reads the contents into a byte array, and extracts the file name from the associated
session object.

5. Finally the servlet creates a file with the extracted name, opens a FileOutputStream and
writes the contents to the newly created file.

I've never actually seen something like this done, but it seemed logical enough...  right
(these words always seem to mean trouble!)?  So, does anyone know what it is that I could
be doing wrong?  The file name comes through fine and the Servlet creates the new file with
the corresponding name without a problem.  The file even ends up with the appropriate size.
 The problem is that when I take a look at the newly created file, it just holds spaces in
place of the original text.

Thanks,

Daniel



I've tried to put together a digest version of what I'm trying to do...

------------------------------------------------------------------------------------------------------------
Client Class:
------------------------------------------------------------------------------------------------------------

public class FileUploadClient
{
    public static void main(String args[])
    {
        try
        {
            java.net.URL serverUrl = new java.net.URL("http://localhost:8081/upload");
            File uploadFile = new File("C:\\Temp\\UploadFile.txt");

            PostMethod post = new PostMethod();
            post.setPath(serverUrl.getPath());
            post.addParameter("file-name", uploadFile.getName());

            HttpClient client = new HttpClient();
            HostConfiguration hostConfig = new HostConfiguration();

            hostConfig.setHost(serverUrl.getHost(), serverUrl.getPort(), serverUrl.getProtocol());
            client.setConnectionTimeout(30000);
            client.setHostConfiguration(hostConfig);

            client.executeMethod(post);

            String fileToUpload = post.getResponseBodyAsString();

            PutMethod put = new PutMethod();
            put.setPath(serverUrl.getPath());
            put.setRequestBody(uploadFile);

            client.executeMethod(put);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            System.out.println("Finished Transfer..");
        }
    }
}


------------------------------------------------------------------------------------------------------------
Servlet Class:
------------------------------------------------------------------------------------------------------------

public class FileUploadServlet extends HttpServlet
{
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException,
ServletException
    {
        HttpSession session = req.getSession(true);
            
        String fileName = req.getParameter("file-name");
        session.setAttribute("file-name", fileName);
            
        PrintWriter out = res.getWriter();
        out.println(fileName);
        out.flush();
        out.close();        
    }
    
    public void doPut(HttpServletRequest req, HttpServletResponse res) throws IOException,
ServletException
    {
        HttpSession session = req.getSession(false);
        String fileName = (String)session.getAttribute("file-name");
        
        File uploadedFile = new File(fileName);
        uploadedFile.createNewFile();
        
        ServletInputStream in = req.getInputStream();
        byte[] reqContent;
        int contentLength = 0;
        
        while(in.read() != -1)
            ++contentLength;
        
        reqContent = new byte[contentLength];
        
        in.read(reqContent);
        
        FileOutputStream fOut = new FileOutputStream(uploadedFile);
        fOut.write(reqContent);
        fOut.flush();
        fOut.close();
    }
}

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message