Skip to main content

upload picture into a frame

2 replies [Last post]
mozum
Offline
Joined: 2012-05-01
Points: 0

Please can any one help me with this. I am trying to upload upload a passport size image in frame of the same size withing a bigger by clicking a button to browse for the picture. I have succeeded in getting a material that will browse for the picture but could not upload into the frame. Can you please help me with the code for uploading the image into the textarea or any frame of that size.
See the code below:

package picture;

import java.awt.*;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.*;

class UploadForm extends JFrame implements ActionListener {
File image;
JTextField server = new JTextField(10);
JTextField username = new JTextField(10);
JTextField imagePath = new JTextField(10);
JTextArea opt = new JTextArea(10,15);
JButton imageBrowse = new JButton("Browse Image");
JPasswordField password = new JPasswordField(10);
JButton uploadButton = new JButton("Upload");

public void actionPerformed(ActionEvent ae) {
if (ae.getSource().equals(imageBrowse)) {
String s;
JFileChooser jfc = new JFileChooser();
jfc.showDialog(jfc, "Choose an image file");
s = jfc.getSelectedFile().getAbsolutePath();
imagePath.setText(s);
} else
if (ae.getSource().equals(uploadButton)) {

try {
File f = new File(imagePath.getText());
String urlString = "ftp://" + username.getText() + ":" + password.getText() + "@" + server.getText() + "/" + f.getName();
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
BufferedOutputStream out = new BufferedOutputStream(connection.getOutputStream());
FileInputStream in = new FileInputStream(f);
byte[] buffer = new byte[1024];
int i = 0;
while ((i = in.read(buffer)) >= 0) {
out.write(buffer, 0, i);
}
out.close();
in.close();
}catch (Exception e) {
System.out.println(e.getMessage());
}
}
}

public UploadForm() {
getContentPane().setBackground(Color.PINK);
setLayout(new FlowLayout());
setSize(500,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel ft = new JPanel();
ft.add(imagePath);
ft.add(imageBrowse);
add("West",ft);
JPanel lt = new JPanel();
lt.add(new Label("Server"));
lt.add(server);
add("West",lt);
JPanel mt = new JPanel();
mt.add(new Label("Username"));
mt.add(username);
add("West",mt);
JPanel nt = new JPanel();
nt.add(new Label("Password"));
nt.add(password);
add("West",nt);
JPanel bt = new JPanel();
bt.add(uploadButton);
add("Center",bt);
JPanel kt = new JPanel();
kt.add(opt);
add("Center",kt);
imageBrowse.addActionListener(this);
uploadButton.addActionListener(this);
setVisible(true);

}

public static void main(String[] args) {
JFrame f = new UploadForm();

}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mozum
Offline
Joined: 2012-05-01
Points: 0

Thanks a lot for bailing me out of this dilemma. Infact the clue you've given me is so fundamental to the project I am attempting. Once again accept my gratitude.

bparker1084
Offline
Joined: 2012-12-04
Points: 0

Here is the complete script for uploading an image into a frame : http://www.c-sharpcorner.com/UploadFile/433c33/image-uploading-in-java/