public static JSONObject doPostFile(String req, String filePath) throws IOException {
JSONObject jsonObject = null;
File file = new File(filePath);
if(!file.exists() || !file.isFile()) {
throw new IOException("文件不存在");
}
StringBuffer sb_result = new StringBuffer();
BufferedReader reader = null;
try {
URL url = new URL(req);
// 打开和URL之间的连接
HttpURLConnection connect = (HttpURLConnection) url.openConnection();
connect.setRequestMethod(REQUEST_METHOD_POST);
connect.setDoInput(true);
connect.setDoOutput(true);
//忽略缓存
connect.setUseCaches(false);
connect.setChunkedStreamingMode(104857600);
connect.setRequestProperty("connection", "Keep-Alive");
connect.setRequestProperty("Charsert", "UTF-8");
String BOUNDARY = "---------" +System.currentTimeMillis();
StringBuffer sb = new StringBuffer();
sb.append("--");
sb.append(BOUNDARY);
sb.append("\r\n");
sb.append("Content-Disposition: form-data; name=\"media\"; filename=\"" + file.getName() + "\"\r\n");
sb.append("Content-Type: application/octet-stream\r\n\r\n");
byte[] head = sb.toString().getBytes("UTF-8");
DataInputStream in = new DataInputStream(new FileInputStream(file));
OutputStream out = new DataOutputStream(connect.getOutputStream());
out.write(head);
int bytes = 0;
byte[] bufferOut = new byte[1024];
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
in.close();
byte[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("UTF-8");
out.write(foot);
reader = new BufferedReader(new InputStreamReader(connect.getInputStream(), "UTF-8"));
out.flush();
out.close();
String line = null;
while ((line = reader.readLine()) != null) {// line为返回值,这就可以判断是否成功、
sb_result.append(line);
}
url = null;
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!"+e);
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
String temp = sb_result.toString();
if (StringUtils.isNotEmpty(temp)) {
jsonObject = JSONObject.fromObject(temp);
}
return jsonObject;
}//调用main方法
String ACCESS_TOKEN = "DhIC_2VMcLAhIh_rB6A_8U1kDj-Q8AQmF5BfcsxUd9-CIM7DsWtJOJGE-y4UMEyJz2z47QxwXZSMFlfthIqiC_9s57nPBsBT_ITFyYoHytMv5BVH8OBdQdGziOgQmAnmPVGeAEALRC";
System.out.println(ACCESS_TOKEN);
String urla = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token="+ACCESS_TOKEN+"&type=image";
System.out.println();
System.out.println(urla);
String filePath = "F:/weixin/1.jpg";
try {
JSONObject jsonObject = doPostFile(urla, filePath);
System.out.println(jsonObject.get("message")+"------------");
} catch (IOException e) {
e.printStackTrace();
}
错误提示
发送 POST 请求出现异常!javax.net.ssl.SSLException: Connection has been shutdown: javax.net.ssl.SSLException: java.net.SocketException: Connection reset by peer: socket write error
javax.net.ssl.SSLException: Connection has been shutdown: javax.net.ssl.SSLException: java.net.SocketException: Connection reset by peer: socket write error
at sun.security.ssl.SSLSocketImpl.checkEOF(SSLSocketImpl.java:1476)
at sun.security.ssl.AppInputStream.read(AppInputStream.java:92)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:275)
at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:687)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:633)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1323)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at cn.canyue.core.util.api.ApiUtil.doPostFile(ApiUtil.java:206)
at cn.canyue.core.util.api.ApiUtil.main(ApiUtil.java:273)
Caused by: javax.net.ssl.SSLException: java.net.SocketException: Connection reset by peer: socket write error
at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1884)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1842)
at sun.security.ssl.SSLSocketImpl.handleException(SSLSocketImpl.java:1806)
at sun.security.ssl.SSLSocketImpl.handleException(SSLSocketImpl.java:1751)
at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:127)
at java.io.BufferedOutputStream.write(BufferedOutputStream.java:122)
at java.io.PrintStream.write(PrintStream.java:480)
at sun.net.www.http.ChunkedOutputStream.flush(ChunkedOutputStream.java:166)
at sun.net.www.http.ChunkedOutputStream.flush(ChunkedOutputStream.java:297)
at java.io.FilterOutputStream.flush(FilterOutputStream.java:140)
at java.io.FilterOutputStream.close(FilterOutputStream.java:157)
at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.close(HttpURLConnection.java:3221)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1257)
... 3 more