Java server program - write files to a specific folder on client side - java

I am writing a Java server program. Currently I'm using a DataOutputStream to output files to the requesting client or browser. I want the files to be sent to a particular directory on the client's side and not just to the browser (I do not want to write a client program to handle this issue). How do I make the server program write the file to a particular directory on the client's side?
Thanks in advance

Server doesn't know anything about client and neither is this a good idea to allow server to write files or anything on the client directories as it's an Http Security concerns and it won't allow servers to write viruses, changing properties etc on the user's machines who are visiting their web page.
So, what you can do is, client can send a request to server and server will write file on the client (e.g. browser AND not the directory) with sending back some mime-type. There are few mime-types which now a days, browsers can handle and show to the client from within the browser. Client can then download it or discard it as per his/her own choice to any directory on his/her machine.
Here is a bit of code you can write on server side to achieve what I said above:
OutputStream out = response.getOutputStream();
File file = new File(path_to_a_file);
response.setContentType(file_content_type);
response.setHeader("Content-disposition","attachment; filename="+FILE_NAME);
FileInputStream inputStream = new FileInputStream(FILE);
//Read bytes and write until finished

Related

How to handle java request in server side?

I am student and I am actually new to Javafx or java world, but I like programming...
In my room I have an old computer, I made it as a Server for test. (Ubuntu Server Operating System).
In this Server, I installed Apache at port 80 and Tomcat at port 8080.
Now I have developed very simple Login GUI using Javafx. Now I want do "Login in" or "Sign up", I have 4 ideas(questions) and 1 Problems:
Client-Side:1 Problem
User click "Login in", what is the normal way to do such a request? I mean, I should firstly get the username and password; and then MD5(password); and then username.getbytes(), password.getbytes(); and then java Serialization; then transfer them to server side. Am i right? or should I need some cryptograph in the transfer process?
Serve-Side: 4 ideas or questions
The Server (my old Ubuntu server computer) get the request from Javafx GUI Application. Then how can I make such things, and response?
I mean:
If I want using Java Codes to handel the request under Http Apache Server (port 80), what should I do? I mean, write a Java Programm, and then how run it in Http Server? If a use PHP, will it the same?
If I want using Java Codes to handel the request under Tomcat Apache Server (port 8080), what should I do?
If I self write a Java Programm at some any port (e.g. 9999), something like: while(true) serverSocket.acceppt();}(at port 9999 e.g.) , what should I do , I should run it under Tomcat or Apache? Or what should I do? I really do not know....
If I want using Apache to forward the request to tomcat, I should use somethink like mod_jk or mod_proxy, right?
I need some help about one Concept or direction.
Client-Side:1 Problem
The normal process of using the authentication is to store the user passwords on the server and then every time the user wants to login, the server matches the password against the already stored password in the server. If it matches, then the user session starts.
You can have your password stored on the server as plain text (not at all recommended) or in encrypted form. If you decide to store it in the encrypted form then every time the user provides the password, you must encrypt it on the client side using java script encryption libraries and match it against the password saved on the server. For sending the encrypted password to the server you can use either ajax (e.g. JQuery) or just HTML submit method.
Serve-Side: 4 ideas or questions
The Server (my old Ubuntu server computer) get the request from Javafx GUI Application. Then how can I make such things, and response? I mean:
If I want using Java Codes to handel the request under Http Apache Server (port 80), what should I do? I mean, write a Java Programm, and then how run it in Http Server? If a use PHP, will it the same?
If you decide to build your server with Java language, then you can only have tomcat server (which is actually a servlet container). Each request on the server (such as for user login) is handled by a backend servlet which receives the HTTP request from the client side and performs the required operation. You can also write JSP.
If you decide to use PHP then you only need apache server and not tomcat server. For php, every request is handled by a PHP file on the server.
If I want using Java Codes to handel the request under Tomcat Apache Server (port 8080), what should I do?
If I self write a Java Programm at some any port (e.g. 9999), something like: while(true) serverSocket.acceppt();}(at port 9999 e.g.) , what should I do , I should run it under Tomcat or Apache? Or what should I do? I really do not know....
Writing a customer java program which listens to HTTP request on a specific port is called a server program and apache or apache tomcat are nothing but these program already written and provided for free. So, only in very specific situation you might decide to write your own but for sure having a login authorization doesn't require it.
If I want using Apache to forward the request to tomcat, I should use somethink like mod_jk or mod_proxy, right?
This is don't know.

Delete file from a FTP server using Java

I am writing an application that involves multiple clients. One client uploads a file using FTP to a server, and then another client downloads the file and deletes it. I am using the FTP server kind of as a middleman to exchange information because I do not want the user to have to port forward. I have figured out how to upload a file, but I cannot figure out how to delete the file. The command for deleting a file using FTP is:
DELE <filename>
I have tried doing so, but with no success. Here is the code that I have tried:
public static void deleteFile(String name) throws IOException
{
URL url = new URL("ftp://a1111111:password#mywebsite.com/public_html/misc/screenshots/picture.png;type=i");
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(url.openConnection().getOutputStream()));
writer.write("DELE picture.png");
writer.flush();
}
I have a feeling that the < filename > that I provided may be wrong because I did not include the directory names in the path. However, since I explicitly told the path that of the file in the URL, I am not quite sure what I am supposed to enter for the < filename >.
I have read other questions on this web site about problems very similar to this and all of the responses tell to use a library. The thing is, most of those libraries are written in pure Java so the developers of that library had to figure out a way to do what I am trying to do without a library. I want to do that as well. I do not like attaching extra files besides my own to the things that I make. So please, do not tell me to use a library - it's not what I'm looking for.
If you need any clarification, please ask!
EDIT: If I use this code to receive a response:
byte[] response = new byte[conn.getInputStream().available()];
conn.getInputStream().read(response);
System.out.println("Response: " + new String(response));
My command just gets echoed back:
Response: DELE test1.png
I think you need to do the retrieve and delete in two separate operations; some random documentation I found for FtpURLConnection says, in part:
This class Opens an FTP input (or output) stream given a URL. It works as a one shot FTP transfer :
Login
Get (or Put) the file
Disconnect
I did not see any methods in the documentation that would allow deleting a file.
You may wish to use the URL mechanism to retrieve the file, but I would drop down to using raw sockets to delete the file. Create a new connection to the FTP command port, log in, and issue the DELE command manually. If this is the only step you're taking, you might be able to get away with doing relatively poor error handling and maybe only two read() requests and simply show the output transcript to the user once you're done.
It's a bit dirty, but I completely understand not wanting to carry around a megabyte of additional source to achieve the moral equivalent of echo -e "user foo\npass password\ndele /path/to/file\nlogout" | nc ftp.remote.example.com 21.
Can you use some FTP clients to do the operation?
You can try http://commons.apache.org/net/api-3.1/org/apache/commons/net/ftp/FTPClient.html from the Apache commons-net. It is easy to use.

send to many files from an applet to an servlet

I need to send X number of files to my servlet from an applet, which is the best way to do this?
And I need to send before the files, a java object populated.
I need to do it all in a single connection.
I'll upload my applet 3 ~ 10mb to my servlet.
I currently use FileInput together with the OutputStream and BufferedOutputStream to send a file, causing the buffer size is 8K.
First time I'll try to zip all the files to upload a zip file to the servlet, but I know it's not a good solution.
In the Applet side, send it as a normal multipart/form-data request by either URLConnection or HttpClient. In the Servlet side, use either HttpServletRequest#getParts() or Commons FileUpload to extract the parts from the request. This way the applet and servlet are not tight coupled to each other, but just reuseable on different servers (e.g. PHP) and/or clients (e.g. a simple HTML page).
Whether or not to zip the individual files into a single zip file is a decision you'd need to make yourself based on coding and performance impact.

Upload a file using Java Swing coupled with php in the server side

I have a Java Swing application that needs to upload a file to the server. My server is a an Apache. I know that I can do this with a Servlet but I don't want to install/maintain a Tomcat server. So I'am wondering is it possible to do it with a PHP script in the server side ?
Thanks
Well you can do that using jakarta HttpClient library
"Upload file HttpClient"
The article has both server side (php) and client side (java) code
you need to specify the folder on the PHP code, it is already doing through the method
move_uploaded_file ($_FILES['userfile'] ['tmp_name'], $_FILES['userfile'] ['name']);
This function checks to ensure that the file designated by $_FILES['userfile'] ['tmp_name'] is a valid upload file (meaning that it was uploaded via PHP's HTTP POST upload mechanism). If the file is valid, it will be moved to the filename given by $_FILES['userfile'] ['name'].

I want to copy an excel file from one location to other URL server location

I want to copy/move an Excel from local machine/given path to Server location.
The server location , we manually uploads the Excel file but i want to do it autmatically by using Java files.Can i do it , if so please give me a suggestion on it.
How do we skip those uploading mechanism which we do manually.
thanks in advance
VSRK
If you upload it via a web form, you can use Apache HttpClient to programmatically create the request sent to the server and upload the file. You will need to handle by yourself any authentication needed before the upload, store any cookies the server sends you, and do any post-authentication navigation the web page requires. You will also need to recreate the upload request exactly as the web browser sends it, parameters and all.

Categories