java⽣成报⽂_⽣成xml报⽂⽅法并输出
创建根节点
Element root= new Element("root");
//创建报⽂体
Element body = new Element("body");
//添加⼦节点并赋值
body.addContent(new Element("SEQ_NO").setText(""));
//获取已存在的节点并赋值(只能⼀层⼀层的获取)
//获取已存在的节点的值
String str=ChildText("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 = PrettyFormat();
XMLOutputter XMLOut = new XMLOutputter(format);
//XMLOut..setEncoding("utf-8");Doc.setXMLEncoding("gb2312");
//获得WebRoot路径
// ServletConfig servletConfig=(ServletConfig).getServletContext().getAttribute("servletConfig");
String Session().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.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.apachemons.ftp.FTPClient;
import org.apachemons.ftp.FTPFile;
import org.apachemons.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 = Property("ftp_address");
int ftp_port = Integer.Property("ftp_port")); String ftp_username = Property("ftp_username"); String ftp_password = Property("ftp_password"); t(ftp_address,ftp_port); //测试环境FTP
ftp.login(ftp_username,ftp_password); //测试环境⽤户名密码/*t("10.10.123.**",21); //测试FTP地址
ftp.login("test","aaa@2468"); //测试FTP⽤户名密码
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
reply = ReplyCode();
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.login(username,password);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
reply = ReplyCode();
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.Name());
ftp.Name());
String[] files = file.list();
for (int i = 0; i < files.length; i++) {
File file1 = new Path()+"\\"+files[i] );
if(file1.isDirectory()){
upload(file1);
ftp.changeToParentDirectory();
}else{
File file2 = new Path()+"\\"+files[i]); FileInputStream input = new FileInputStream(file2); ftp.Name(), input);
input.close();
}
}
}else{
File file2 = new Path());
FileInputStream input = new FileInputStream(file2); String aString = Name();
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 Path());
FileInputStream input = new FileInputStream(file2);
String saveFileName = Name();
if(changeFileName)
{
//到⽂件的扩展名
int ipos = saveFileName.indexOf(".");
String strExtName = saveFileName.substring(ipos,saveFileName.length()); //⽤新的⽂件名保存上传的⽂件
saveFileName = TimeString()+strExtName;
ftp.storeFile(saveFileName, input);
}
else //否则⽤原有的⽂件名长传⽂件
{
ists()) //如果⽂件存在就返回2
{
return "1";
}
ftp.storeFile(saveFileName, input);
}
input.close();
return saveFileName;
}
else
{
return "0";
}
}
/**
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论