Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 880
  • Last Modified:

How to write a file to a shared folder in some external system (using IP Address)?

All,

I'm uploading a text file and writing to my hard disk (C:\Documents and Settings\ravurima\Desktop\PDP) somthing like this .

String fileStorePath =   "C:\\Documents and Settings\\ravurima\\Desktop\\ACHFIles\\"+  
    fileName;

Now i need to write the file to a shared folder(with write permission to a particular user) in some other system. The path of the folder in external system is something like this :: \\10.45.132.191\ACHFiles, and the folder is password protected.

How can i write to the ACHFiles directory of external system programatically using Java?. I'm not sure how to connect to that system and write to that directory from Java?.

Code snippest would be halpful..

Thanks,
Ram
0
ramsin112400
Asked:
ramsin112400
  • 4
  • 3
  • 3
  • +1
1 Solution
 
ramsin112400Author Commented:
I'm using this code to write a file to the local hard disk ::

      //      Write the file to the file specified

                OutputStream bos = new FileOutputStream(fileStorePath);
                int bytesRead = 0;
                byte[] buffer = new byte[8192];
                while ((bytesRead = stream.read(buffer, 0, 8192)) != -1)
                {
                      bos.write(buffer, 0, bytesRead);
                }
                bos.close();
0
 
ramsin112400Author Commented:

Here's a problem:

1.I created a local user in my system
2. I created a folder on my desktop and given permissions to write to this local user.
3. Then i mapped this folder to a Drive


I I could upload a text file and write to a folder on the desktop . Now problem is i need to write this shared folder..

 The URL look something like this ..to connect to the shared folder::

\\IP Address\Drive\Document

How can i connect to the shared folder and write the text file to that folder?

Thanks,
Maruthi.
0
 
ramsin112400Author Commented:
My present code which is writing to the hard disk ::
-----------------------------------------------------------

PDPFileUploadForm pdpFileUploadForm = (PDPFileUploadForm) form;
            
//Process the FormFile
FormFile myFile       =       pdpFileUploadForm.getTheFile();
String fileType           =      pdpFileUploadForm.getFileType();
        
try
{
        String contentType             =       myFile.getContentType();
        String fileName                =       myFile.getFileName();
        int fileSize                         =       myFile.getFileSize();
        byte[] fileData                =       myFile.getFileData();
        String fileStorePath             =   "C:\\Documents and Settings\\ravurima\\Desktop\\ACHFIles\\"+ fileName;


         ByteArrayOutputStream baos       =       new ByteArrayOutputStream();
         InputStream stream             =       myFile.getInputStream();
      
                
          //      Write the file to the file specified
          OutputStream bos = new FileOutputStream(urlcon.toString());
          int bytesRead = 0;
          byte[] buffer = new byte[8192];
          while ((bytesRead = stream.read(buffer, 0, 8192)) != -1)
          {
      bos.write(buffer, 0, bytesRead);
          }
          bos.close();
          System.out.println("PDPFileUploadAction >>> execute() >>> After Writin ");



0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Mayank SAssociate Director - Product EngineeringCommented:
You can specify the file-path as:

String filePath = "\\IP Address\SharedFolder\Document.extension" ;

There should not be any issues with it (assuming that SharedFolder is the shared-name of the directory which is supposed to contain the file), as long as you have permissions to write to it.
0
 
hoomanvCommented:
not sure if it is possible just saying "\\IP Address\SharedFolder\Document.extension"
however you can map this address to a drive letter, and access it like any other location in your hard disk
to do it

open My Computer --> Tools --> Map Network Drive ...
enter the path, you can also provide the user/pass if there is any
0
 
Igor BazarnyCommented:
There should not be a problem with UNC path. I've just written a sample to verify it. However you might have problem with network permissions which aren't the same as local file permissions. MS teaches complicated rules of how they combine.

My sample code:
import java.io.*;
public class UNCPath{
      public static void main(String[] args){
            File file = new File(args[0]);
            System.out.println("File: "+file.getPath());
            System.out.println("Exists: "+file.exists());
            System.out.println("Is Dir: "+file.isDirectory());
      }
}
------------------------
It correctly prints network file properties.
0
 
ramsin112400Author Commented:
geeting this exception ::
--------------------------
 IO Exception >>>java.net.MalformedURLException: no protocol: //10.45.132.191/ACH Shared
 java.net.MalformedURLException: no protocol: //10.45.132.191/ACH Shared

-----------------------
whats the protocol i need to give, if i need to connect to a shared folder on my local system or system  in the same netwrork.

can anybody tell me : how the URL should look like :: i tried several ways like this ::

URL u = new URL("//10.45.132.191/ACH Shared")
URL u = new URL("\\\\10.45.132.191\\ACH Shared")

none of them worked..any cles?
0
 
hoomanvCommented:
try
URI u = new URI( "file://///10.45.132.191/ACH Shared");
File f = new File( myURI );
0
 
Igor BazarnyCommented:
Why do you think you need URL at all? If you only need to acceess file on local network, java.io.File will be enough
0
 
Mayank SAssociate Director - Product EngineeringCommented:
How about File f = new File ( "\\\\10.45.132.191\\ACH Shared" ) ;

?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> not sure if it is possible just saying "\\IP Address\SharedFolder\Document.extension"

I think it should.
0
 
hoomanvCommented:
notice: it is URI not URL
URI u = new URI("file://///10.45.132.191/ACH Shared");
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now