-2

I have an upload program which uploads file and saves it to the specified host on which this application is deployed.

I want to save this files to other host. Can someone guide me what changes I must do to my dispatcher servlet for the same or any other alternate?

Michaël
  • 3,679
  • 7
  • 39
  • 64
Angel491
  • 17
  • 7
  • 2
    Can you show us some code and describe what you have tried? – Keppil Oct 19 '12 at 14:26
  • This question is far too broad! What exactly is your 'dispatcher servlet'? How do you want to connect to the remote host (HTTP, FTP, CIFS, etc.)? – home Oct 19 '12 at 14:40

1 Answers1

-1

I Would advice to open a new connection with the other host within the Servlet and upload the file using the connection.

 int contentChar = null;
 FileInputStream inputStream = new FileInputStream(getfileRootDir()+ "/"+fileId));

 URL oracle = new URL("other server url");
 HttpURLConnection connection = (HttpURLConnection)url.openConnection();
 OutputStream outPutStream = connection.getOutputStrea()
 while ((contentChar = inputStream.read()) != -1) {
      outPutStream.write(contentLine );
 }
 inputStream .close();
 outPutStream.close(); 
 connection.close();

If you want to use BufferredReader/Writer from efficiency perspective, you may want to write as below:

   String contentLine = null;
   BufferedReader reader = new BufferedReader(
                                    new FileReader(getfileRootDir()+ "/"+fileId));

   URL oracle = new URL("other server url");
   HttpURLConnection connection = (HttpURLConnection)url.openConnection();
   OutputStream outPutStream = connection.getOutputStrea()
   Writer streamWriter = new BufferedWriter(new OutputStreamWriter(outPutStream ));
   while ((contentLine = reader.readLine()) != null) {
        streamWriter.write(contentLine );
   }
   reader.close();       
   streamWriter.close();
   outPutStream .close(); 
   connection.close();
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
  • You are doing this with one simple java code. This can be run stand alone as well. Don't see any issue. – Yogendra Singh Oct 19 '12 at 14:37
  • @artbristol : Is it not that character encoding is taken care with the content being passed to write? – Yogendra Singh Oct 19 '12 at 14:41
  • /test/upload/files ../files -------------------------------------------------------------------This is what i am using now..i.e. path in dispatcher servlet – Angel491 Oct 19 '12 at 14:44
  • There is no need to use a `Writer` here. Just write the bytes of the file directly to the `OutputStream`. – artbristol Oct 19 '12 at 14:45
  • abcd.xxx.com:8080/test/upload/files ../files ---------------- here if u see abcd.xxx.com:8080/ ... i need my files to be uploaded at other host which is abcd...and this is what i assumed it should be.. as i want my files to be uploaded to other host.. – Angel491 Oct 19 '12 at 14:45
  • I am saying, do not dispatch your request to the other host. Keep it within your current host servlet. Write the above code to establish the connection and upload the file. – Yogendra Singh Oct 19 '12 at 14:47
  • @artbristol: I agree that `BufferredWriter` is not mandatory but its added from efficiency perspective. – Yogendra Singh Oct 19 '12 at 14:49
  • Yes. In one of the classes in the request processing path of current host. – Yogendra Singh Oct 19 '12 at 14:51
  • in controller class iam using File file1=new File(getfileRootDir()+ "/"+fileId); – Angel491 Oct 19 '12 at 14:52
  • @YogendraSingh using a `Writer` on a byte stream will mangle the results, sometimes but not always, so the code might appear to work at first. Please, update your answer to remove it. And read http://kunststube.net/encoding/. – artbristol Oct 19 '12 at 14:55
  • I believe this will read the input file. You can use this file `file1` object to write the data through out stream. I am updating the answer with couple additional steps. Please refer, – Yogendra Singh Oct 19 '12 at 14:57
  • @artbristol I updated the answer. If you feel it is good, may you want to remove your -1. – Yogendra Singh Oct 19 '12 at 15:18
  • Thanks for the update but you're still recommending using a `Writer`, which is going to mangle binary files from time to time. Why not suggest using commons-io's `IOUtils.copy`? – artbristol Oct 19 '12 at 15:20
  • @artbristol: Thanks for your inputs. My idea was to share the basic concept as to use the connection from first server to second server and proceed `to answer the real question`. Your valuable inputs regarding character encoding are available on this page, which may help the OP/users. – Yogendra Singh Oct 19 '12 at 15:31
  • hi guys,Thanks for your suggestions .i shared the location of host and local path of attachments.Problem resolved – Angel491 Oct 23 '12 at 09:47
  • @Angel491: Good to know, your problem is resolved. If you think that answer was helpful, please don't forget to accept. – Yogendra Singh Oct 23 '12 at 12:53