package com.taobao.top.sample.uploadItem;
import java.io.File;
import java.io.IOException;
import java.security.MessageDigest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
/**
* @author liboqi
*
*/
public class UploadItemByZixue {
/**
* @param args
* @throws IOException
* @throws HttpException
*/
// XXX 请更换成 "http://sip.alisoft.com/sip/rest/";
String url = "http://10.2.224.46:8180/sip/rest/";
// String url = "http://sip.alisoft.com/sip/rest/";
// XXX 请更换成自己的secret
String secret = "test_secret";
// XXX 请更换成自己的appkey
String appkey = "100";
public static void main(String[] args) throws HttpException, IOException {
// 新增一个商品,返回新增商品的iid
new UploadItemByZixue().addItemIncludeImage();
}
/**
* 签名方法
*
* @param params
* @param secret
* @return
*/
@SuppressWarnings("unchecked")
public static String signature(Map params, String secret) {
String result = null;
if (params == null)
return result;
params.remove("sip_sign");
Map treeMap = new TreeMap();
treeMap.putAll(params);
Iterator iter = treeMap.keySet().iterator();
StringBuffer orgin = new StringBuffer(secret);
while (iter.hasNext()) {
String name = (String) iter.next();
orgin.append(name).append(params.get(name));
}
try {
MessageDigest md = MessageDigest.getInstance("MD5");
result = byte2hex(md.digest(orgin.toString().getBytes("utf-8")));
} catch (Exception ex) {
throw new java.lang.RuntimeException("sign error !");
}
return result;
}
/**
*
* 二行制转字符串
*
* @param b
*
* @return
*
*/
public static String byte2hex(byte[] b) {
StringBuffer hs = new StringBuffer();
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
hs.append("0").append(stmp);
else
hs.append(stmp);
}
return hs.toString().toUpperCase();
}
private final DateFormat ymdhmsFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
private final String[] EMPTY_STRING_ARRAY = new String[0];
/**
* 添加一个商品,有图片
*
* @param nick
* @throws HttpException
* @throws IOException
*/
public void addItemIncludeImage() throws HttpException, IOException {
Map<String, String> getSign = new HashMap<String, String>();
getSign.put("sip_apiname", "taobao.item.add");
getSign.put("sip_appkey", appkey);
getSign.put("sip_sessionid", "45613");
getSign.put("sip_timestamp", ymdhmsFormat.format(new Date()));
getSign.put("v", "1.0");
getSign.put("type", "fixed");
getSign.put("cid", "1512");
getSign.put("props", "10005:10027;10006:10086;20930:32998;20879:32559");
getSign.put("desc", "just so so");
getSign.put("title", "zixue-test");
getSign.put("num", "12");
getSign.put("price", "101.23");
getSign.put("stuff_status", "new");
getSign.put("approve_status", "instock");
getSign.put("location.state", "浙江");
getSign.put("location.city", "杭州");
Set<String> keySet = getSign.keySet();
String[] names = keySet.toArray(EMPTY_STRING_ARRAY);
Part[] part = new Part[getSign.size() + 2];
int i = 0;
for (; i < getSign.size(); i++) {
StringPart tmp = new StringPart(names[i], getSign.get(names[i]),
"utf-8");
part[i] = tmp;
}
part[i++] = new StringPart("sip_sign", signature(getSign, secret));
part[i] = new FilePart("image", new File(this.getClass().getResource(
"item_pic.jpg").getFile()));
PostMethod postMethod = new PostMethod(url);
MultipartRequestEntity mrp = new MultipartRequestEntity(part,
postMethod.getParams());
postMethod.setRequestEntity(mrp);
// 执行postMethod
HttpClient httpClient = new HttpClient();
httpClient.getParams().setContentCharset("utf-8");
httpClient.executeMethod(postMethod);
Header responseHeader = postMethod.getResponseHeader("sip_status");
if (responseHeader != null) {
String status = responseHeader.getValue();
if ("9999".equals(status)) {
} else if ("1004".equals(status)) {
System.out.println("sip:请在浏览器里访问淘宝登录页面:"
+ postMethod.getResponseHeader("sip_isp_loginurl")
.getValue());
} else {
System.out.println("sip:其他错误:"
+ postMethod.getResponseHeader("sip_error_message")
.getValue());
}
}
System.out.println(postMethod.getResponseBodyAsString());
postMethod.releaseConnection();
}
}
有技术兴趣的 请加28830308群.
1192

被折叠的 条评论
为什么被折叠?



