java生成报文_生成xml报文方法并输出
创建根节点
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";
}
}
/**
*
*
删除ftp上的文件
* @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 getFTPFileList(FTPClient ftp) {
ArrayList fileNameList = new ArrayList();
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();
}
}
java生成报文_生成xml报文方法并输出相关推荐
- java 生成瓦片_生成矢量瓦片 | SuperMap iDesktop Java
SuperMap iDesktop 桌面产品支持将矢量地图生成遵循 MapBox MVT 规范的矢量瓦片(.mvt),瓦片数据包含要素的几何形状与属性信息,而要素的风格样式由 MapBox 标准样式文 ...
- JAXB根据带继承关系的类生成soap请求的XML报文(互转)
文章目录 生成的请求报文与需要解析的响应报文 使用Apache CXF或者jdk自带的wsimport生成客户端代码 不生成客户端代码 类的继承关系 包结构与package-info.java 将子类 ...
- java递增序列号_生成Java自动递增序列号日食
我设计在Java中,其中文本框是there.so我想在这个文本框显示的序列号...一种形式是指当前文本框的值应该是1..then我会填补这些表格和提交...当我回来的时候textfield的值应该是2 ...
- java高斯分布随机数_生成符合高斯分布或者其他任意分布的随机数
在一些情况下经常需要用到随机数,而高斯随机数又是最常用到的.这一篇讲一下如何编程生成符合正态分布的高斯随机数,甚至任何其他分布的随机数. 我们知道C语言的标准库函数可以生成符合均匀分布的伪随机数.那么 ...
- Java解析银联报文_银联ISO8583报文解析过程
主密钥: aabbccddeeff11223344556677889900 1.从签到报文中获取工作密钥,包括MACKEY明文,PINKEY明文 签到: 12-03-31 16:38:09----&g ...
- java生成访问令牌_生成个人访问令牌以调用Azure Web Api(OAuth)
我有一个ASP.NET Web API,我在Azure Active Directory下注册了一个新的应用程序 . 这就是ConfigureAuth在Web API的源代码中的方式 . public ...
- JVM - 结合代码示例彻底搞懂Java内存区域_线程栈 | 本地方法栈 | 程序计数器
文章目录 Pre 运行时数据区总览 线程栈 概要 栈内部主要组成部分 局部变量 操作数栈 动态链接 方法出口 小结 程序计数器 本地方法栈 附 测试demo javap JVM字节码指令集手册 Pre ...
- json报文转化为xml报文_JSON与XML互相转化(Jackson)
选型: json-lib.jar可以做,但是太老了,抛弃: fastjson完全没有xml相关功能: staxon经测试不能用. 最终选用jackson来主要实现. JDK为1.8,如果低于1.8,则 ...
- 在线生成艺术字_生成艺术:如何修改绘画
在线生成艺术字 世界是由物理,生物和化学过程创造的,因此从根本上来说是生成的. 但是它不是由干净的数学线组成的. 相反,这是数学模式与熵之间的斗争. 生成艺术反映了现实的两个方面,为世界提供了独特的视 ...
最新文章
- Windows 8.1内置微软五笔输入法
- 【script】python3使用http.server搭建简易web服务
- 【TensorFlow】TensorFlow函数精讲之 tf.random_normal()
- java开闭原则实现代码_向对象的六大原则
- 【bzoj4419】[Shoi2013]发微博 STL-set
- React中的三种类型组件介绍
- 最新伯乐PHP个人在线自动发卡网源码V3.1
- 【AI视野·今日CV 计算机视觉论文速览 第178期】Fri, 17 Jan 2020
- 双系统彻底删除Ubuntu启动项
- 土豆运行linux,安装到土豆上的 Linux 是怎么个意思?
- web of science /pubmed主要可检索字段
- python pdf文件处理
- html怎么给图片加倒影,用CSS3实现图片倒影效果,同时给出兼容早期版本IE的方案...
- Python3.9标准库math中的函数汇总介绍(53个函数和5个常数)
- Robocup新手指南
- 技术话题(2)实时通讯RT和同步实时通讯IRT的区别
- app自动化测试——app自动化控制、常见控件定位方法
- 独立个人博客有什么用?建独立博客有何意义?
- 段错误需要使用的工具 nm objdump readelf
- 【CenterNet】模型文件resnet101-5d3b4d8f.pth下载