Bom dia Pessoal, estou precisando de uma ajuda:
Tenho um sistema que precisa gerenciar uploads pela WEB, de arquivos grandes, estou trabalhando com um código que no Eclipse ele funciona, mas quando aplico no Html não faz nada.
A função Java que roda o upload é essa:
public void test() {
try {
this.connect("ftp.site.com.br", 21);
this.login("usuario", "senha");
storeFile("/PASTAREMOTA/texto.txt", "C:\\texto.txt");
this.logout();
this.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
Segue o código HTML
<applet code="FtpApplet.class" archive="includes/Ftp.jar" width="300" height ="300">
Segue o código java completo:
package ftp;
import java.applet.Applet;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import javax.swing.JProgressBar;
import org.apache.commons.net.io.CopyStreamEvent;
import org.apache.commons.net.io.CopyStreamListener;
import org.apache.commons.net.ftp.*;
public class FtpApplet extends Applet {
private MyFTPClient ftp = new MyFTPClient();
private int ProgressBarLen = 100;
private int ProgressBarPart = 20;
private JProgressBar progressBar = new JProgressBar(0, ProgressBarLen);
public FtpApplet() {
super();
}
public void destroy() {
// Put your code here
}
public String getAppletInfo() {
return "This is my default applet created by Eclipse";
}
public void init() {
// Put your code here
this.add(progressBar);
this.progressBar.setValue(100);
// this.test();
}
public void start() {
// Put your code here
this.test();
}
public void stop() {
// Put your code here
}
public void connect(String hostname, int port) throws Exception {
ftp.connect(hostname, port);
}
public void disconnect() throws Exception {
ftp.disconnect();
}
public boolean login(String username, String password) throws Exception {
return ftp.login(username, password);
}
public boolean logout() throws Exception {
return ftp.logout();
}
public void setProgress(int value) {
if (this.progressBar.getValue() == value) {
return;
}
this.progressBar.setValue(value);
}
public void setprogressBarPart(int value) {
this.ProgressBarPart = value;
}
public class MyCopyStreamListener implements CopyStreamListener {
FtpApplet m_applet = null;
int m_progress;
MyCopyStreamListener(FtpApplet applet) {
m_applet = applet;
m_progress = 0;
}
public void bytesTransferred(CopyStreamEvent event) {
}
public void bytesTransferred(long totalBytesTransferred,
int bytesTransferred, long streamSize) {
int per = (int) (totalBytesTransferred * m_applet.ProgressBarLen / streamSize);
if (per > m_progress + m_applet.ProgressBarLen
/ m_applet.ProgressBarPart) {
// System.out.println("per: " + per);
m_progress = per;
m_applet.setProgress(m_progress);
}
}
}
public class retrieveFile_PrivilegedAction implements PrivilegedAction {
private String m_remote = null;
private String m_local = null;
FtpApplet m_applet = null;
retrieveFile_PrivilegedAction(String remote, String local,
FtpApplet applet) {
m_remote = remote;
m_local = local;
m_applet = applet;
}
public Object run() {
try {
FTPFile[] ftpfile = ftp.listFiles(m_remote);
// System.out.println("ftpfile count " + ftpfile.length);
if (ftpfile.length != 1) {
return false;
}
long size = ftpfile[0].getSize();
// System.out.println("file length" + size);
MyCopyStreamListener listener = new MyCopyStreamListener(
m_applet);
FileOutputStream fos = new FileOutputStream(m_local);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
boolean ret = ftp.retrieveFile(m_remote, fos, size, listener);
m_applet.setProgress(m_applet.ProgressBarLen);
fos.close();
return ret;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}
}
public boolean retrieveFile(String remote, String local) throws IOException {
retrieveFile_PrivilegedAction privilegedAction = new retrieveFile_PrivilegedAction(
remote, local, this);
return (Boolean) AccessController.doPrivileged(privilegedAction);
}
public class storeFile_PrivilegedAction implements PrivilegedAction {
private String m_remote = null;
private String m_local = null;
FtpApplet m_applet = null;
storeFile_PrivilegedAction(String remote, String local, FtpApplet applet) {
m_remote = remote;
m_local = local;
m_applet = applet;
}
public Object run() {
try {
File file = new File(m_local);
FileInputStream fis = new FileInputStream(file);
long size = file.length();
MyCopyStreamListener listener = new MyCopyStreamListener(
m_applet);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
boolean ret = ftp.storeFile(m_remote, fis, size, listener);
m_applet.setProgress(m_applet.ProgressBarLen);
return ret;
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
return false;
}
}
};
public boolean storeFile(String remote, String local) throws IOException {
storeFile_PrivilegedAction privilegedAction = new storeFile_PrivilegedAction(
remote, local, this);
return (Boolean) AccessController.doPrivileged(privilegedAction);
}
// t e s t
public void test() {
try {
this.connect("ftp.site.com.br", 21);
this.login("usuario", "senha");
storeFile("/PASTAREMOTA/texto.txt", "C:\\texto.txt");
this.logout();
this.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
…
package ftp;
import java.net.Socket;
import org.apache.commons.net.io.CopyStreamEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
import java.io.BufferedOutputStream;
import org.apache.commons.net.io.CopyStreamListener;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTPCommand;
import org.apache.commons.net.io.ToNetASCIIOutputStream;
import org.apache.commons.net.io.Util;
import org.apache.commons.net.ftp.FTP;
import java.io.BufferedInputStream;
import org.apache.commons.net.io.FromNetASCIIInputStream;
public class MyFTPClient extends FTPClient {
MyFTPClient() {
super();
}
private int __fileType = FTP.ASCII_FILE_TYPE;
// setFileType
public boolean setFileType(int fileType, int formatOrByteSize)
throws IOException {
__fileType = fileType;
return super.setFileType(fileType, formatOrByteSize);
}
public boolean setFileType(int fileType) throws IOException {
__fileType = fileType;
return super.setFileType(fileType);
}
/**
* upload to ftp
*/
public boolean storeFile(String remote, InputStream local, long size,
CopyStreamListener listener) throws IOException {
return __storeFile(FTPCommand.STOR, remote, local, size, listener);
}
private boolean __storeFile(int command, String remote, InputStream local,
long size, CopyStreamListener listener) throws IOException {
OutputStream output;
Socket socket;
if ((socket = _openDataConnection_(command, remote)) == null) {
return false;
}
output = new BufferedOutputStream(socket.getOutputStream(),
getBufferSize());
if (__fileType == ASCII_FILE_TYPE) {
output = new ToNetASCIIOutputStream(output);
}
// Treat everything else as binary for now
try {
Util.copyStream(local, output, getBufferSize(), size, listener,
false);
} catch (IOException e) {
try {
socket.close();
} catch (IOException f) {
}
throw e;
}
output.close();
socket.close();
return completePendingCommand();
}
/**
* download from ftp
*/
public boolean retrieveFile(String remote, OutputStream local, long size,
CopyStreamListener listener) throws IOException {
InputStream input;
Socket socket;
if ((socket = _openDataConnection_(FTPCommand.RETR, remote)) == null) {
return false;
}
input = new BufferedInputStream(socket.getInputStream(),
getBufferSize());
if (__fileType == ASCII_FILE_TYPE) {
input = new FromNetASCIIInputStream(input);
}
// Treat everything else as binary for now
try {
Util.copyStream(input, local, getBufferSize(), size, listener,
false);
} catch (IOException e) {
try {
socket.close();
} catch (IOException f) {
}
throw e;
}
socket.close();
return completePendingCommand();
}
}