Reference: https://blog.csdn.net/yjclsx/article/details/70675057
1 / * 2 * upload file interface call flow upload files . 3 * @param URL . 4 * @param path . 5 * @return . 6 * / . 7 public static String sendPostUplodFile (URL String, String path) { . 8 the DataOutputStream OUT = null ; . 9 = in the BufferedReader null ; 10 String Result = "" ; . 11 the try { 12 is the URL realUrl = new new the URL (URL); 13 is //The connection between the opening and the URL 14 the HttpURLConnection Conn = (the HttpURLConnection) realUrl.openConnection (); 15 // send POST request must set the following two lines 16 conn.setDoOutput ( to true ); . 17 conn.setDoInput ( to true ); 18 is . 19 String = the BOUNDARY "---- WebKitFormBoundary07I8UIuBx6LN2KyY" ; 20 is conn.setUseCaches ( to false ); 21 is conn.setRequestMethod ( "the POST" ); 22 is conn.setRequestProperty ( "Connection", "the Keep-Alive" ); 23 is // 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 = ("\r\n--" + BOUNDARY + "--\r\n").getBytes(); 31 //添加参数 32 StringBuffer sb1 = new StringBuffer(); 33 sb1.append("--"); 34 sb1.append(BOUNDARY); 35 sb1.append("\r\n"); 36 sb1.append("Content-Disposition: form-data;name=\"luid\""); 37 sb1.append("\r\n"); 38 sb1.append("\r\n"); 39 sb1.append("123"); 40 sb1.append("\r\n"); 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("\r\n"); 48 sb.append("Content-Disposition: form-data;name=\"file\";filename=\"" + file.getName() + "\""); 49 sb.append("\r\n"); 50 sb.append("Content-Type: application/octet-stream"); 51 sb.append("\r\n"); 52 sb.append("\r\n"); 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 ( "\ R & lt \ n-" .getBytes ()); 62 is in1.close (); 63 is out.write (end_data); 64 65 // buffer flush output stream 66 out.flush (); 67 // defined BufferedReader input stream to retrieve the response of the URL 68 in = new new BufferedReader ( new new the InputStreamReader (conn.getInputStream ())); 69 String Line; 70 the while (! (= in.readLine Line ()) = null ) { 71 is Result = + Line; 72 } 73 is }the catch (Exception E) { 74 // the TODO Auto-Generated Block the catch 75 log.error ( "POST request transmission abnormality" + E); 76 e.printStackTrace (); 77 } the finally { 78 the try { 79 IF (OUT! = null ) { 80 the out.close (); 81 } 82 IF (in =! null ) { 83 in.close (); 84 } 85 } the 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 }