package com.ccc;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.util.List;
import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
public class GenerateToken {
static FileWriter fw = null;
static PrintWriter pw = null;
static DecimalFormat df = new DecimalFormat("0");
public static void openFile(String str) throws Exception {
File f = new File(str);
fw = new FileWriter(f, true);
pw = new PrintWriter(fw);
}
public static void closeFile() throws Exception {
fw.flush();
pw.close();
fw.close();
}
private static Logger logger = Logger.getLogger(GenerateToken.class);
public static String post(String host, String userAccount, String body) {
String accessToken = "";
ResponseEntity<String> result = null;
try {
URL url = new URL(host);
URLConnection connection = null;
try {
connection = url.openConnection();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
connection.setDoOutput(true);
String auth = userAccount;
String code = new sun.misc.BASE64Encoder().encode(auth.getBytes());
connection.setRequestProperty("Authorization", "Basic" + code);
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.add("Accept", "application/json");
headers.add("Authorization", "Basic " + code);
headers.add("Content-Type", "application/x-www-form-urlencoded");
HttpEntity<String> entity = new HttpEntity<String>(body, headers);
try {
result = restTemplate.exchange(host, HttpMethod.POST, entity,
String.class);
// System.out.print(result);
} catch (Exception e) {
e.printStackTrace();
}
if (result == null) {
logger.info("Fail to get token for user:" + auth);
} else {
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(result.getBody());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (jsonObject != null
&& !jsonObject.getString("access_token").isEmpty()) {
accessToken = jsonObject.getString("access_token");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (MalformedURLException e) {
System.err.println(e.toString());
} catch (IOException e) {
System.err.println(e.toString());
}
return accessToken;
}
public static void main(String[] args) throws NoSuchAlgorithmException, MalformedURLException {
final String host = "https://gateway-staging-sccc.wiredcraft.net/login";
final String body = "grant_type=client_credentials";
final String pwd = ":123456";
String fileReadPath = "D:/3600Dormant3600Registered_forcardlost.xlsx"; //args[0];
String fileGeneratePath = "D:/TestData";//args[1];
String generateNum = "7200"; //args[2];
String userID = null;
String cardNo = null;
File fl = new File(fileGeneratePath);
if(!fl.exists()){
fl.mkdirs();
}
String str = fileGeneratePath + "/UserAccount" + ".csv";
try {
openFile(str);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
List<UserAccountDetail> userAccountList = null;
try {
userAccountList = UserAccountDetail.readFromXLSX2007(fileReadPath, generateNum);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
for(int i=0; i <userAccountList.size(); i ++ ){
userID = userAccountList.get(i).getUserID();
cardNo = userAccountList.get(i).getCardNo();
String userAccount = userID.concat(pwd);
// System.out.println(userAccount);
String jsonstring = post(host,userAccount, body);
System.out.println(jsonstring);
try{
String strPrint = userID + ",\t" + cardNo + ",\t" + jsonstring;
System.out.println(strPrint);
pw.println(strPrint);
pw.flush();
}
catch(Throwable e){
e.printStackTrace();
}
}
try {
closeFile();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.ccc;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.log4j.Logger;
public class UserAccountDetail {
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
public String getCardNo() {
return cardNo;
}
public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}
private static Logger logger = Logger.getLogger(UserAccountDetail.class);
private String userID;
private String cardNo;
public static String sourceFile = "D:/3600Dormant3600Registered_forcardlost.xlsx";
public UserAccountDetail() {
super();
}
public UserAccountDetail(String userID, String cardNo) {
super();
this.userID = userID;
this.cardNo = cardNo;
}
public static List<UserAccountDetail> readFromXLSX2007(String filePath, String generateNum) throws IOException {
File excelFile = null;// Excel文件对象
InputStream is = null;// 输入流对象
String cellStr = null;// 单元格,最终按字符串处理
String cellStr1 = null;
int num = Integer.parseInt(generateNum);
List<UserAccountDetail> userAccountList = new ArrayList<UserAccountDetail>();// 返回封装数据的List
UserAccountDetail userAccount = null;// 每一个SKU信息对象
try {
excelFile = new File(filePath);
is = new FileInputStream(excelFile);// 获取文件输入流
XSSFWorkbook workbook2007 = new XSSFWorkbook(is);// 创建Excel2007文件对象
XSSFSheet sheet = (XSSFSheet) workbook2007.getSheetAt(0);// 取出第一个工作表,索引是0
// 开始循环遍历行,表头不处理,从1开始
for (int i = 1; i <= num+1; i++) {
userAccount = new UserAccountDetail();// 实例化SKUDetail对象
XSSFRow row = sheet.getRow(i);// 获取行对象
int rowNo = i +1;
if (row == null) {// 如果为空,不处理
break;
}
XSSFCell cell = row.getCell(0);// 获取单元格对象
XSSFCell cell1 = row.getCell(2);// 获取单元格对象
if (cell == null || cell1 == null ) {// 单元格为空设置cellStr为空串
logger.info("there is blank on row: " + rowNo);
break;
}
// else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {// 对数字值的处理
// cellStr = df.format(cell.getNumericCellValue());
// }
else{
cellStr = cell.getStringCellValue();
cellStr1 = cell1.getStringCellValue();
}
if (cellStr.equals("")||cellStr == null||cellStr1.equals("")||cellStr1 == null) {// 如果为空格,不处理
logger.info("there is empty on row: " + rowNo);
break;
}
userAccount.userID = cellStr;
userAccount.cardNo = cellStr1;
userAccountList.add(userAccount);// 数据装入List
}
}catch(Throwable e)
{
e.printStackTrace();
}
finally {// 关闭文件流
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return userAccountList;
}
}