Saving a .txt to my online server - java

I've been meaning to do this for a long time. I want my user to save/update a text file to an online server. How can I connect to my server, and then to a certain file;
In php and other languages, you must include a connect.php file, which contains your sql table name, username, pswrd, and hostname.
Is there such a set up in a function to write in your Java code? Can you post some code answers on how you login and then, update a .txt file?

you want to save a file, so why connect via database?
In Java, you can do the same thing using JSP. In your JSP, accept input (via HTTP Request object) and save the data to file.
To save a file, I suggest you use Apache Commons FileUtils rather than writing your own code.
Use FileUtils.writeStringToFile method(File, String, Charset)
http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html#writeStringToFile(java.io.File, java.lang.String, java.nio.charset.Charset)

Related

Is it possible to read an "mbox" file passing the mbox data not a path?

All the mbox examples I've seen have code like this:
final Store store = session
.getStore(new URLName("mstor:" + <path>);
Since I want to run the mbox code in a REST service, I need to be able to pass the mbox data itself in a variable instead of a disk path.
However, I cannot find any examples showing you can pass the data instead of a path and the API itself doesn't seem to have any methods supporting passing the data.
Has anyone successfully read mbox data using this mstor API to work without a path but instead using the mbox data?
If mbox API must get a disk path, perhaps a workaround for the REST service would be to create a temporary path using the mbox data "POSTed" and use that temporary path with the existing mbox API? Then, make sure the temporary file is gone when done.

How to upload and download a file with the given path(already known) without using html form in servlet using post method?

I know solution of the above problem using HTML form in which user chooses a file from computer and upload it and then download it but how to do it without using HTML with the already given path of file.
Thank you For your concern in Advance.
I assume the scenario is that the user accesses your server from a browser.
In this case you will not be able to access the file system given file paths. Only the user can grant access to certain files by selecting them in a file upload input.
If the user uses a mobile/desktop application that sends the file to your server, you must implement the upload in the app itself.

How do I append some text to a distant txt file via http in Java?

A text file is located at http://example.com/myText.txt with read and write permissions.
How can I append some line of text on top of it using Java. I see examples with the class OutputStreamWriter but they all use a file as an output. What about http?
(I need the code to be compatible with Java 5)
HTTP in the sense you understand it does not support any write operation of this nature.
There is no clear relationship between a URL and a file on the server; Specifically HTTP GET generally returns you a file-like resource if the URL refers to a web-like server; but the outcomes of HTTP POST are not simply an overwrite of the remote url file they are processed on a case by case basis by the other side.
If this is really what you want I strongly suggest you use FTP which is intended for this purpose.
There is no generic solution to this other than reading the content, appending the line locally, and writing it back with PUT (and that requires that the server supports the PUT method).

Let user choose file type while saving a file in Web application

I'm working on a web application at the moment using Tomcat and spring framework. A CSV file is stored on the server, the path to the file and the filename is stored in the database.
A search button on a webpage would list all records in database, if user click on one listed item, a saving dialog would be displayed and the file will be saved locally.
I want a dialog like this opened when user click on one item, how can I do it with httpServlet? I can think about a solution is set content-disposition type in response header to 'attachment'. It would force the browser to download the file instead of trying to display it in browser. But I want user to be able to select the type for file download. Please take a look at the image below
Setting the Content-Type response header to something that the browser is unable to render should prompt the user to save the file upon receiving the response. A value of application/octet-stream (arbitrary binary data) should do the trick.
However, since you expect the actual file content to be in different format depending on user choice, here's what you would need to do:
create a link/form on your html page that will allow user to select a type and make a request to URL with proper extension (like download/file.xls for XLS or download/file.csv for CSV).
in your servlet that handles these URLs, check the extension requested (easy) and then convert the file to expected format within your servlet (not so easy) and send it in response.

Pass a string from a JAVA application to a php variable

Assuming that we have a JAVA application that connects and exchanges some XML's with an EPP server.
That application writes the data that it receives from the server into files.
It creates a file for each response it gets, according to System.currentTimeMillis().
The name of the file is currentMillis.
Now, since I'm going to need a php script to locate those files, can I simply make the
JAVA application return a value (that of the currentMillis) and somehow from the php script parse that into a PHP variable?
You will need to do the Java and PHP communication over HTTP. So convert the java code that returns the time into a Servlet.
Then invoke the servlet from your PHP code and capture the data returned into a PHP variable.

Categories