参考文章: https://blog.csdn.net/u014204541/article/details/79609619
json
url = new URL(tfsUrl); HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection(); httpConnection.setDoInput(true); httpConnection.setDoOutput(true); httpConnection.setUseCaches(false); httpConnection.setRequestProperty("Content-Type", "application/json"); httpConnection.setRequestMethod("POST"); httpConnection.connect(); DataOutputStream out = new DataOutputStream(httpConnection.getOutputStream()); if (null != fileUploadInfo) { out.writeBytes(JSON.toJSONString(fileUploadInfo)); } out.flush(); out.close(); in = httpConnection.getInputStream(); out1 = new ByteArrayOutputStream(); int temp = 0; while ((temp = in.read()) != -1) { out1.write(temp); } } catch (IOException e) { e.printStackTrace(); } finally { try { out1.close(); } catch (IOException e) { e.printStackTrace(); } } String response = new String(out1.toByteArray()); if (response == null || "".equals(response)) return ""; return response;
x-www-form-urlencoded:
url = new URL(tfsUrl); HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection(); httpConnection.setDoInput(true); httpConnection.setDoOutput(true); httpConnection.setUseCaches(false); httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); httpConnection.connect(); DataOutputStream out = new DataOutputStream(httpConnection.getOutputStream()); String content = "filePath=" + URLEncoder.encode(filePath, "UTF-8"); out.writeBytes(content); out.flush(); out.close(); in = httpConnection.getInputStream(); out1 = new ByteArrayOutputStream(); int temp = 0; while ((temp = in.read()) != -1) { out1.write(temp); } } catch (IOException e) { e.printStackTrace(); } finally { try { out1.close(); } catch (IOException e) { e.printStackTrace(); } } String response = new String(out1.toByteArray()); if (response == null || "".equals(response)) return ""; return response;