I have developed an applet that needs to read in a data file from the user. I have the user pick the file outside the applet and upload it, I then move a copy of the file to the applet folder so I can access it with the applet with only sandbox security. My problem arises from the fact that I run the applet on several hosts. How can I determine the correct host this applet was downloaded from so I can connect to the proper host for the correct data file?
See the Applet.getCodeBase() & getDocumentBase() methods.
Related
I want to read a directory from a client's computer and get the contents of the files that are stored in that directory.
I already know how to read the files as well as get all the files, but when I try an external test (not localhost) I get a FileNotFound Exception, but when I try an internal test everything works perfectly.
I need to save some image files from user directory to the project folder on server, but I cant find working solution. What is the simplest way to select file in users pc and copy it to the server? And what url may I use to acces this file?
Run an FTP(S) server, and use a Java FTP client to connect and upload the files.
You can make the FTP home directory a subfolder of wwwroot on your server, so you can access the uploaded files as "example.com/diagrams/mydiagram".
Beware though, the client must have the FTP password to connect, therefore your users can upload anything they want (including malicious scripts). If you don't trust your users, you'll need per user auth and verification of the uploaded files, which will require server side code.
I have created an applet which creates a file on running it. But when I run my applet via server, it fails.
Is there any possible way to create a file on server with applet?
EDIT:I am creating a sound record applet which works fine when I run the applet in browser locally.It actually creates a file of recorded sound,but when I run the same applet on server,it does not create file.Is it because the server does not allow you to do so?
Is there any possible solution so that the file can be created?
File objects always point to a location (that may not exist) on the client machine.
To store something on a server, it would require some server side functionality to accept the bytes and create a (server-side) File. That might be done with PHP, servlets/JSP, ASP etc. Once the server-side is organized to accept the bytes, the applet can connect to it and push the sound recording through.
Java Applets are run on the client machine. Once you invoke the page containing an applet, the applet gets downloaded to the client's machine and runs. Hence it will not get access to the server.
I would like to implement a Java application which runs on a webpage and allows:
User A (from its browser) can request a file to User B (with its browser opened). Of course User A knows User B's IP and port.
User B can respond this request from its browser.
Which options (Java libraries, technologies,...) do I have?
THANKS!
If you want Java in the browser, the only way to go is applets.
But a normal (untrusted) applet:
can not access files on the local system
can not listen at any ports
can only open sockets on the server it came from.
This is for security reasons: an applet should not be able to harm the user whose computer it runs on.
So, if you want to do what you said, you need the user to trust you and give you more permissions. For the file access, the way to go would be the JNLP API (i.e. start your applet with a jnlp file, and then use the API in javax.jnlp, specially FileOpenService and FileSaveService. The user then needs to confirm the access before choosing a file with a file chooser.
This still does not help for the network access - your applets need to have suitable SocketPermissions there, if you don't want to proxy everything on your server (which would not be peer-to-peer). For this, you need to sign your applet, and request all permissions from the user (there is no finer-grained way to give only the necessary SocketPermissions, I think). You can do this in the jnlp-file.
I have written a code in JAVA to search files from the system.User specifies the filename and extension on text input on applet window and on button click code establishes a connection to oracle database and searches the directory name from the database table.The code for searching works fine without applet and on using applet it detects an error which is
"access denied(java.util.PropertyPermissionfile.encoding read)"
How do i grant this applet the permission to read from database?
An applet is only allowed to connect to server from where it was downloaded. Otherwise you need to sign the applet. Or use a proxy to connect to the database from the machine the applet came from.
See also How can I connect from an applet to a database on the server?