|
| 1 | +import javax.activation.MimetypesFileTypeMap; |
| 2 | +import java.io.*; |
| 3 | +import java.net.HttpURLConnection; |
| 4 | +import java.net.URI; |
| 5 | +import java.net.URL; |
| 6 | +import java.util.*; |
| 7 | + |
| 8 | + |
| 9 | +public class HttpClientUtil { |
| 10 | + |
| 11 | + public static void main(String[] args) { |
| 12 | +// Map<String, String> data = new HashMap<>(); |
| 13 | + String url = "http://172.20.112.102:5001/predict"; |
| 14 | + String fileName = "F:\\新建文件夹\\G_W_N\\src\\main\\resources\\bus.jpg"; |
| 15 | + Map<String, String> textMap = new HashMap<String, String>(); |
| 16 | + //可以设置多个input的name,value |
| 17 | + //设置file的name,路径 |
| 18 | + Map<String, String> fileMap = new HashMap<String, String>(); |
| 19 | + fileMap.put("file", fileName); |
| 20 | + String contentType = "";//image/png |
| 21 | + String ret = formUpload(url, textMap, fileMap,contentType); |
| 22 | + System.out.println(ret); |
| 23 | + |
| 24 | + } |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + /** |
| 30 | + * 上传图片 |
| 31 | + * @param urlStr |
| 32 | + * @param textMap |
| 33 | + * @param fileMap |
| 34 | + * @param contentType 没有传入文件类型默认采用application/octet-stream |
| 35 | + * contentType非空采用filename匹配默认的图片类型 |
| 36 | + * @return 返回response数据 |
| 37 | + */ |
| 38 | + @SuppressWarnings("rawtypes") |
| 39 | + public static String formUpload(String urlStr, Map<String, String> textMap, |
| 40 | + Map<String, String> fileMap,String contentType) { |
| 41 | + String res = ""; |
| 42 | + HttpURLConnection conn = null; |
| 43 | + // boundary就是request头和上传文件内容的分隔符 |
| 44 | + String BOUNDARY = "---------------------------123821742118716"; |
| 45 | + try { |
| 46 | + URL url = new URL(urlStr); |
| 47 | + conn = (HttpURLConnection) url.openConnection(); |
| 48 | + conn.setConnectTimeout(5000); |
| 49 | + conn.setReadTimeout(30000); |
| 50 | + conn.setDoOutput(true); |
| 51 | + conn.setDoInput(true); |
| 52 | + conn.setUseCaches(false); |
| 53 | + conn.setRequestMethod("POST"); |
| 54 | + conn.setRequestProperty("Connection", "Keep-Alive"); |
| 55 | + conn.setRequestProperty("Content-Type","multipart/form-data; boundary=" + BOUNDARY); |
| 56 | + OutputStream out = new DataOutputStream(conn.getOutputStream()); |
| 57 | + // text |
| 58 | + if (textMap != null) { |
| 59 | + StringBuffer strBuf = new StringBuffer(); |
| 60 | + Iterator iter = textMap.entrySet().iterator(); |
| 61 | + while (iter.hasNext()) { |
| 62 | + Map.Entry entry = (Map.Entry) iter.next(); |
| 63 | + String inputName = (String) entry.getKey(); |
| 64 | + String inputValue = (String) entry.getValue(); |
| 65 | + if (inputValue == null) { |
| 66 | + continue; |
| 67 | + } |
| 68 | + strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n"); |
| 69 | + strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"\r\n\r\n"); |
| 70 | + strBuf.append(inputValue); |
| 71 | + } |
| 72 | + out.write(strBuf.toString().getBytes()); |
| 73 | + } |
| 74 | + // file |
| 75 | + if (fileMap != null) { |
| 76 | + Iterator iter = fileMap.entrySet().iterator(); |
| 77 | + while (iter.hasNext()) { |
| 78 | + Map.Entry entry = (Map.Entry) iter.next(); |
| 79 | + String inputName = (String) entry.getKey(); |
| 80 | + String inputValue = (String) entry.getValue(); |
| 81 | + if (inputValue == null) { |
| 82 | + continue; |
| 83 | + } |
| 84 | + File file = new File(inputValue); |
| 85 | + String filename = file.getName(); |
| 86 | + |
| 87 | + //没有传入文件类型,同时根据文件获取不到类型,默认采用application/octet-stream |
| 88 | + contentType = new MimetypesFileTypeMap().getContentType(file); |
| 89 | + //contentType非空采用filename匹配默认的图片类型 |
| 90 | + if(!"".equals(contentType)){ |
| 91 | + if (filename.endsWith(".png")) { |
| 92 | + contentType = "image/png"; |
| 93 | + }else if (filename.endsWith(".jpg") || filename.endsWith(".jpeg") || filename.endsWith(".jpe")) { |
| 94 | + contentType = "image/jpeg"; |
| 95 | + }else if (filename.endsWith(".gif")) { |
| 96 | + contentType = "image/gif"; |
| 97 | + }else if (filename.endsWith(".ico")) { |
| 98 | + contentType = "image/image/x-icon"; |
| 99 | + } |
| 100 | + } |
| 101 | + if (contentType == null || "".equals(contentType)) { |
| 102 | + contentType = "application/octet-stream"; |
| 103 | + } |
| 104 | + StringBuffer strBuf = new StringBuffer(); |
| 105 | + strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n"); |
| 106 | + strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"; filename=\"" + filename + "\"\r\n"); |
| 107 | + strBuf.append("Content-Type:" + contentType + "\r\n\r\n"); |
| 108 | + out.write(strBuf.toString().getBytes()); |
| 109 | + DataInputStream in = new DataInputStream(new FileInputStream(file)); |
| 110 | + int bytes = 0; |
| 111 | + byte[] bufferOut = new byte[1024]; |
| 112 | + while ((bytes = in.read(bufferOut)) != -1) { |
| 113 | + out.write(bufferOut, 0, bytes); |
| 114 | + } |
| 115 | + in.close(); |
| 116 | + } |
| 117 | + } |
| 118 | + byte[] endData = ("\r\n--" + BOUNDARY + "--\r\n").getBytes(); |
| 119 | + out.write(endData); |
| 120 | + out.flush(); |
| 121 | + out.close(); |
| 122 | + // 读取返回数据 |
| 123 | + StringBuffer strBuf = new StringBuffer(); |
| 124 | + BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); |
| 125 | + String line = null; |
| 126 | + while ((line = reader.readLine()) != null) { |
| 127 | + strBuf.append(line).append("\n"); |
| 128 | + } |
| 129 | + res = strBuf.toString(); |
| 130 | + reader.close(); |
| 131 | + reader = null; |
| 132 | + } catch (Exception e) { |
| 133 | + System.out.println("发送POST请求出错。" + urlStr); |
| 134 | + e.printStackTrace(); |
| 135 | + } finally { |
| 136 | + if (conn != null) { |
| 137 | + conn.disconnect(); |
| 138 | + conn = null; |
| 139 | + } |
| 140 | + } |
| 141 | + return res; |
| 142 | + } |
| 143 | +} |
0 commit comments