最初に、primefaces を介して一時ディレクトリに HTTP アップロードを行います。次に、org.apache.commons.net.ftp.FTPClient または sun.net.ftp.FtpClient を介して、必要な FTP サーバーにアップロードします。
以下は例です。
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import sun.net.ftp.FtpClient;
/**
*
* @author fali
*/
public class FtpUtil {
public String server, username,password, remote, remotedir, local;
FtpClient ftp;
public static int BUFFER_SIZE = 10240;
public FtpUtil(){
server = "localhost";
username = "anonymous";
password = " ";
remotedir = "/incoming";
remote = "dvs.txt";
local = "C:\\dvs.txt";
}
protected void putFile() {
if (local.length() == 0) {
System.out.println("Please enter file name");
}
byte[] buffer = new byte[BUFFER_SIZE];
try {
File f = new File(local);
int size = (int) f.length();
System.out.println("File " + local + ": " + size + " bytes");
System.out.println(size);
FileInputStream in = new FileInputStream(local);
OutputStream out = ftp.put(remote);
int counter = 0;
while (true) {
int bytes = in.read(buffer);
if (bytes < 0)
break;
out.write(buffer, 0, bytes);
counter += bytes;
System.out.println(counter);
}
out.close();
in.close();
} catch (Exception ex) {
System.out.println("Error: " + ex.toString());
}
}
public String Upload(){
String result="";
try{
ftp = new FtpClient(server);
ftp.login(username, password);
System.out.println(ftp.welcomeMsg);
ftp.cd(remotedir);
putFile();
disconnect();
}catch(Exception ex){
System.out.println(ex);
result = "Error : "+ex;
}
return "";
}
protected void disconnect() {
if (ftp != null) {
try {
ftp.closeServer();
} catch (IOException ex) {
}
ftp = null;
}
}
}
マネージドBean/コントローラーで;
public String create() {
System.out.println("Request Button Clicked");
try {
// generate reference number
//current.setReferenceno(genReferenceNo());
// add to database
//getFacade().persist(current);
// upload to ftp
FtpUtil fu = new FtpUtil();
fu.Upload();
// show reference number
JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("QueueCreated"));
JsfUtil.addSuccessMessage("Your Reference No. is :" + current.referenceno);
current = null;
// try {
// System.out.println("Redirecting");
// FacesContext.getCurrentInstance().getExternalContext().dispatch("/");
// } catch (Exception ex) {
// System.out.println(ex);
// }
return "";
} catch (Exception e) {
JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
return null;
}
}
そして、あなたのページにこのようなものがあります。
<br />
<ppctu:commandButton action="#{appointmentController.create}" type="Submit" value="Request" />