thaks for yours anwswers..
harmmeijer: the applet is signed ok, I dont think that is the problem...
itchyscratchy: the server calls are made from start() method. The applet is crashed during its sending files to FTP server, when finish, the applet look ok.
The class I use is FtpBean: http://www.geocities.com/SiliconValley/Code/9129/javabean/ftpbean/
(I test too with apache commons-net, and the same effect...)
The ftp is Filezilla in localhost.
This is the code, I explain a little:
The start() method calls iniciar() method where its is defined the array of files to upload, and connect to ftp server. The for loop on every element of array and uploads a file on subirFichero() method.
Basicaly its this.
The HTML code is:
<applet
codebase = "."
code= "revelado.Upload.class"
archive = "revelado.jar"
name= "Revelado"
width= "750"
height= "415"
hspace= "0"
vspace= "0"
align= "middle"
>
<PARAM NAME="usern" VALUE="username">
</applet>
package revelado;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.io.*;
import javax.swing.border.*;
import java.net.*;
import ftp.*;
public class Upload
extends Applet {
private boolean isStandalone = false;
JPanel jPanel1 = new JPanel();
JLabel jLabel1 = new JLabel();
JLabel jlmensaje = new JLabel();
JLabel jlarchivo = new JLabel();
TitledBorder titledBorder1;
TitledBorder titledBorder2;
//mis variables
String DIRECTORIOHOME = System.getProperty("user.home");
String[] fotos_sel = new String[1000]; //array of selected images
int[] indice_tamano = new int[1000]; //array of sizes
int[] indice_cantidad = new int[1000]; //array of quantitys
int num_fotos_sel = 0; //number of selected images
double importe = 0; //total prize
double[] precios_tam = {
0.12, 0.39, 0.60, 1.50};
//prizes
String server = "localhost";
String username = "pepe";
String password = "pepe01";
String nombreusuario = null;
JProgressBar jProgreso = new JProgressBar();
//Obtener el valor de un par醡etro
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
//Construir el applet
public Upload() {
}
//Inicializar el applet
public void init() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
//Inicializaci髇 de componentes
private void jbInit() throws Exception {
titledBorder1 = new TitledBorder("");
titledBorder2 = new TitledBorder("");
this.setLayout(null);
jPanel1.setBackground(Color.lightGray);
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel1.setBounds(new Rectangle(113, 70, 541, 151));
jPanel1.setLayout(null);
jLabel1.setFont(new java.awt.Font("Dialog", 1, 16));
jLabel1.setText("Subiendo archivos al servidor");
jLabel1.setBounds(new Rectangle(150, 26, 242, 15));
jlmensaje.setFont(new java.awt.Font("Dialog", 0, 10));
jlmensaje.setForeground(Color.red);
jlmensaje.setHorizontalAlignment(SwingConstants.CENTER);
jlmensaje.setText(
"Por favor, no cierre esta ventana hasta que termine de subir todas " +
"las fotos");
jlmensaje.setBounds(new Rectangle(59, 49, 422, 30));
jlarchivo.setBackground(Color.white);
jlarchivo.setBorder(titledBorder2);
jlarchivo.setHorizontalAlignment(SwingConstants.CENTER);
jlarchivo.setBounds(new Rectangle(16, 85, 508, 24));
jProgreso.setForeground(new Color(49, 226, 197));
jProgreso.setBounds(new Rectangle(130, 121, 281, 18));
jPanel1.add(jlmensaje, null);
jPanel1.add(jlarchivo, null);
jPanel1.add(jProgreso, null);
jPanel1.add(jLabel1, null);
this.add(jPanel1, null);
nombreusuario = getParameter("usern");
}
//Iniciar el applet
public void start() {
jlarchivo.setText("Start() method...");
iniciar();
}
public void iniciar() {
//init images selected array
fotos_sel[0] = "C:/fotos/05160009.JPG";
fotos_sel[1] = "C:/fotos/05160010.JPG";
fotos_sel[2] = "C:/fotos/05160011.JPG";
// etc...
num_fotos_sel=3; //number of selected images
//conectar al ftp (instanciar clase FtpExample)
FtpExample miftp = new FtpExample();
miftp.connect();
//make the directory
subirpedido(miftp);
jProgreso.setMinimum(0);
jProgreso.setMaximum(num_fotos_sel);
for (int i = 0; i < num_fotos_sel; i++) {
jlarchivo.setText(fotos_sel[i]);
jProgreso.setValue(i);
subirFichero(miftp, fotos_sel[i]);
try {
Thread.sleep(1000);
}
catch (InterruptedException ex) {
//salida(ex.toString());
}
}
jlarchivo.setText("Proceso finalizado correctamente");
jProgreso.setValue(num_fotos_sel);
miftp.close();
}
//Detener el applet
public void stop() {
}
//Destruir el applet
public void destroy() {
}
//Obtener informaci髇 del applet
public String getAppletInfo() {
return "Subir ficheros al server";
}
//Obtener informaci髇 del par醡etro
public String[][] getParameterInfo() {
return null;
}
//sube al ftp (a la carpeta del usuario) el archivo
//pedido.txt que tiene las lineas del pedido
public void subirpedido(FtpExample miftp) {
jlarchivo.setText("Iniciando la conexi髇...");
//make folder of user
miftp.directorio("www/usuarios/" + nombreusuario);
}
//uploads a file
public void subirFichero(FtpExample miftp, String nombre) {
//remote name:
String nremoto = "";
int lr = nombre.lastIndexOf("\\");
if (lr<0){
lr = nombre.lastIndexOf("/");
}
nremoto = nombre.substring(lr + 1);
String archivoremoto = "www/usuarios/" + nombreusuario + "/" + nremoto;
//upload file
miftp.subir(nombre, archivoremoto);
}
}
class FtpExample
implements FtpObserver {
FtpBean ftp;
long num_of_bytes = 0;
public FtpExample() {
// Create a new FtpBean object.
ftp = new FtpBean();
}
// Connect to a ftp server.
public void connect() {
try {
ftp.ftpConnect("localhost", "pepe", "pepe01");
}
catch (Exception e) {
System.out.println(e);
}
}
// Close connection
public void close() {
try {
ftp.close();
}
catch (Exception e) {
System.out.println(e);
}
}
// Go to directory pub and list its content.
public void listDirectory() {
FtpListResult ftplrs = null;
try {
// Go to directory
ftp.setDirectory("/");
// Get its directory content.
ftplrs = ftp.getDirectoryContent();
}
catch (Exception e) {
System.out.println(e);
}
// Print out the type and file name of each row.
while (ftplrs.next()) {
int type = ftplrs.getType();
if (type == FtpListResult.DIRECTORY) {
System.out.print("DIR\t");
}
else if (type == FtpListResult.FILE) {
System.out.print("FILE\t");
}
else if (type == FtpListResult.LINK) {
System.out.print("LINK\t");
}
else if (type == FtpListResult.OTHERS) {
System.out.print("OTHER\t");
}
System.out.println(ftplrs.getName());
}
}
// Implemented for FtpObserver interface.
// To monitor download progress.
public void byteRead(int bytes) {
num_of_bytes += bytes;
System.out.println(num_of_bytes + " of bytes read already.");
}
// Needed to implements by FtpObserver interface.
public void byteWrite(int bytes) {
}
//crea un directorio
public void directorio(String nombre) {
try {
ftp.makeDirectory(nombre);
}
catch (Exception e) {
System.out.println(e);
}
}
public void subir(String local, String remoto) {
try {
ftp.putBinaryFile(local, remoto);
}
catch (Exception e) {
System.out.println(e);
}
}
// Main
public static void main(String[] args) {
FtpExample example = new FtpExample();
example.connect();
example.directorio("raul");
example.listDirectory();
example.subir("C:/fotos/05160009.JPG", "/raul/foto1.jpg");
//example.getFile();
example.close();
}
}
When using jre jre1.5.0_06 I have no crashing IE.
"Uploading" to localhost does give IE 100% CPU and other programs
including IE respond verry slow (the PC is too bussy copying a large
file). Putting a sleep in the wile(file read output write) loop makes it
all a lot more responsive but it takes longer to "upload".
Here is the code I used (just one file).
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.io.*;
import javax.swing.border.*;
import java.net.*;
public class test
extends Applet {
private boolean isStandalone = false;
JPanel jPanel1 = new JPanel();
JLabel jLabel1 = new JLabel();
JLabel jlmensaje = new JLabel();
JLabel jlarchivo = new JLabel();
TitledBorder titledBorder1;
TitledBorder titledBorder2;
//mis variables
String DIRECTORIOHOME = System.getProperty("user.home");
String[] fotos_sel = new String[1000]; //array of selected images
int[] indice_tamano = new int[1000]; //array of sizes
int[] indice_cantidad = new int[1000]; //array of quantitys
int num_fotos_sel = 0; //number of selected images
double importe = 0; //total prize
double[] precios_tam = {
0.12, 0.39, 0.60, 1.50};
//prizes
String server = "localhost";
String username = "pepe";
String password = "pepe01";
String nombreusuario = null;
JProgressBar jProgreso = new JProgressBar();
//Inicializar el applet
public void init() {
// do nothing
}
//Inicializaci髇 de componentes
public void start() {
titledBorder1 = new TitledBorder("");
titledBorder2 = new TitledBorder("");
this.setLayout(null);
jPanel1.setBackground(Color.lightGray);
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel1.setBounds(new Rectangle(113, 70, 541, 151));
jPanel1.setLayout(null);
jLabel1.setFont(new java.awt.Font("Dialog", 1, 16));
jLabel1.setText("Subiendo archivos al servidor");
jLabel1.setBounds(new Rectangle(150, 26, 242, 15));
jlmensaje.setFont(new java.awt.Font("Dialog", 0, 10));
jlmensaje.setForeground(Color.red);
jlmensaje.setHorizontalAlignment(SwingConstants.CENTER);
jlmensaje.setText(
"Por favor, no cierre esta ventana hasta que termine de subir todas " +
"las fotos");
jlmensaje.setBounds(new Rectangle(59, 49, 422, 30));
jlarchivo.setBackground(Color.white);
jlarchivo.setBorder(titledBorder2);
jlarchivo.setHorizontalAlignment(SwingConstants.CENTER);
jlarchivo.setBounds(new Rectangle(16, 85, 508, 24));
jProgreso.setForeground(new Color(49, 226, 197));
jProgreso.setBounds(new Rectangle(130, 121, 281, 18));
jProgreso.setMaximum(100);
jProgreso.setMinimum(0);
jPanel1.add(jlmensaje, null);
jPanel1.add(jlarchivo, null);
jPanel1.add(jProgreso, null);
jPanel1.add(jLabel1, null);
this.add(jPanel1, null);
nombreusuario = getParameter("usern");
new Thread(new ftp()).start();
}
private void setVal(int val){
jProgreso.setValue(val);
}
class ftp implements Runnable{
public void run() {
// if the ftp url points to a directory (not a file) a dirlist is returned
// ftp://username:password@ftp.yourserver.com/myFolder;type=a"
// to retreive a binary file you have to provide the following url:
// ftp://username:password@ftp.yourserver.com/yourBinaryFile.zip;type=i"
// forcing it to use ASCII you can do:
// ftp://username:password@ftp.yourserver.com/yourBinaryFile.zip;type=a"
try{
File f = new File("C:\\test.txt"); // file is 32 MB
FileInputStream fi = new FileInputStream(f);
byte[] b = new byte[56000];
URL u = new URL("ftp://anonymous:pwd@localhost/myfile.txt;type=i");
// myfile.txt does not exist or is overridden
URLConnection c = u.openConnection();
c.setDoInput(false);
OutputStream o = c.getOutputStream();
double total = f.length();
double done = 0d;
double d = (done / total) * 100;
int i = (new Double(d)).intValue();
while(fi.read(b)>-1){
o.write(b);
// here IExplore takes a lot of CPU
done = done + b.length;
d = (done / total) * 100;
i = (new Double(d)).intValue();
test.this.setVal(i);
// IExplore does not take 100%
// but it takes a long long time to upload
// for a remote host you can use sleep
Thread.sleep(100);
}
o.close();
i = (new Double(d)).intValue();
test.this.setVal(i);
}catch(Exception e1){
e1.printStackTrace();
}
}
}
}