
public static String postFormData(String url, Mapmap) throws Exception { BufferedReader in = null; URL urls = new URL(url); HttpURLConnection connection = null; OutputStream outputStream = null; String rs = ""; try { connection = (HttpURLConnection) urls.openConnection(); connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=----footfoodapplicationrequestnetwork"); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8"); connection.setRequestProperty("Accept", "*/*"); connection.setRequestProperty("Range", "bytes=" + ""); connection.setConnectTimeout(8000); connection.setReadTimeout(20000); connection.setRequestMethod("POST"); StringBuffer buffer = new StringBuffer(); outputStream = connection.getOutputStream(); Set > entries = map.entrySet(); for (Entry entry : entries) { // 每次都清空buffer,避免写入上次的数据 buffer.delete(0, buffer.length()); buffer.append("------footfoodapplicationrequestnetworkrn"); Object value = entry.getValue(); if (!(value instanceof File)) { buffer.append("Content-Disposition: form-data; name=""); buffer.append(entry.getKey()); buffer.append(""rnrn"); buffer.append(entry.getValue()); buffer.append("rn"); outputStream.write(buffer.toString().getBytes()); // System.out.println(buffer.toString()); } else { buffer.append("Content-Disposition: form-data; name="" + entry.getKey() + ""; filename="" + ((File) entry.getValue()).getName() + ""rn"); buffer.append("Content-Type: " + "zip" + "rnrn"); // System.out.println(buffer.toString()); outputStream.write(buffer.toString().getBytes()); File file = (File) entry.getValue(); DataInputStream ins = new DataInputStream(new FileInputStream(file)); int bytes = 0; byte[] bufferOut = new byte[1024]; while ((bytes = ins.read(bufferOut)) != -1) { outputStream.write(bufferOut, 0, bytes); // System.out.print("A"); } // 文件流后面添加换行,否则文件后面的一个参数会丢失 outputStream.write("rn".getBytes()); // System.out.println("rn"); } } if (entries != null && map.size() > 0) { buffer.delete(0, buffer.length()); buffer.append("------footfoodapplicationrequestnetwork--rn"); // System.out.println(buffer.toString()); } outputStream.write(buffer.toString().getBytes()); try { connection.connect(); if (connection.getResponseCode() == 200) { in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); String line = ""; while ((line = in.readLine()) != null) { rs += line; } } } catch (Exception e) { System.out.println("发生异常"); rs = null; } return rs; } finally { try { outputStream.close(); if (in != null){ in.close(); } } catch (Exception e) { } outputStream = null; if (connection != null) connection.disconnect(); connection = null; } } public static void main(String[] args) { String url = "http://localhost:???"; HashMap map = new HashMap (); map.put("lineSta", "0121"); map.put("devTypeId", "04"); map.put("deviceId", "034"); map.put("inOutType", "10"); map.put("versionNo", "1016"); map.put("deviceTime", String.valueOf(System.currentTimeMillis())); File file = new File("file/01.zip"); // 文件路径,基于项目,在项目根目录建立file文件夹,01.zip map.put("logFileName", file.getName()); // 上传文件名,与文件保持一致,这里自动获取 map.put("uploadFile",file); // 上传的文件 String returnStr2 = ""; try { returnStr2 = HttpUtil.postFormData(url, map); } catch (Exception e) { System.out.println("上传,发生异常" + e.getMessage()); } System.out.println(returnStr2); }
使用java from-data原始方式上传过于麻烦,需要手动组装数据,过于麻烦,这里帮大家写了一个demo,直接调用即可,启动
可以自动获取,获取FILE的类型即可,具体方式可以百度。我这里用的是zip,于是就写死了是zip
想要看到form-data的原始数据格式,请解除System.out.print 打印注释
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)