Java Demo
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import java.io.File;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
/*
{
"retCode":0,
"tid":1035162
"files":
[
{
"fileSize":71007,
"height":480,
"rid":1510777,
"width":320,
"fileName":"1035162_1303205757907.jpg",
"audit":"New",
"orgFileName":"m.jpg",
"isorg":1,
"url":"http://test745.pcauto.com.cn:9192/upcfiles/yidong/1104/19/c0/1035162_1303205757907.jpg"
}
],
}
*/
/*
状态码 值 备注
==========================================================
UPC_OK 0; 成功
UPC_NO_TRANSACTION_SPECIFIED 1; 事务编码未指定
UPC_SPECIFIED_TRANSACTION_NOT_FOUND 2; 事务编码不存在
UPC_CREATE_TRANSACTION_FAILED 3; 创建事务失败
UPC_INVALID_PARAMETERS 4; 非法的参数
UPC_UPLOAD_FAILED 5; 上传失败
UPC_PROCESS_FAILED 6; 处理失败
UPC_TRANSACTION_CLOSED 7; 事务已关闭
UPC_DEL_TRANSACTION_FAILED 8; 删除事务失败
UPC_DEL_RESOURCE_FAILED 9; 资源删除失败
UPC_NO_RESOURCE_SPECIFIED 10; 未指定资源
UPC_UPDATE_RESOURCE_FAILED 11; 资源更新失败
UPC_USER_NOT_LOGON 12; 用户未登录
UPC_INVALID_STATE 13; 非法的状态
UPC_SIZE_LIMITED_EXCEED 14; 文件大小超出规定
UPC_NO_APPLICATION_SPECIFIED 15; 未指定应用编码
UPC_COMMIT_FAILED 16;
UPC_SPECIFIED_APPLICATION_NOT_FOUND 17;
UPC_INVALID_COMMAND 18;
UPC_INVALID_USER 19;
UPC_ERROR_OTHERS 100; 未知错误
*/
class TestUpload {
public static void main(String[] args) {
testUpload();
}
public static void testUpload() throws IOException {
DefaultHttpClient hc = new DefaultHttpClient();
// http://upc.pcauto.com.cn/upload_quick.jsp 正式环境使用
HttpPost post =
new HttpPost(
"http://test745.pconline.com.cn:9192/uploadcenter/upload_quick.jsp?application=yidong");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart(
"file", // 上传form的input标签名称
new FileBody(new File("C:\\data\\path0\\m.jpg")));
// 测试登录Cookie,正式环境不需要用,
// 测试环境获得cookie方法:http://test232.pconline.com.cn:7003/passport2/ 测试环境登录:ddd/123456
post.setHeader("Cookie", "common_session_id1=1E206769DF2AC93174ACCA2C04D86B60B43FD0351F1331A8");
post.setEntity(reqEntity);
HttpResponse response = hc.execute(post);
System.out.println(response.getStatusLine());
System.out.println(EntityUtils.toString(response.getEntity()));
/*
{
"retCode":0,
"tid":1035162
"files":
[
{
"fileSize":71007,
"height":480,
"rid":1510777,
"width":320,
"fileName":"1035162_1303205757907.jpg",
"audit":"New",
"orgFileName":"m.jpg",
"isorg":1,
"url":"http://test745.pcauto.com.cn:9192/upcfiles/yidong/1104/19/c0/1035162_1303205757907.jpg"
}
],
}
*/
post.abort();
}
}