創建根節點
Element root= new Element("root");
//創建報文體
Element body = new Element("body");
//添加子節點並賦值
body.addContent(new Element("SEQ_NO").setText(""));
//獲取已存在的節點並賦值(只能一層一層的獲取)
body.getChild("SEQ_NO").getChild("SEQ_NO2").setText(""));;
//獲取已存在的節點的值
String str=Message_Body.getChildText("EP_DEC_HEAD");
//根節點(報文)添加到文檔中;
Document Doc = new Document(root);
//自定義報文名字
String FileName = "EMVS_EP_3120980024_"+hm.get("CONTR_NO")+"_"+BIZ_TYPE+"0_"+nowTime+".DEC";
//輸出報文到項目WebRoot目錄
Format format = Format.getPrettyFormat();
XMLOutputter XMLOut = new XMLOutputter(format);
//XMLOut..setEncoding("utf-8");Doc.setXMLEncoding("gb2312");
//獲得WebRoot路徑
// ServletConfig servletConfig=(ServletConfig).getServletContext().getAttribute("servletConfig");
String webRootPath=request.getSession().getServletContext().getRealPath("/");
String uploadPath="download\\dadan\\";
//uploadPath附加傳入的路徑,組成一個上傳的完整路徑
uploadPath=webRootPath+uploadPath;
XMLOut.output(Doc, new FileOutputStream(uploadPath+fileName));
//利用ftp發送文件到指定文件夾
File file = new File(uploadPath+fileName);
FTPFunc fTPFunc = new FTPFunc();
fTPFunc.connect("send");
fTPFunc.upload(file);
ftp連接,上傳,下載工具類
package com.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import seastar.servlet.demand.Utils;
@SuppressWarnings("all")
public class FTPFunc {
private static FTPClient ftp;
/**
* 使用默認服務器和端口進行連接
* @param path 上傳到ftp服務器哪個路徑下
* @return
* @throws Exception
*/
public boolean connect(String path) throws Exception {
boolean result = false;
ftp = new FTPClient();
int reply;
String ftp_address = Utils.getProperty("ftp_address");
int ftp_port = Integer.parseInt(Utils.getProperty("ftp_port"));
String ftp_username = Utils.getProperty("ftp_username");
String ftp_password = Utils.getProperty("ftp_password");
ftp.connect(ftp_address,ftp_port); //測試環境FTP
ftp.login(ftp_username,ftp_password); //測試環境用戶名密碼
/*ftp.connect("10.10.123.**",21); //測試FTP地址
ftp.login("test","aaa@2468"); //測試FTP用戶名密碼
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
ftp.changeWorkingDirectory(path);
result = true;
return result;
}
/**
*
* @param path 上傳到ftp服務器哪個路徑下
* @param addr 服務器的IP地址
* @param port 端口號
* @param username 用戶名
* @param password 密碼
* @return
* @throws Exception
*/
public boolean connect(String path,String addr,int port,String username,String password) throws Exception {
boolean result = false;
ftp = new FTPClient();
int reply;
ftp.connect(addr,port);
ftp.login(username,password);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
ftp.changeWorkingDirectory(path);
result = true;
return result;
}
/**
*
* @param file 上傳的文件或文件夾
* @throws Exception
*/
public void upload(File file) throws Exception{
if(file.isDirectory()){
ftp.makeDirectory(file.getName());
ftp.changeWorkingDirectory(file.getName());
String[] files = file.list();
for (int i = 0; i < files.length; i++) {
File file1 = new File(file.getPath()+"\\"+files[i] );
if(file1.isDirectory()){
upload(file1);
ftp.changeToParentDirectory();
}else{
File file2 = new File(file.getPath()+"\\"+files[i]);
FileInputStream input = new FileInputStream(file2);
ftp.storeFile(file2.getName(), input);
input.close();
}
}
}else{
File file2 = new File(file.getPath());
FileInputStream input = new FileInputStream(file2);
String aString = file2.getName();
ftp.storeFile(aString, input);
input.close();
}
}
/**
* 上傳單個文件
* @param file 需要上傳的文件
* @param changeFileName 是否需要變更文件的名稱
* @return 返回FTP上文件的名稱
* @throws Exception
*/
public String upload(File file, boolean changeFileName) throws Exception{
if(file.isFile()){
File file2 = new File(file.getPath());
FileInputStream input = new FileInputStream(file2);
String saveFileName = file2.getName();
if(changeFileName)
{
//找到文件的擴展名
int ipos = saveFileName.indexOf(".");
String strExtName = saveFileName.substring(ipos,saveFileName.length());
//用新的文件名保存上傳的文件
saveFileName = PBMeth.getTimeString()+strExtName;
ftp.storeFile(saveFileName, input);
}
else //否則用原有的文件名長傳文件
{
if(file.exists()) //如果文件存在就返回2
{
return "1";
}
ftp.storeFile(saveFileName, input);
}
input.close();
return saveFileName;
}
else
{
return "0";
}
}
/**
*
* <p>刪除ftp上的文件</p>
* @param srcFname
* @return true || false
*/
public boolean removeFile(String srcFname){
boolean flag = false;
if(ftp != null)
{
try
{
flag = ftp.deleteFile(srcFname);
}
catch (IOException e)
{
e.printStackTrace();
}
}
return flag;
}
/**
* 連接到FTP的方法
* @param addr 服務器的IP地址
* @param port 端口號
* @param username 用戶名
* @param password 密碼
* @param path FTP的目錄
* @return
* @throws Exception
*/
public boolean getConnectionFTP(String addr,int port,String username,String password,String path,FTPClient ftp) throws Exception {
boolean result = false;
int reply;
try {
ftp.connect(addr,port);
ftp.login(username,password);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
ftp.changeWorkingDirectory(path);
result = true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
return result;
}
//獲得文件列表
public ArrayList<String> getFTPFileList(FTPClient ftp) {
ArrayList<String> fileNameList = new ArrayList<String>();
try {
FTPFile[] fs = ftp.listFiles();//獲得文件列表
for (FTPFile file : fs) {
fileNameList.add(file.getName());//獲得文件名稱組成的list
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return fileNameList;
}
//是否成下載FTP文件到本地
public Boolean downloadFromFTP(String localPath,String fileName,FTPClient ftp) {
try {
File localFile = new File(localPath+fileName);
OutputStream is = new FileOutputStream(localFile);
ftp.retrieveFile(fileName, is);//循環取得文件並寫到指定本地路徑
is.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
//刪除指定名稱的FTP上的文件
public Boolean deleteFromFTP(String path,String fileName,FTPClient ftp) {
boolean result = true;
try {
result = ftp.deleteFile(path+fileName);//刪除FTP上的文件
} catch (Exception e) {
e.printStackTrace();
return false;
}
return result;
}
public void doConnectAndUpload(String string, File file) throws Exception{
FTPClient ftp1 = new FTPClient();
int reply;
ftp1.connect("120.55.192.123",21);
ftp1.login("administrator","Regs@2016");
/*ftp1.connect("61.152.176.30",21);
ftp1.login("EMVSXZGJ","EMVS123");*/
ftp1.setFileType(FTPClient.BINARY_FILE_TYPE);
ftp1.enterLocalPassiveMode();
reply = ftp1.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp1.disconnect();
return;
}
//ftp1.changeWorkingDirectory(string);
File file2 = new File(file.getPath());
FileInputStream input = new FileInputStream(file2);
String aString = file2.getName();
//ftp1.setControlEncoding("UTF-8");
boolean aa = ftp1.storeFile(new String(aString.getBytes("UTF-8"),"iso-8859-1"),input);//iso-8859-1
System.out.println(aa);
boolean a = ftp1.storeFile(aString, input);
System.out.println(a);
input.close();
ftp1.logout();
}
}