0
長時間的搜索者,第一次詢問堆棧溢出的問題。我似乎總是能夠通過搜索找到我的答案,除了這一次:)Apache Commons FTP Applet在html按鈕上傳
我有點初學Java編程。儘管我認爲使用apache commons ftp的applet是在我的網站上實現FTP文件上傳解決方案的最佳解決方案。
我已經能夠傳輸文件,當我有我的所有代碼在我的init()方法。
雖然當我將我的代碼移動到另一個方法,我在JavaScript內調用它打印工作目錄(PWD)後失敗。
此代碼作品:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication2;
import java.io.IOException;
import javax.swing.JApplet;
import javax.swing.*;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.io.Util;
import org.apache.commons.net.io.CopyStreamAdapter;
import org.apache.commons.net.io.CopyStreamEvent;
import org.apache.commons.net.io.CopyStreamListener;
/**
*
* @author Mike
*/
public class FTPMain extends JApplet {
int userId;
private File[] files;
final JPanel myPanel = new JPanel();
final JLabel lblStatus = new JLabel();
final JProgressBar myProgressBar = new JProgressBar();
//Called when this applet is loaded into the browser.
public void init() {
JButton btnSelectFiles = new JButton("Select Files");
JButton btnUpload = new JButton("Upload Files");
myPanel.add(btnSelectFiles);
myPanel.add(btnUpload);
myPanel.add(lblStatus);
myPanel.add(myProgressBar);
myPanel.setBackground(Color.white);
btnSelectFiles.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
final JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fc.setMultiSelectionEnabled(true);
int returnVal = fc.showOpenDialog(myPanel);
if(returnVal == JFileChooser.APPROVE_OPTION) {
files = fc.getSelectedFiles();
//for(int i = 0; i < files.length; i++) {
lblStatus.setText("File count: " + files.length);
//}
} else {
lblStatus.setText("Open command cancelled by user.");
}
}
});
btnUpload.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lblStatus.setText("Uploading Files...");
final FTPClient ftpclient = new FTPClient();
//ftpclient.setControlKeepAliveTimeout(300);
File myFile;
try {
FTPUtils.ftpConnect(ftpclient, "myhost", "myusername", "mypassword");
//ftpclient.enterLocalPassiveMode();
ftpclient.setFileType(FTP.BINARY_FILE_TYPE);
for(int i = 0; i < files.length; i++) {
String to = "/path/to/files/queue";
ftpclient.changeWorkingDirectory(to);
ftpclient.printWorkingDirectory();
myFile = new File(files[i].getAbsolutePath());
final String remoteFile = myFile.getName();
final InputStream inputStream = new FileInputStream(myFile);
final OutputStream outputStream = ftpclient.storeFileStream(remoteFile);
//byte[] bytesIn = new byte[4096];
//int read = 0;
int streamSize = (int)files[i].getTotalSpace();
Util.copyStream(inputStream, outputStream, 4096, streamSize, myListener);
/*while((read = inputStream.read(bytesIn)) != -1) {
outputStream.write(bytesIn, 0, read);
}*/
inputStream.close();
outputStream.close();
boolean completed = ftpclient.completePendingCommand();
if(completed) {
lblStatus.setText((i+1) + " files have been uploaded successfully.");
}
}
} catch (IOException ex) {
// TODO Auto-generated catch block
lblStatus.setText(ex.getMessage());
//ex.printStackTrace();
}
}
});
getContentPane().add(myPanel);
}
public CopyStreamListener myListener = new CopyStreamListener(){
//private long megsTotal = 0;
public void bytesTransferred(CopyStreamEvent event) {
bytesTransferred(event.getTotalBytesTransferred(), event.getBytesTransferred(), event.getStreamSize());
}
public void bytesTransferred(long totalBytesTransferred,
int bytesTransferred, long streamSize) {
final int percent = (bytesTransferred/(int)streamSize) * 100;
//myProgressBar.setString("Processing " + percent + "%");
//myProgressBar.setValue(percent);
lblStatus.setText("Total: "+percent);
/*long megs = totalBytesTransferred/1000000;
for (long l = megsTotal; l < megs; l++) {
System.err.print("#");
}
megsTotal = megs;
myLabel.setText("Total: " + megsTotal);
myProgressBar.setValue((int)megsTotal);*/
}
};
//private static CopyStreamListener createListener(){
// return
//}
public void startFTPUpload(String strUserInfo) {
//myLabel.setText("UserId " + userId + " is now set.");
}
}
雖然當我移動文件transfering代碼(btnUpload動作偵聽器內)到startFTPUpload方法,並通過JavaScript運行它,它的PWD線失敗後,無一個錯誤。
/* javascript code */
$("#btnUpload").click(function() {
var val = $(this).val();
var myApp = document.applets["FTPUpload"];
if(myApp) {
//alert("found app");
myApp.startFTPUpload(val);
}
});
/* ----------------- */
public void startFTPUpload(String strUserInfo) {
lblStatus.setText("Uploading Files...");
final FTPClient ftpclient = new FTPClient();
//ftpclient.setControlKeepAliveTimeout(300);
File myFile;
try {
FTPUtils.ftpConnect(ftpclient, "myhost", "myusername", "mypassword");
//ftpclient.enterLocalPassiveMode();
ftpclient.setFileType(FTP.BINARY_FILE_TYPE);
for(int i = 0; i < files.length; i++) {
String to = "/path/to/files/queue";
ftpclient.changeWorkingDirectory(to);
ftpclient.printWorkingDirectory();
myFile = new File(files[i].getAbsolutePath());
final String remoteFile = myFile.getName();
final InputStream inputStream = new FileInputStream(myFile);
final OutputStream outputStream = ftpclient.storeFileStream(remoteFile);
//byte[] bytesIn = new byte[4096];
//int read = 0;
int streamSize = (int)files[i].getTotalSpace();
Util.copyStream(inputStream, outputStream, 4096, streamSize, myListener);
/*while((read = inputStream.read(bytesIn)) != -1) {
outputStream.write(bytesIn, 0, read);
}*/
inputStream.close();
outputStream.close();
boolean completed = ftpclient.completePendingCommand();
if(completed) {
lblStatus.setText((i+1) + " files have been uploaded successfully.");
}
}
} catch (IOException ex) {
// TODO Auto-generated catch block
lblStatus.setText(ex.getMessage());
//ex.printStackTrace();
}
//myLabel.setText("UserId " + userId + " is now set.");
}
任何幫助是非常感謝,並記住我是一個Java初學者。我可能沒有使用正確的方法,歡迎提出建議。
謝謝! 邁克