0

My code is working fine (choosing directory) when used in the main method. However, using the same code copied into a servlet, isn't working, what should I do to get this to work with servlet?

public class SelectDirectory {

    public static void main(String args[]) throws IOException, Exception {
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new java.io.File("."));
        chooser.setDialogTitle("choosertitle");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);

        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            String dirFolder = "" + chooser.getSelectedFile() + "";
            FunctionsOfZip zipFun = new FunctionsOfZip();
            zipFun.InputReader(dirFolder);
        } else {
            System.out.println("No Selection ");
        }
    } 
}
JonK
  • 2,097
  • 2
  • 25
  • 36
tajMahal
  • 418
  • 6
  • 18
  • 40
  • What should that do? Open a FileChooser window on the web server? Usually, those don't have a monitor (or a person) attached to them. – Thilo Jun 04 '14 at 08:47
  • a servlet runs on the server side. that 'll cause some trouble trying to run something like this through servers. – Stultuske Jun 04 '14 at 08:47
  • http://stackoverflow.com/a/4502422/1031945 – Aniket Kulkarni Jun 04 '14 at 08:48
  • @Stultuske, then there is no posibility of running this code using servlet? – tajMahal Jun 04 '14 at 08:49
  • of course it will run. but the client won't get the FileChooser, it will "run" on the server machine. and, as Thilo said: those usually don't have a monitor attached, or a person observing them, so nobody would notice. even if somebody noticed it, it would be running on the wrong system – Stultuske Jun 04 '14 at 08:51

2 Answers2

2

JFileChooser is for Swing Application. Servlet is running on Server side. At client side there is browser which has HTML input type named file. This file input will open the directory of client to select file for upload.

 <form>
 <input type="file"/>
 </form>
Masudul
  • 21,823
  • 5
  • 43
  • 58
0

You have to write in your JSP page an input param with type="file" , then haldle it in your servlet.

This link may help you.

Mifmif
  • 3,132
  • 18
  • 23