Results 1 to 4 of 4
  1. #1
    MrFish is offline Member
    Join Date
    Jan 2010
    Posts
    43
    Rep Power
    0

    Default Apache Common's ClientFTP Uploading Broken Images?

    Hello. I am trying to upload images to a specific folder on my website. I've got the files to upload but they are showing up as broken images. Anyone know what I could be doing wrong?-

    Java Code:
    import java.io.*;
    import org.apache.commons.net.*;
    import org.apache.commons.net.ftp.*;
    import org.apache.commons.net.io.*;
    import org.apache.commons.net.util.*;
    
    public class ScreenshotUploader 
    {
    	private String username;
    	private String password;
    	private String server;
    	private FTPClient ftp;
    	private String outputFolder;
    	private String status;
    	
    	public ScreenshotUploader()
    	{
    		status = "waiting";
    		username = "username";
    		password = "password";
    		server = "server";
    		outputFolder = "www/pathto/folder/";
    		
    		ftp = new FTPClient();
    		
    		try
    		{
    			ftp.connect(server);
    			System.out.print(ftp.getReplyString());
    			ftp.login(username, password);
    			System.out.print(ftp.getReplyString());
    			ftp.changeWorkingDirectory(outputFolder);
    			System.out.print(ftp.getReplyString());
    			ftp.setControlKeepAliveTimeout(300);
    			ftp.setFileType(FTP.ASCII_FILE_TYPE);
    		}catch(Exception e){System.out.println(e);}		
    	}
    	
    	public String uploadFile(String localPath)
    	{
    		System.out.println("Uploading " + localPath);
    		if(status.equals("processing"))
    		{
    			System.out.println("FTP is Busy...");
    			return null;
    		}
    		else
    		{
    			status = "processing";
    			String fileName = "";
    			
    			for(int i = 0; i < 28; i++)
    			{
    				int k = (int)Math.round(Math.random()*9);
    				char c = (char)(Math.round(Math.random()*20)+66);
    				char bc = (char)(Math.round(Math.random()*20)+98);
    				int rand = (int)Math.round(Math.random()*100);
    				fileName += c;
    			}
    			
    			fileName += ".png";
    			try
    			{
    				InputStream in = new FileInputStream(localPath);
    				ftp.storeFile(fileName, in);
    				status = "waiting";
    				return fileName;
    			}catch(Exception e)
    			{
    				status = "waiting";
    				System.out.println(e);
    				return null;
    			}
    		}
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Apache Common's ClientFTP Uploading Broken Images?

    Have you tried uploading a text file so you can easily look at its contents to check for problems?

    What does this do: ftp.setFileType(FTP.ASCII_FILE_TYPE);
    An image is binary, you don't want FTP changing lineend characters, etc
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    MrFish is offline Member
    Join Date
    Jan 2010
    Posts
    43
    Rep Power
    0

    Default Re: Apache Common's ClientFTP Uploading Broken Images?

    Quote Originally Posted by Norm View Post
    Have you tried uploading a text file so you can easily look at its contents to check for problems?

    What does this do: ftp.setFileType(FTP.ASCII_FILE_TYPE);
    An image is binary, you don't want FTP changing lineend characters, etc
    That was the problem. By luck I changed that before reading this and came back to declare the problem solved. How do you know if something is a binary or ascii? Isn't ascii just a different character encoding?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Apache Common's ClientFTP Uploading Broken Images?

    ASCII is usually text with lineends Different OSs have different lineend characters. FTP tries to fix them according to the local system.
    For binary files, FTP doesn't try to fix anything.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Uploading File to server ( Apache.commons.net)
    By quirell in forum Networking
    Replies: 0
    Last Post: 01-14-2012, 11:19 AM
  2. Replies: 1
    Last Post: 05-19-2010, 09:10 AM
  3. still broken?
    By xcallmejudasx in forum Forum Lobby
    Replies: 1
    Last Post: 06-05-2009, 11:26 PM
  4. need help in uploading images in swing program!!!
    By ashton in forum New To Java
    Replies: 10
    Last Post: 01-26-2009, 10:01 AM
  5. Form for uploading images
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-11-2008, 09:15 AM

Posting Permissions

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