📄 writedata.java
字号:
package com.idoc.file;
/**
* <p>Title: IDOC Interface</p>
* <p>Description: IDOC 转换器</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: SiChuan XinHua</p>
* @author Richary
* @version 1.0
*/
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
import com.sap.mw.jco.*;
import com.idoc.util.Operate;
import com.idoc.util.GetProperty;
import java.util.Vector;
import java.net.URL;
import java.io.IOException;
public class WriteData {
// The repository we will be using
private IRepository repository;
private static final String SID = "R3";
public WriteData() {
}
/**
*
* @param vec -- 文件内容
* @param file -- 文件的详细路径和名
* @return
*/
public boolean writeTxtData(Vector vec, String filename, String type) {
GetProperty get = new GetProperty();
//Operate oper = new Operate();
//Properties properties = oper.getProperties("server.properties");
String local = get.getLocalup();
if (type.equalsIgnoreCase("sales")) {
local = local + "/sales/" + filename;
}
else {
local = local + "/inventory/" + filename;
}
try {
FileWriter fw = new FileWriter(local);
BufferedWriter bw = new BufferedWriter(fw);
// System.out.println("writeTxtData aaa");
for (int i = 0; i < vec.size(); i++) {
bw.write(vec.get(i).toString());
bw.newLine();
}
// System.out.println("writeTxtData qqq");
bw.close();
fw.close();
}
catch (Exception ex) {
//
System.out.println("WriteData'writeTxtData is Error!" + ex.toString());
return false;
}
return true;
}
/**
* delete file
* @param filename
* @return
*/
public boolean deleteFile(File file) {
boolean flag = true;
try {
flag = file.delete();
}
catch (Exception ex) {
flag = false;
}
return flag;
}
/**
*
* @param file
* @return
*/
public boolean deleteFile(String filename) {
boolean flag = true;
try {
File file = new File(filename);
flag = file.delete();
}
catch (Exception ex) {
flag = false;
}
return flag;
}
/**
* 创建下传文件的备份目录,每天做一次
* @param type
*/
public static String createDownList(String type) {
URL url = null;
try {
url = com.idoc.file.WriteData.class.getResource("");
}
catch (Exception ex1) {
//
}
String dir = url.toString();
dir = dir.substring(6, 8);
String backDir = "//idoc//outbound//bak//" + type + "//" +
Operate.getNowDate() + "//";
if (! (new File(dir, backDir)).isDirectory()) { // 创建备份目录
new File(dir, backDir).mkdirs();
}
backDir = dir + backDir;
//System.out.println("backDir : " + backDir);
return backDir;
}
/**
* copy File
* @param in
* @param out
* @throws java.lang.Exception
*/
public static void CopyFile(File in, File out) throws Exception {
try {
//FileInputStream fis = null;
//FileOutputStream fos = null;
try {
//in.renameTo(out);
//fis = new FileInputStream(in);
//fos = new FileOutputStream(out);
}
catch (Exception ex) {
System.out.println("WriteData's CopyFile AAA is not File");
//javax.swing.JOptionPane.showMessageDialog(null, "请确认你的网络是否连通!", "网络错误", 0, null);
}
/*
byte[] buf = new byte[1024];
int i = 0;
while ( (i = fis.read(buf)) != -1) {
fos.write(buf, 0, i);
}
//System.out.println("delete : " + in.delete() + " : " + in.toString());
fis.close();
fos.close();
*/
}
catch (Exception ex) {
System.out.println("WriteData's CopyFile BBB is Error!");
ex.printStackTrace();
}
}
/**
* create list
* @param dir
*/
public static void mklist(String dir) {
if (! (new File(dir, "idoc").isDirectory())) {
new File(dir, "idoc").mkdir();
new File(dir, "idoc/outbound/").mkdir();
/*
new File(dir, "idoc/inbound/").mkdir();
new File(dir, "idoc/inbound/bak/").mkdir();
new File(dir, "idoc/inbound/bak/sales/").mkdir();
new File(dir, "idoc/inbound/bak/inventory/").mkdir();
new File(dir, "idoc/inbound/sales/").mkdir();
new File(dir, "idoc/inbound/inventory/").mkdir();
*/
new File(dir, "idoc/outbound/bak/").mkdir();
new File(dir, "idoc/outbound/bak/material/").mkdir();
new File(dir, "idoc/outbound/bak/catalog/").mkdir();
new File(dir, "idoc/outbound/bak/inventory/").mkdir();
new File(dir, "idoc/outbound/bak/ean/").mkdir();
new File(dir, "idoc/outbound/bak/storage/").mkdir();
new File(dir, "idoc/outbound/bak/set/").mkdir();
new File(dir, "idoc/outbound/bak/publish/").mkdir();
// new File(dir, "idoc/outbound/bak/discount/").mkdir();
// new File(dir, "idoc/outbound/material/").mkdir();
// new File(dir, "idoc/outbound/catalog/").mkdir();
// new File(dir, "idoc/outbound/inventory/").mkdir();
// new File(dir, "idoc/outbound/ean/").mkdir();
// new File(dir, "idoc/outbound/storage/").mkdir();
// new File(dir, "idoc/outbound/set/").mkdir();
// new File(dir, "idoc/outbound/publish/").mkdir();
// new File(dir, "idoc/outbound/discount/").mkdir();
}
// 上传数据
if (! (new File(dir, "idoc/outbound/").isDirectory())) {
new File(dir, "idoc/outbound/").mkdir();
}
if (! (new File(dir, "idoc/outbound/bak/").isDirectory())) {
new File(dir, "idoc/outbound/bak/").mkdir();
}
if (! (new File(dir, "idoc/outbound/bak/inventory/").isDirectory())) {
new File(dir, "idoc/outbound/bak/inventory/").mkdir();
}
// 备份目录 -- 下传
if (! (new File(dir, "idoc/outbound/bak/")).isDirectory()) {
new File(dir, "idoc/outbound/bak/").mkdir();
}
if (! (new File(dir, "idoc/outbound/bak/material/")).isDirectory()) {
new File(dir, "idoc/outbound/bak/material/").mkdir();
}
if (! (new File(dir, "idoc/outbound/bak/catalog/")).isDirectory()) {
new File(dir, "idoc/outbound/bak/catalog/").mkdir();
}
if (! (new File(dir, "idoc/outbound/bak/inventory/")).isDirectory()) {
new File(dir, "idoc/outbound/bak/inventory/").mkdir();
}
if (! (new File(dir, "idoc/outbound/bak/ean/")).isDirectory()) {
new File(dir, "idoc/outbound/bak/ean/").mkdir();
}
if (! (new File(dir, "idoc/outbound/bak/storage/")).isDirectory()) {
new File(dir, "idoc/outbound/bak/storage/").mkdir();
}
if (! (new File(dir, "idoc/outbound/bak/set/")).isDirectory()) {
new File(dir, "idoc/outbound/bak/set/").mkdir();
}
if (! (new File(dir, "idoc/outbound/bak/publish/")).isDirectory()) {
new File(dir, "idoc/outbound/bak/publish/").mkdir();
}
if (! (new File(dir, "idoc/outbound/bak/promotion/")).isDirectory()) {
new File(dir, "idoc/outbound/bak/promotion/").mkdir();
}
// 下载获取数据的目录
/*
if (! (new File(dir, "idoc/outbound/material/")).isDirectory()) {
new File(dir, "idoc/outbound/material/").mkdir();
}
if (! (new File(dir, "idoc/outbound/catalog/")).isDirectory()) {
new File(dir, "idoc/outbound/catalog/").mkdir();
}
if (! (new File(dir, "idoc/outbound/inventory/")).isDirectory()) {
new File(dir, "idoc/outbound/inventory/").mkdir();
}
if (! (new File(dir, "idoc/outbound/ean/")).isDirectory()) {
new File(dir, "idoc/outbound/ean/").mkdir();
}
if (! (new File(dir, "idoc/outbound/storage/")).isDirectory()) {
new File(dir, "idoc/outbound/storage/").mkdir();
}
if (! (new File(dir, "idoc/outbound/set/")).isDirectory()) {
new File(dir, "idoc/outbound/set/").mkdir();
}
if (! (new File(dir, "idoc/outbound/publish/")).isDirectory()) {
new File(dir, "idoc/outbound/publish/").mkdir();
}
if (! (new File(dir, "idoc/outbound/promotion/")).isDirectory()) {
new File(dir, "idoc/outbound/promotion/").mkdir();
}
*/
}
private JCO.Client getClientPool() {
JCO.Client client = null;
try {
GetProperty get = new GetProperty();
// get.getPropertiesData();
String clientid = get.getClient();
String userid = get.getUserid();
String passw = get.getPassw();
String language = get.getLanguage();
String host = get.getHost();
String syscode = get.getSyscode();
/*
String clientid = get.getClient();
String userid = get.getUserid();
String passw = get.getPassw();
String language = get.getLanguage();
String host = get.getHost();
String syscode = get.getSyscode();
*/
//System.out.println("clientid = " + clientid + " userid= " + userid + " passw = " + passw + " host= " +host);
// Add a connection pool to the specified system
// The pool will be saved in the pool list to be used
// from other threads by JCO.getClient(SID).
// The pool must be explicitely removed by JCO.removeClientPool(SID)
JCO.addClientPool(SID, // Alias for this pool
10, // Max. number of connections
clientid, // SAP client
userid, // userid
passw, // password
language, // language
host, // host name
syscode);
}
catch (JCO.Exception ex) {
System.out.println("Caught an exception: \n" + ex.toString());
}
catch (Exception ex1) {
System.out.println("Properties read fail");
}
repository = JCO.createRepository("MYRepository", SID);
// Get a client from the pool
client = JCO.getClient(SID);
return client;
}
/**
*
* @param type ---- sales or inventory
* @return
*/
public String writeSAPIdoc(String type, String filename, String job) {
String dir = null;
boolean flag = false;
String idoc_message = null;
JCO.Client client = null;
try {
client = getClientPool();
// Get a function template from the repository
IFunctionTemplate ftemplate = null;
if (job.equalsIgnoreCase("idoc")) {
GetProperty get = new GetProperty();
String f_sales = get.getF_sales();
//String f_sales = get.getF_sales();
String f_inventory = get.getF_inventory();
if (type.equalsIgnoreCase("sales")) {
// 销售数据 Z_IDOC_PROCESS
dir = f_sales + filename;
}
else if (type.equalsIgnoreCase("inventory")) {
// 盘点数据
dir = f_inventory + filename;
}
try {
ftemplate = repository.getFunctionTemplate("Z_IDOC_PROCESS");
}catch(Exception ex) {
System.out.println("ftemplate!");
ex.printStackTrace();
return null;
}
}
else if (job.equalsIgnoreCase("trigger")) {
ftemplate = repository.getFunctionTemplate("Z_IDOC_TRIGGER");
}
if (ftemplate != null) {
// Create a function from the template
JCO.Function function = ftemplate.getFunction();
JCO.ParameterList input = function.getImportParameterList();
if (job.equalsIgnoreCase("idoc")) {
input.setValue(dir, "FILE_NAME");
// Call the remote system
}
client.execute(function);
// Print return message
JCO.Structure ret = function.getExportParameterList().getStructure(
"RETURN");
//System.out.println(ret.getString("TYPE"));
if (ret.getString("TYPE").equalsIgnoreCase("s")) {
flag = true;
}
else {
flag = false;
}
// 如果成功 -- IDOC号码;否则,错误信息
idoc_message = ret.getString("MESSAGE");
// 写日志
//System.out.println("idoc_message " + idoc_message + " id = " + ret.getString("ID") + " NUMBER = " + ret.getString("NUMBER"));
//table.ge
//System.out.println("Message: " + ret.getString("MESSAGE"));
}
}
catch (Exception ex) {
ex.printStackTrace();
return null;
}
finally {
// Release the client to the pool
JCO.releaseClient(client);
JCO.removeClientPool(SID);
}
return idoc_message;
}
public void recursiveRemoveDir(File directory) throws Exception {
String[] filelist = null;
if (!directory.exists()) {
return ;
} else {
filelist = directory.list();
File tmpFile = null;
for (int i = 0; i < filelist.length; i++) {
tmpFile = new File(directory.getAbsolutePath(), filelist[i]);
if (tmpFile.isDirectory()) {
recursiveRemoveDir(tmpFile);
}
else if (tmpFile.isFile()) {
tmpFile.delete();
}
}
directory.delete();
}
}
public static void main(String[] args) {
String flag = "";
WriteData write = new WriteData();
//File file = new File("Z:/inbound/sales/");
try {
//write.recursiveRemoveDir(file);
}catch(Exception ex) {
ex.printStackTrace();
}
//flag = write.writeSAPIdoc("sales","sales901.txt","idoc");
System.out.println("数据处理失败!请检查 " + flag);
flag = write.writeSAPIdoc("sales","sales35680991.txt","trigger");
System.out.println("数据处理失败!请检查 " + flag);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -