借鉴:https://blog.csdn.net/yjclsx/article/details/70675057
1 /** 2 * 调用流程上传文件接口上传文件 3 * @param url 4 * @param path 5 * @return 6 */ 7 public static String sendPostUplodFile(String url,String path) { 8 DataOutputStream out = null; 9 BufferedReader in = null; 10 String result = ""; 11 try { 12 URL realUrl = new URL(url); 13 //打开和URL之间的连接 14 HttpURLConnection conn = (HttpURLConnection)realUrl.openConnection(); 15 //发送POST请求必须设置如下两行 16 conn.setDoOutput(true); 17 conn.setDoInput(true); 18 19 String BOUNDARY = "----WebKitFormBoundary07I8UIuBx6LN2KyY"; 20 conn.setUseCaches(false); 21 conn.setRequestMethod("POST"); 22 conn.setRequestProperty("connection", "Keep-Alive"); 23 // conn.setRequestProperty("user-agent", "Mozilla/4.0 (conpatible; MSIE 6.0; Windows NT 5.1; SV1)"); 24 conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36"); 25 conn.setRequestProperty("Charsert", "UTF-8"); 26 conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); 27 conn.connect(); 28 29 out = new DataOutputStream(conn.getOutputStream()); 30 byte[] end_data = (" --" + BOUNDARY + "-- ").getBytes(); 31 //添加参数 32 StringBuffer sb1 = new StringBuffer(); 33 sb1.append("--"); 34 sb1.append(BOUNDARY); 35 sb1.append(" "); 36 sb1.append("Content-Disposition: form-data;name="luid""); 37 sb1.append(" "); 38 sb1.append(" "); 39 sb1.append("123"); 40 sb1.append(" "); 41 out.write(sb1.toString().getBytes()); 42 //添加参数file 43 File file = new File(path); 44 StringBuffer sb = new StringBuffer(); 45 sb.append("--"); 46 sb.append(BOUNDARY); 47 sb.append(" "); 48 sb.append("Content-Disposition: form-data;name="file";filename="" + file.getName() + """); 49 sb.append(" "); 50 sb.append("Content-Type: application/octet-stream"); 51 sb.append(" "); 52 sb.append(" "); 53 out.write(sb.toString().getBytes()); 54 55 DataInputStream in1 = new DataInputStream(new FileInputStream(file)); 56 int bytes = 0; 57 byte[] bufferOut = new byte[1024]; 58 while ((bytes = in1.read(bufferOut)) != -1) { 59 out.write(bufferOut,0,bytes); 60 } 61 out.write(" ".getBytes()); 62 in1.close(); 63 out.write(end_data); 64 65 //flush输出流的缓冲 66 out.flush(); 67 //定义BufferedReader输入流来读取URL的响应 68 in = new BufferedReader(new InputStreamReader(conn.getInputStream())); 69 String line; 70 while ((line = in.readLine()) != null) { 71 result += line; 72 } 73 } catch (Exception e) { 74 // TODO Auto-generated catch block 75 log.error("发送POST请求出现异常" + e); 76 e.printStackTrace(); 77 }finally { 78 try { 79 if (out != null) { 80 out.close(); 81 } 82 if (in != null) { 83 in.close(); 84 } 85 } catch (Exception ex) { 86 // TODO: handle exception 87 ex.printStackTrace(); 88 } 89 } 90 return result; 91 }
//将字符串转换json并取值
1 public static String getFileId(String result) { 2 JSONObject json = JSONObject.parseObject(result); 3 String fileId = ""; 4 if (result != null) { 5 String code = json.getString("errorCode"); 6 if (code.equals("0000")) { 7 fileId = json.getString("fileId"); 8 } 9 } 10 11 return fileId; 12 }