1

I have a situation where I want to create a inputStream from local machine (let's say directory of file is C:\Users\temp\file.txt) to a remote server that has Windows Server 2008.

How can I make the web app use the local version of the file, because I tried this before compiling into a WAR file.

File f = new File("C:\Users\temp\file.txt");

And when I try using InputStream, it crashes. I believe it is referrencing the server's C:\Users\temp\file.txt, which doesn't exist.

I know this is a "dumb" question, but any suggestions would be immensely helpful.

emochoco
  • 117
  • 1
  • 2
  • 9
  • Depending on what that text file is, you may consider putting it in your resources folder. At any rate, it's a good idea to try to keep web apps environment independent. This way any of your colleagues' machines or various server environments will have no trouble running it – Erich Apr 24 '13 at 19:57

2 Answers2

0

If you're trying to access files stored on one machine from another machine, you'll have to "share" the file somehow. There are a couple of fairly simple ways to do this:

  1. Set up a networked file system. A popular choice is Samba or an S3 bucket mounted as a drive. This has the advantage that all files appear "local" for the purpose of reading the file, so you'd use a FileInputStream like normal.
  2. Share using HTTP. Set up an HTTP server like Apache on the host with the file, copy the file to a web-shared directory, and use a URL or an HTTP client like Apache HTTPComponents Client to access the file by its URL. (You can use this approach with FTP or other services, too.)

Sharing files is a little bit of a hassle at first, but once you have a shared file system or web server set up, sharing lots of files is a cinch.

Community
  • 1
  • 1
sigpwned
  • 6,957
  • 5
  • 28
  • 48
-1

Using per host property files helps in this scenario. Generally you want to contain all differences between environments to your property files.

Check out PropertyPlaceholderConfigurer for handling switching property files.

You'd extend that to handle the {ENV} placeholder and execute it from Spring similar to this

<bean id="propertyConfigurer" class="com.yourcompany.PerHostPropertiesConfigurer">
    <property name="filename" value="appname-{ENV}.ini" />
</bean>

in appname-LOCAL.ini have this

text.filename=C:\Users\temp\file.txt

and in appname-PROD.ini change it to a mounted drive or a different local path, something accessible from the viewpoint of your server like this

text.filename=Z:\Users\temp\file.txt
Erich
  • 2,743
  • 1
  • 24
  • 28
  • From the way he asked the question, I don't think the file is on the same machine. Will that approach access a remote file? – sigpwned Apr 24 '13 at 19:40
  • You can access a remote file if it's on a mounted drive using this technique. If the drive/folder isn't mounted to the server he'll have to do something a little more tricky like FTP, or as you mention in your answer, HTTP/cloud – Erich Apr 24 '13 at 19:42