?
Solved

upload a file to a server

Posted on 2006-04-26
5
Medium Priority
?
198 Views
Last Modified: 2010-03-31
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
Comment
Question by:SolangeRichard
  • 2
4 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16547098
What error are you getting? You might try the absolute path
0
 

Author Comment

by:SolangeRichard
ID: 16547831
Well, I dont receive any error at all, it just wont load.
0
 

Author Comment

by:SolangeRichard
ID: 16547863
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
 

Accepted Solution

by:
GranMod earned 0 total points
ID: 16575570
Closed, 250 points refunded.
GranMod
The Experts Exchange
Community Support Moderator of all Ages
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question