Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

FTP Upload: Image

807588Apr 17 2009 — edited Apr 20 2009
Hi there

I use the org.apache.commons.net.ftp.FTPClient package to upload data to my FTP Server. It works without any errors for files, but not for images.
I make a ScreenShot, store it local and finally upload it. My Program correctly creates the image local, but the upload is a bit strange, because I cannot open the image on the server. The image is stored, but in the wrong way I think.

Here's my source Code to make the image, which works:
private void screenShot() {
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Dimension screenSize = toolkit.getScreenSize();
		Rectangle rect = new Rectangle(200, 280, screenSize.width - 350, 200);
		BufferedImage image = robot.createScreenCapture(rect);

		//create a file with a random name
		File file = new File("/home/jukka/Desktop/screens/"+makeName()+".png");
		try {
			ImageIO.write(image, "png", file);

			//upload the image
			ftp.uploadImage(file.getName(), file.getAbsolutePath());
		} catch (IOException e) {
			log.log("IO Exception: "+e.getMessage());
		}
	}
{code}

And that's my FtpClient, as mentioned before, the one from apache:

{code:java}

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

public class FtpClient {

	private FTPClient client;

	public FtpClient(String host, String user, String password)
			throws IOException {
		client = new FTPClient();
		client.connect(host);
		client.login(user, password);
		client.setFileType(FTP.BINARY_FILE_TYPE);
	}
	
	public void uploadImage(String remote, String local){
		try {
			InputStream input = new BufferedInputStream(
					new FileInputStream(local));
			client.storeFile("images/"+remote, input);
			input.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	//some more code 
}
{code}


Thanks for your Help.
- Jaruka                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 18 2009
Added on Apr 17 2009
13 comments
1,120 views