0

I have a java code to upload a image in local drive . now I want when a user upload a image it should we upload on remote location how can i achive it ...

 try {

        String UPLOAD_DIRECTORY = "E:\\uploaded";  

       File uploadsFolder = new File(UPLOAD_DIRECTORY);

       String fileName = item.getName();

       if (fileName != null) {
        fileName = FilenameUtils. getName(fileName); // uploaded file filename
       }

       File uploadedFile = new File(uploadsFolder, fileName);
       item.write(uploadedFile);

       /// Save a list with the received files
       receivedFiles.put(item.getFieldName(), uploadedFile);
       receivedContentTypes.put(item.getFieldName(), item.getContentType());

       /// Send a customized message to the client.
       response += uploadedFile;

      } catch (Exception e) {
       throw new UploadActionException(e);
      }

1 Answers1

1

This is server side, it's not related to GWT. You must do a multiform post through an http client.

here's a short example Java Http Client to upload file over POST

and here an official one https://hc.apache.org/httpcomponents-client-ga/httpmime/examples/org/apache/http/examples/entity/mime/ClientMultipartFormPost.java

Community
  • 1
  • 1
Zied Hamdi
  • 1,924
  • 1
  • 16
  • 30
  • thanks form your response but how can i give my remote location url with specifi folder name –  Jun 09 '14 at 08:35
  • 1
    You have to create a servlet on the remote machine to accept your post request anyway. So you can pass it arguments to tell it where to put the file. Alternatively you can use ftp (which I don't recommend since it will tie your code to the remote structure) – Zied Hamdi Jun 09 '14 at 08:53
  • @mark check [this](http://stackoverflow.com/questions/23885042/get-file-extension-from-uploaded-file/23885816#23885816) answer. I also uploaded client images to my server side with GWT but I used client side with JQuery filupload. – Cataclysm Jun 09 '14 at 09:32
  • Note also you can have batch ftp copying from your GWT server to the other one (there are tools ready for that, you don't need to program it). Another solution If you don't need the file locally, you can forward the HttpRequest to the file hosting server (search with keywords "servlet forward" on google for more) – Zied Hamdi Jun 09 '14 at 12:16
  • sorry but what is the mean it (Note also you can have batch ftp copying from your GWT server to the other one (there are tools ready for that, you don't need to program it)). would you like to explain some more.. –  Jun 10 '14 at 05:27
  • You can have the place where you want your files to be checking you GWT server that will also be an FTP server. From the file host server you can request for new files periodically like this: http://stackoverflow.com/questions/5064408/copy-files-from-ftp-server-to-local-directory – Zied Hamdi Jun 10 '14 at 10:46
  • Inversly, you can have your ftp server on the file hosting machine, and send files via ftp through GWT on each new event (file upload). It's up to you to decide which strategy is better, this latter can be contacting the ftp server too often – Zied Hamdi Jun 10 '14 at 10:49