目前官方给了一个java的示例,但是不知道用php怎么写,哪位大侠给指导一下,要求传送一个json文件,下面是java的示例,如果改成php怎么写呢public class SendFileData {
final static String url = "https://wycs.bjjtw.gov.cn/receive/app/common/binapi /XXXXXX";
public static String post(String strURL, Map<String, String> textMap, Map<String, String>
fileMap)
{
String BOUNDARY = "---------------------------123821742118716"; //boundary 就是
request 头和上传文件内容的分隔符
try
{
URL url = new URL(strURL);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
 
 
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept-Charset", "utf-8");
connection.setRequestProperty("contentType", "utf-8");
connection.setRequestProperty("Accept", "text/html");
connection.setRequestProperty("Content-Encoding", "gzip");
connection.setRequestProperty("Content-Type", "multipart/form-data;
charset=UTF-8; boundary=" + BOUNDARY); // 设置发送数据的格式
connection.connect();
OutputStream out = new DataOutputStream(connection.getOutputStream());
if(textMap != null)
{StringBuffer strBuf = new StringBuffer();
Iterator<Map.Entry<String, String>> iter = textMap.entrySet().iterator();
while(iter.hasNext())
{
Map.Entry<String, String> entry = iter.next();
String inputName = (String)entry.getKey();
String inputValue = (String)entry.getValue();
if(inputValue == null)
{
continue;
}
strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
 
 
strBuf.append("Content-Disposition: form-data; name=\""
+ inputName + "\"\r\n\r\n");
strBuf.append(inputValue);
}
out.write(strBuf.toString().getBytes("UTF-8"));
}
if(fileMap != null)
{
Iterator<Map.Entry<String, String>> iter = fileMap.entrySet().iterator();
while(iter.hasNext())
{
Map.Entry<String, String> entry = iter.next();
String inputName = (String)entry.getKey();
String inputValue = (String)entry.getValue();
if(inputValue == null){
continue;
}
File file = new File(inputValue);
String filename = file.getName();
StringBuffer strBuf = new StringBuffer();
strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
strBuf.append("Content-Disposition: form-data; name=\""
+ inputName + "\"; filename=\"" + filename+ "\"\r\n");
strBuf.append("Content-Type:" + "multipart/form-data" + "\r\n\r\n");
 
 
out.write(strBuf.toString().getBytes("UTF-8"));
DataInputStream in = new DataInputStream(new FileInputStream(file));
int bytes = 0;
byte[] bufferOut = new byte[1024];
while((bytes = in.read(bufferOut)) != -1)
{
out.write(bufferOut, 0, bytes);
}
in.close();
}
}
byte[] endData = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
out.write(endData);
out.flush();
out.close();
// 读取响应
int length = (int)connection.getContentLength();// 获取长度
InputStream is = null;
if(responseCode==200){
is = connection.getInputStream();
}else{
is = connection.getErrorStream();
}
if(length != -1)
 
 
{
byte[] data = new byte[length];
byte[] temp = new byte[512];
int readLen = 0;
int destPos = 0;
while((readLen = is.read(temp)) > 0)
{
System.arraycopy(temp, 0, data, destPos, readLen);
destPos += readLen;
}
String result = new String(data, "UTF-8"); // utf-8 编码
return result;
}
}
catch(IOException e)
{
e.printStackTrace();
}
return "error"; // 自定义错误信息
}
public static void main(String[] args)
{
String filepath = "D:/XXXX_CKBA_REQ_20161116212643067.json";
Map<String, String> textMap = new HashMap<String, String>();
 
 
textMap.put("fileName", "XXXX_CKBA_REQ_20161116212643067.json");
Map<String, String> fileMap = new HashMap<String, String>();
fileMap.put("binFile", filepath);
post(url, textMap, fileMap);
}