创建根节点
?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="downloaddadan";
?? ??? ??? ?//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","[email?protected]");?? ?//测试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,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,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","[email?protected]");?? ??? ??? ??? ?/*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(); ?? ?}??? ???? ???? ?}