Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

how do i upload a file over ftp server using java and jsp?  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I am trying to upload a file over ftp server using java and i am successfully uploaded.but my approach is when the user browse the file and after submit it will be stored in server. i am trying many times but i am unable to get exactly.any one can help me on this? Thanks in advance

my java code:
 
                   public static void main(String[] args) {
                   String server = "localhost";
                   int port = 21;
                   String user = "admin";
                   String pass = "password";
                   FTPClient ftpClient = new FTPClient();
                   try {
                   ftpClient.connect(server, port);
                   ftpClient.login(user, pass);
                   ftpClient.enterLocalPassiveMode();
                   ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
                   File firstLocalFile = new File("C:\\Users\\demo\\example.txt");
                   String firstRemoteFile = "example.txt";
                   InputStream inputStream = new FileInputStream(firstLocalFile);
                  boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
                  inputStream.close();
                  if (done) {
                  System.out.println("file is uploaded successfully");
                  }
                  } catch (IOException ex) {
                  System.out.println("Error: " + ex.getMessage());
                  ex.printStackTrace();
                 } finally {
                  try {
                if (ftpClient.isConnected()) {
                ftpClient.logout();
                ftpClient.disconnect();
                }
                } catch (IOException ex) {
                ex.printStackTrace();
                }
                }
                }

       
jsp code :

             <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
             pageEncoding="ISO-8859-1"%>
             <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
             <html>
             <head>
             <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
             <title>UPLOAD FILE</title>
              </head>
              <body>
             <center>
              <h1>Upload File Form</h1>
               <form action="FTPUploadFileDemo" enctype="multipart/form-data" method="post" accept = ".txt">
               Select<input type="file" name="file2" /><br>
               <input type="submit" value="upload" />
                </form>
               <%
               String file_name=(String)request.getParameter("filename");
              if(file_name!=null){
                out.println("File uploaded successfuly");
               }
                 %>
           </center>
            </body>
          </html>      


 please any one can help me or suggest me what i need to change?
 
Marshal
Posts: 62212
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please read this.
 
Saloon Keeper
Posts: 5130
135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PatienceIsAVirtue

I'm unclear about what the flow of control is supposed to be. The HTML page uploads a file to an HTTP server, which will in turn upload the file to an FTP server? An HTML page can't upload files via FTP.

If so, you could write a servlet that accepts the HTTP file upload (possibly using a library such as Apache Commons FileUpload). For that you will need a servlet container such as Tomcat to run in - a pure HTTP server will not do. The servlet would then incorporate the Java code you wrote for uploading to the FTP server.
 
chebolu dinesh kumar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.can you explain some more details ?
 
Tim Moores
Saloon Keeper
Posts: 5130
135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you misunderstood what I said - I asked you for more details because I'm unclear what you want to do, exactly:

I'm unclear about what the flow of control is supposed to be. The HTML page uploads a file to an HTTP server, which will in turn upload the file to an FTP server? An HTML page can't upload files via FTP.


 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the code shown above, it's unclear that where saved file is stored in JSP page and how it is diverted to JAVA code.
 
chebolu dinesh kumar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

SunilK Chauhan wrote:From the code shown above, it's unclear that where saved file is stored in JSP page and how it is diverted to JAVA code.




    hi chauhan, with that above code i am successfully stored the file in ftp server . i am using filezilla as a ftp server.but i need to upload a file from UI and click the upload button after that file will be stored in ftp server.because of that i am using jsp but i am unable call the file path from jsp page.i dont have much knowledge on jsp can you help me? Thanks
 
Tim Moores
Saloon Keeper
Posts: 5130
135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While you could in theory convert that code into a JavaBean for use with jsp:useBean in a JSP, in practice I think that wouldn't work, because JSPs can not handle form file uploads. That's why I recommended converting the code into a servlet in my previous post.
 
chebolu dinesh kumar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 I am implementing a servlet program i am not facing any  errors.while upload the file and submit the button i got errors

my servlet code :
 
 


can anyone help me on this ? can anyone review what i am wrong and what i need to change the code?
Thanks in advance
 
Tim Moores
Saloon Keeper
Posts: 5130
135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i am not facing any  errors. ... I got errors


That's the part that confuses me - what are those errors? Quote them here in full. What, exactly, are you doing when those errors occur? What is or is not printed out? Which lines of code are or are not being executed?
 
Tim Moores
Saloon Keeper
Posts: 5130
135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some issues that need to be addressed at some point:

  • Using doGet for altering the state of the server - the code should use POST rather than GET


  • Using local directory names like - the code should use directories relative to the installed server. On a production system access to the file system will be heavily curtailed.
  •  
    chebolu dinesh kumar
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    while upload a file from Ui and click upload button i got java.lang.ClassNotFoundException : classname and HTTP status 500 internal error
     
    Tim Moores
    Saloon Keeper
    Posts: 5130
    135
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Make sure all classes are in packages (or did you just omit the package declaration when posting?), and that the fully-qualified class name (including the package names) is used in web.xml where appropriate.
     
    You showed up just in time for the waffles! And this tiny ad:
    RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
    https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!