Reading and writing files in a JApplet
I want to create a file manager for a web server that will run in the browser. I started by making a simple interface to create a directory in the folder that the java class is run.
When run, the applet creates the folder fine but when I upload it and run it on a web server it doesn't create the folder.
Here is my directory creating code:
Code :
File f = new File(System.getProperty("user.dir")+"\\"+folderName+"\\");
f.mkdir();
I have had a look at using URL instead of files but it kept throwing exceptions and I would have no idea how to do it that way. Any help would be very much appreciated.
Re: Reading and writing files in a JApplet
Never, ever, ever use string concatenation on file paths.
Create a File object that represents the user dir, then use the File(File, String) constructor to create a File object that represents a directory in the user dir, then use mkdir.
"\\" is a literal '\' in Java. A 'path separator' is a platform defined entity that may not even exist on all Java platforms. Try to break the habit of using them explicitly.
Just to check that your code is doing what you want it to do, print the return value of File.getAbsolutePath() for the two File objects (user dir and new sub dir) to make sure they're what you expect.
Re: Reading and writing files in a JApplet
But if I want to create a directory them I cant specify a file because the directory won't even exist. This will then throw an exception saying the file does not exist.
The rest of the code is hear to help spot what may need changing:
Code :
package classes;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class CreateDir extends JPanel
implements ActionListener
{
JTextField TextAreaCreateDir;
public CreateDir()
{
this.removeAll();
this.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridheight = 1;
c.gridwidth = 1;
c.fill = GridBagConstraints.BOTH;
TextAreaCreateDir = new JTextField("");
add(new JLabel("Name: "), c);
c.gridx = 1;
c.gridy = 0;
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridheight = 1;
c.gridwidth = 2;
c.fill = GridBagConstraints.BOTH;
TextAreaCreateDir = new JTextField("");
this.add(TextAreaCreateDir, c);
JButton ButtonCreateDir = new JButton("Create Directory");
ButtonCreateDir.setActionCommand(TextAreaCreateDir.getText());
ButtonCreateDir.setName("CreateDir");
ButtonCreateDir.addActionListener(this);
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
c.gridwidth = 1;
c.fill = GridBagConstraints.NONE;
this.add(ButtonCreateDir,c);
}
public void CreateDirectory(String folderName)
{
if( folderName.replaceAll("[\\w-_]", "").equals(""))
{
File f = new File(System.getProperty("user.dir")+"\\"+folderName+"\\");
f.mkdir();
if(f.isDirectory())
JOptionPane.showMessageDialog(this, "Directory Created");
else
JOptionPane.showMessageDialog(this, "Failed to Create Directory");
}
else
JOptionPane.showMessageDialog(this, "Only letters, numbers and _- allowed");
}
public void actionPerformed(ActionEvent e)
{
if( "CreateDir".equals(e.getActionCommand()));
{
CreateDirectory(TextAreaCreateDir.getText());
}
}
}
Re: Reading and writing files in a JApplet
Post your exceptions. You are running this as an applet in a user browser? Where do you expect the directory to be created - on the web server, or on the user's PC? If the former, you need something like a servlet. If the latter, then I suspect your applet would have to be signed to get access to the local filestore.
Re: Reading and writing files in a JApplet
Quote:
Originally Posted by
Sean4u
Post your exceptions. You are running this as an applet in a user browser? Where do you expect the directory to be created - on the web server, or on the user's PC? If the former, you need something like a servlet. If the latter, then I suspect your applet would have to be signed to get access to the local filestore.
I need the directory to be created on the web server. There are no exeptions thrown when I run.
When I run the applet through eclipse, it will create the directory fine, but if I run the applet embedded in a browser on a local system it wont work. Surely they should both do the same if they are both run on the same computer locally?
Re: Reading and writing files in a JApplet
Like you've already been told- if you want to create files on the server, you're going to need to use something like a servlet. Applets don't run on the server's side, just the client side.
If you want to create files on the client machine, you're going to need to sign the applet.
What exactly do you mean by "it won't work"?
Re: Reading and writing files in a JApplet
Quote:
Originally Posted by
KevinWorkman
Like you've already been told- if you want to create files on the server, you're going to need to use something like a servlet. Applets don't run on the server's side, just the client side.
If you want to create files on the client machine, you're going to need to sign the applet.
What exactly do you mean by "it won't work"?
OK, does this mean that when the program is run through eclipse, it allows all permissions? But then when you run through the browser it is not allowed unless it is signed?
Re: Reading and writing files in a JApplet
Quote:
Originally Posted by
Majora94
OK, does this mean that when the program is run through eclipse, it allows all permissions? But then when you run through the browser it is not allowed unless it is signed?
Seems like a reasonable enough description to me. I would check out your Java Console, I would not be surprised if you found some errors there. What does the applet do when you run it in your browser? And are you sure you want to run this from an applet? From what you described, you should be using something like a servlet instead of an applet.
Re: Reading and writing files in a JApplet
You could use an Applet in a web browser for a file manager - sites like photobox.co.uk do that. If you search for "applet servlet communication" you'll find some sites that explain what needs to be done. It's reasonably straightforward, just convert your method calls to servlet requests, write those servlets and beef up your error handling a bit.