Results 1 to 4 of 4
  1. #1
    ajay.aju1 is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Unable File Upload in FTP Server

    Here i am trying to upload multiple file from browser to ftp server, creating directory in ftp server and upload file into same dorectory,

    while uploading i getting file not found exception.

    Following is my code, please review and suggest me..

    Creating Directory......

    Java Code:
    import java.io.FileInputStream;
    import java.io.IOException;
    
    import org.apache.commons.fileupload.FileItem;
    import org.apache.commons.net.ftp.FTPClient;
    
    public class FTPUtill {
    
    	public static boolean makeDirectories(FTPClient client, String savePath)
    			throws IOException {
    		String[] pathElements = savePath.split("/");
    		
    		if (pathElements != null && pathElements.length > 0) {
    			for (String singleDir : pathElements) {
    				boolean existed = client.changeWorkingDirectory(singleDir);
    				if (!existed) {
    					boolean created = client.makeDirectory(singleDir);
    					if (created) {
    						System.out.println("CREATED directory: " + singleDir);
    						client.changeWorkingDirectory(singleDir);
    						
    					} else {
    						System.out.println("COULD NOT create directory: " + singleDir);
    						return false;
    					}
    				}
    			}
    		}
    		return true;
    	}
    }
    FTPFileUpload.java

    Java Code:
    import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.Date;
    import java.util.Iterator;
    import java.util.List;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.commons.fileupload.FileItem;
    import org.apache.commons.fileupload.FileItemFactory;
    import org.apache.commons.fileupload.FileUploadException;
    import org.apache.commons.fileupload.disk.DiskFileItemFactory;
    import org.apache.commons.fileupload.servlet.ServletFileUpload;
    import org.apache.commons.net.ftp.FTP;
    import org.apache.commons.net.ftp.FTPClient;
    
    import com.oreilly.servlet.MultipartRequest;
    
    /**
     * Servlet implementation class DemoTest
     */
    @WebServlet("/test")
    public class DemoTest extends HttpServlet {
    	private static final long serialVersionUID = 1L;
    
    	/**
    	 * @see HttpServlet#HttpServlet()
    	 */
    	public DemoTest() {
    		super();
    		// TODO Auto-generated constructor stub
    	}
    
    	@SuppressWarnings("deprecation")
    	protected void doPost(HttpServletRequest request,
    			HttpServletResponse response) throws ServletException, IOException {
    
    		boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    		String value = null;
    		String value1 = null;
    		String value2 = null;
    		String fileName = null;
    		
    		FTPClient client = new FTPClient();
    		InputStream fis;
    		boolean result;
    		String ftpServerAddress = "ftp.hgel.in";
    		int port = 21;
    		String username = "";
    		String password = "";
    
    		if (isMultipart) {
    			FileItemFactory factory = new DiskFileItemFactory();
    			ServletFileUpload upload = new ServletFileUpload(factory);
    
    			try {
    
    				client.connect(ftpServerAddress, port);
    				result = client.login(username, password);
    				if (result == true) {
    					System.out.println("Successfully Login");
    				} else {
    					System.out.println("Invalid Login ! Please Tyr Again");
    				}
    
    				client.setFileType(FTP.BINARY_FILE_TYPE);
    				client.enterLocalPassiveMode();
    				@SuppressWarnings("unchecked")
    				List<FileItem> items = upload.parseRequest(request);
    
    				for (FileItem item2 : items) {
    					if (item2.isFormField()) {
    						if (item2.getFieldName().equals("auditor")) {
    							value = item2.getString();
    							System.out.println("Value :" + value);
    						} else if (item2.getFieldName().equals("month")) {
    							value1 = item2.getString();
    							System.out.println("Value :" + value1);
    						}
    
    						else if (item2.getFieldName().equals("roname")) {
    							value2 = item2.getString();
    							System.out.println("Value2 :" + value2);
    
    							@SuppressWarnings("rawtypes")
    							String savePath = "/" + value1 + "/" + value + "/"
    									+ value2;
    							FTPUtill.makeDirectories(client, savePath);
    						
    
    				Iterator iterator = items.iterator();
    				while (iterator.hasNext()) {
    					FileItem item = (FileItem) iterator.next();
    
    					if (!item.isFormField()) {
    						fileName = item.getName();
    						//FileReader fileread = new FileReader(fileName);
    						File localFile = new File(fileName);
    						BufferedInputStream is = new BufferedInputStream(new FileInputStream(localFile));
    						
    						client.storeFile(fileName, is);
    						
    					}
    				}
    			}
    						
    					}
    				}
    
    			} catch (FileUploadException e) {
    				e.printStackTrace();
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		}
    	}
    
    }

    Upload.jsp

    Java 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>Insert title here</title>
    </head>
    <body>
    	<form action="test" method="post" enctype="multipart/form-data">
    		<fieldset>
    			
    			<label>Month of Audit :</label> 
    			<select name="month">
    				<option value="January">January</option>
    				<option value="Febuary">Febuary</option>
    				<option value="March">March</option>
    				<option value="April">April</option>
    				<option value="May">May</option>
    				<option value="June">June</option>
    				<option value="July">July</option>
    				<option value="August">August</option>
    				<option value="September">September</option>
    				<option value="October">October</option>
    				<option value="November">November</option>
    				<option value="December">December</option>
    			</select> 
    			
    			<label>Auditor Name :</label> 
    			<select name="auditor">
    				<option value="Abhinav">Abhinav</option>
    				<option value="Abhishek Sharma">Abhishek Sharma</option>
    				
    			</select> 
    			
    			<label>RO Name :</label> 
    			<input type="text" name="roname"> 
    			
    			<label for="filename_1">Choose Your Reports: </label> 
    			<input id="filename_1" type="file" name="filename_1" size="50" multiple="multiple" />
    			
    			<input type="submit" value="Submit">
    		</fieldset>
    	</form>
    </body>
    </html>
    Please help me on this.....

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,256
    Rep Power
    6

    Default Re: Unable File Upload in FTP Server

    Java Code:
    File localFile = new File(fileName);
    Try doing a System.out of localFile.getAbsolutePath() - methinks it is not what you expect it to be.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    ajay.aju1 is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Re: Unable File Upload in FTP Server

    Quote Originally Posted by gimbal2 View Post
    Java Code:
    File localFile = new File(fileName);
    Try doing a System.out of localFile.getAbsolutePath() - methinks it is not what you expect it to be.
    I am not trying to get absolute path , whatever files would browse by the user, that file should be uploaded to the FTP server

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,256
    Rep Power
    6

    Default Re: Unable File Upload in FTP Server

    Yes, and you need a -correct- absolute path to the file that was uploaded before you can do that. And I'm 99% certain that you don't have that. That's why I'm instructing you to print out the absolute path that the File object will return now to see what it is. Compare that to where the file will actually be after uploading.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Automatically upload file to FTP server
    By dman in forum Networking
    Replies: 1
    Last Post: 03-21-2011, 04:48 PM
  2. Upload/download a file to/from server
    By karq in forum Networking
    Replies: 16
    Last Post: 08-24-2010, 10:43 AM
  3. Upload war file on server
    By sandeeprao.techno in forum Advanced Java
    Replies: 2
    Last Post: 12-16-2009, 11:55 AM
  4. :large file upload to server(chunk upload)
    By tommy_725 in forum Networking
    Replies: 0
    Last Post: 10-16-2009, 01:21 AM
  5. UPload a file to server?
    By makpandian in forum JavaServer Pages (JSP) and JSTL
    Replies: 10
    Last Post: 07-21-2009, 02:35 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •