• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

upload a file to a server

With this  class : http://www.nsftools.com/tips/FTPConnection.java

What should I do to upload a file to the server?


            try {
                  if (returnVal == JFileChooser.APPROVE_OPTION) {

                        imageFile = fileChooser.getSelectedFile();
                        imageName = imageFile.getName();
                        loadGraphicsLocation = imageFile.getPath();

                        if (connection.connect(SERVER)) {
                              if (connection.login(USER, PASSWORD)) {
                                    


                                    System.out.println("Im logged in");  // ok
                                    connection.changeDirectory(Data.IMAGES_DIRECTORY); // ok
                                    System.out.println("Im files in\n" + connection.listFiles());  // ok
                                    
                                                                                                 
                                                                                                  connection.uploadFile(imageName);  // DOES NOT WORK
                                    


                                    

                              }
                              connection.disconnect();
                        }
                  }

            } catch (UnknownHostException e) {
                  System.out.println("ERROR A: " + e.getMessage());
            } catch (IOException e) {
                  System.out.println("ERROR B: " + e.getMessage());
            }
0
SolangeRichard
Asked:
SolangeRichard
  • 2
1 Solution
 
CEHJCommented:
What error are you getting? You might try the absolute path
0
 
SolangeRichardAuthor Commented:
Well, I dont receive any error at all, it just wont load.
0
 
SolangeRichardAuthor Commented:
I found a solution to my problem by using :

connection.executeDataCommand("stor "+iname, imageStream);
instead of
connection.uploadFile(imageName);  


      String loadRoomMainImage() {
            // get the image
            String iname = "error";
            String loadGraphicsLocation = "not defined";
            File imageFile = null;
            JFileChooser fileChooser = new JFileChooser(loadGraphicsLocation);
            
            int returnVal = fileChooser.showOpenDialog(null);

            FTPConnection connection = new FTPConnection();

            try {
                  FileInputStream imageStream = null;
                  if (returnVal == JFileChooser.APPROVE_OPTION) {

                        imageFile = fileChooser.getSelectedFile();
                        iname = imageFile.getName();
                        loadGraphicsLocation = imageFile.getPath();

                        if (connection.connect(Data.SERVER)) {
                              if (connection.login(Data.USER, Data.PASSWORD)) {
                                    connection.changeDirectory(Data.IMAGES_DIRECTORY);                                    
                                    imageStream = new FileInputStream(imageFile);                                    
                                    connection.executeDataCommand("stor "+iname, imageStream);
                              }
                              connection.disconnect();
                        }
                  }

            } catch (UnknownHostException e) {
                  System.out.println("unknown host error: " + e.getMessage());
            } catch (IOException e) {
                  System.out.println("io exception: " + e.getMessage());
            }            
            
            return iname;
            
      }
0
 
GranModCommented:
Closed, 250 points refunded.
GranMod
The Experts Exchange
Community Support Moderator of all Ages
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now