1.java.net.MalformedURLException
一般是域名不正确或者残缺不全!
2.服务器400问题
这个错误困扰了我两天,直到今天才解决,在老师的指导下完成,我还是有点嫩了,别的不说了,先上代码!
这段代码的作用就是混合信息上传到服务器,什么叫混合信息上传?就是文字信息和文件的上传,我觉得在做这个问题之前,需要先看一下HTTP协议!
这样我就直接贴上代码以及如何使用?
/** * 上传文字图片 * @param urlStr * @param textMap * @param fileMap * @return */ public static String formUpload(String urlStr, Map<String, String> textMap, Map<String, String> fileMap) { Log.d("TAG","::fileMap"+fileMap.toString()); String res = ""; HttpURLConnection conn = null; String BOUNDARY = "---------------------------123821742118716"; //boundary就是request头和上传文件内容的分隔符 try { URL url = new URL(urlStr); conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); conn.setReadTimeout(30000); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)"); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); OutputStream out = new DataOutputStream(conn.getOutputStream()); // text if (textMap != null) { StringBuffer strBuf = new StringBuffer(); Iterator<Map.Entry<String, String>> iter = textMap.entrySet().iterator(); while (iter.hasNext()) { Map.Entry<String, String> entry = iter.next(); String inputName = (String) entry.getKey(); String inputValue = (String) entry.getValue(); if (inputValue == null) { continue; } strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n"); strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"\r\n\r\n"); strBuf.append(inputValue); } out.write(strBuf.toString().getBytes()); Log.d("TAG","::"+strBuf.toString()); } // file if (fileMap != null) { Iterator<Map.Entry<String, String>> iter = fileMap.entrySet().iterator(); while (iter.hasNext()) { Map.Entry<String, String> entry = iter.next(); Log.d("TAG","ddd"+entry.toString()); String inputName = (String) entry.getKey(); String inputValue = (String) entry.getValue(); Log.d("TAG","name"+inputName+","+inputValue); if (inputValue == null) { continue; } File file = new File(inputValue); String filename = file.getName(); Log.d("TAG","3"+inputName+","+inputValue); StringBuffer strBuf = new StringBuffer(); strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n"); strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"; filename=\"" + filename + "\"\r\n"); Log.d("TAG", "3.5" + inputName + "," + inputValue); strBuf.append("Content-Type:" + "image/jpg" + "\r\n\r\n"); Log.d("TAG", "::123" + strBuf.toString()); Log.d("TAG","4"+inputName+","+inputValue); out.write(strBuf.toString().getBytes()); // Log.d("TAG","::123"+strBuf.toString()); DataInputStream in = new DataInputStream(new FileInputStream(file)); int bytes = 0; byte[] bufferOut = new byte[1024]; while ((bytes = in.read(bufferOut)) != -1) { out.write(bufferOut, 0, bytes); } in.close(); Log.d("TAG", "5" + inputName + "," + inputValue); } } byte[] endData = ("\r\n--" + BOUNDARY + "--\r\n").getBytes(); out.write(endData); out.flush(); out.close(); Log.d("TAG",conn.getResponseCode()+"::"+conn.getResponseMessage()); // 读取返回数据 StringBuffer strBuf = new StringBuffer(); BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line = null; while ((line = reader.readLine()) != null) { strBuf.append(line).append("\n"); } res = strBuf.toString(); reader.close(); reader = null; } catch (Exception e) { System.out.println("发送POST请求出错。" + urlStr); e.printStackTrace(); } finally { if (conn != null) { conn.disconnect(); conn = null; } } return res; }
这个就是工具方法了,下面是如何使用了:
new Thread(new Runnable() { @Override public void run() { String urlStr = "http://192.168.1.117:8080/bzxt/serv/send_task_allocation"; Map<String,String> mapText = new HashMap<String, String>(); //mapText.put("userId",""); //mapText.put("key",""); mapText.put("a","12"); mapText.put("b","1"); mapText.put("c","挂撤地线"); Map<String,String> mapFile = new HashMap<String, String>(); mapFile.put("d", "/storage/emulated/0/abc_2015-05-03_20-58-16.jpg"); String content = formUpload(urlStr,mapText,mapFile); Log.d("TAG","content="+content); } }).start();仅仅作为示例,如果需要应用到实际中,还请根据自己的情况自行判断使用.
结论:
在编写Http请求的时候,尽量不要去自己强制构造,否则很容易出问题,还有就是一定要好好学习下Http协议!