0

I am working on an application that uses HTTP requests to send data from one server to another. Everything work just fine for strings, but I don't know how to send a file (file upload from one server to another).

I've looked over some examples, but I also need to be able to send a string (a file ID) along with the file.

The request is send from a Java class in the POST Method of that class. Is is like this: Client sends upload request for a file with an ID to a storage server. That storage server then uploads that file to another storage server...so the POST request from the first server to the other is send from the POST method method of that server.

Any sample code or link in the right direction are greatly appreciated.

Petre Popescu
  • 1,950
  • 3
  • 24
  • 38
  • Take a look at http://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet – Grim Mar 20 '13 at 19:11
  • That was done via a form in a webpage. I don't have a form or anything like that. – Petre Popescu Mar 20 '13 at 19:15
  • Are you needing client side, server side, or both? – Moesio Mar 20 '13 at 19:22
  • You can use the HttpClient to transfer the file using the post request – Raunak Agarwal Mar 20 '13 at 19:32
  • @Pazvanti sorry i tought you meant how to process the upload server side - for client side you might probably want to look at [Apache Commons HttpClient](http://hc.apache.org/httpcomponents-client-ga/index.html) - try looking at the [examples](http://hc.apache.org/httpcomponents-client-ga/examples.html) - and in particular the multipart entity one (the last) which should do something similar to what you're looking for. – Grim Mar 20 '13 at 19:40
  • You forgot to tell which API/class you're currently using to send the HTTP request. Now we can't answer how to use that API/class to send the request in `multipart/form-data` format. Is it `java.net.URLConnection`? – BalusC Mar 20 '13 at 20:17

3 Answers3

0

Found this tutorial of how you can send the multi-part data using HttpClient. Take a look

Raunak Agarwal
  • 7,117
  • 6
  • 38
  • 62
0

The Commons FileUpload package makes it easy to add robust, high-performance, file upload capability to your servlets and web applications.

FileUpload parses HTTP requests which conform to RFC 1867, "Form-based File Upload in HTML". That is, if an HTTP request is submitted using the POST method, and with a content type of "multipart/form-data", then FileUpload can parse that request, and make the results available in a manner easily used by the caller, mentioned here.

See this link can help you more.

Since you said -

"I also need to be able to send a string (a file ID) along with the file"

You will have to parse the Http request and check if the FileItem is a form field (string/text - file ID in your case) and process it accordingly.

Here is the sample code for multipart content requests -

boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        FileItemFactory factory = new DiskFileItemFactory();
        FileItem item=null;
        ServletFileUpload servletFileUpload = new ServletFileUpload(factory);
        servletFileUpload.setSizeMax(-1);

        List items =null;
        if (isMultipart) {  
            try 
            {
                items = servletFileUpload.parseRequest(request);
                Iterator iter = items.iterator();
                while (iter.hasNext()) {
                    item = (FileItem) iter.next();                  
                    if (item.isFormField()) 
                    {
//get your file Id from element to match with item.getFieldName() and do whatever you want
}
else if ( !item.isFormField() ){
//save your file here
}

Hope that helps you.

Mohsin
  • 852
  • 8
  • 28
  • @NickJ - I have already mentioned in my answer (link to commons-fileupload) which actually says it is described there ;-) rest of it I have tried to explain on my own and I really hope it would be helpful to him. – Mohsin Mar 21 '13 at 13:19
  • Hi! Sorry for my late reply. I will look into the method you described. at first look it seems to be what I wanted. I will keep up to you :) – Petre Popescu Mar 24 '13 at 09:27
  • I looked over the code and the pages you indicated. But I could not find how to form the request that i sent. The code you send may be very good, but sadly I can't figure out how to form the request from servletA (that send the file). – Petre Popescu Mar 24 '13 at 12:10
  • It is difficult to predict how you are sending the request by uploading file from post method of one "storage server" to another "storage server". Can you post your code for better responses ? – Mohsin Mar 25 '13 at 21:46
0

Here is the solution I managed to implement and it works: http://www.dreamincode.net/forums/topic/316513-upload-file-from-one-servlet-to-another/

Petre Popescu
  • 1,950
  • 3
  • 24
  • 38