• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to overwrite a file in server

 
jasmine tou
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i want a code on how to overwrite a file on server that is already exits in server with the same name and i want to keep the backup for that old file and overwrite new file please can you help me out for this and i want to maintain archive_backup : <old Directory path >_at what time_file name all these should come in table and I'm doing this on struts and tiles applications please help me out
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To interact with the file system you can use the classes in the java.io package.
 
Tim Holloway
Bartender
Posts: 18417
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, Jasmine!

First, I remind everyone that a web server is not a file server (I do this a lot!). Meaning that a web client (browser) cannot itself write any file to the server. All it can do is upload a file and let the web application find something to do with the data. Which may or may not be to save the data in a file.

Now that that's out of the way, the answer is the same as any other (non-server) Java application. The usual best practice is to write the new file under a temporary name, rename/move the older file, then rename/move the new file to appear under the old file's path/name. In case of failure, reverse the steps as needed.
 
Mohammad AbuShady
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what about writing an ftp code?
 
Shiva Kumar V
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the server is windows you can use share the folder and give write access to your system.
Then use "\\" instead of "\" example:"\\\\IP\\foldername"
 
Tim Holloway
Bartender
Posts: 18417
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shiva Kumar V wrote:If the server is windows you can use share the folder and give write access to your system.
Then use "\\" instead of "\" example:"\\\\IP\\foldername"


Shiva, I'm afraid you're confusing file servers with web servers. When you set up a Windows Share, you're publishing it through the Windows file server service. It's a completely different program that uses completely different protocols and completely different tcp/ip ports. To be specific, the protocol is known as the Common Internet File System (CIFS, formerly known as SMB), and the ports are 137-139 plus a couple of additional ports whose numbers I don't remember at the moment.

Although CIFS can be used over the open Internet, it's rarely done. Partly because it's a direct connection to the fileserver's file system and partly because these ports are often firewall-blocked.

The web is served up by a different program and a different protocol (HTTP or HTTPS). Normally this is done over tcp/ip port 80 (HTTP) or 443 (HTTPS). HTTP servers cannot share files. All they can do is present and accept data. It's up to the web applications to figure out how to find the data or where (if anywhere) to store it. The confusion comes from the fact that URLs look a lot like filename paths (they aren't) and that the default behavior for most webservers is to take URLs that don't have explict actions mapped to them and use them as the basis to construct a filename path which they use to locate a file whose contents are copied to the client application. Going the other way requires more work. You either have to create a form with a file upload control on it, write a custom POST processor, or install DAV. But none of these options allow you to actually access the file directly, the way a file share does.

Also, in Java and in URLs, it's better to use the real (forward) slashes in path separators. It's slightly easier to read, OS-independent, and the backslash has an alternate meaning in many contexts as an escape character. Backslashes are an unfortunate legacy of DOS, but the Internet and Java were built primarily on Unix.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!