简体   繁体   中英

InputStream from local machine to tomcat deployed webapp

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM