因为后台为了数据安全,他们就集成了七牛的一套api,而我们移动端只需要发送一些简单的http请求,就可以把需要保存的东西上传到了七牛云了,当然,中间需要我们和服务器打交道拿到一些东西,就如下面这一张图片所表达的:
这里面有一个需求是很陌生的,就是移动端需要以表单的形式提交网络请求,什么叫表单的形式,下面的是七牛给的例子:
//需要上传的表单的样式
// <form method="post" action="http://upload.qiniup.com/" enctype="multipart/form-data">
// <input name="key" type="hidden" value="<resource_key>">
// <input name="token" type="hidden" value="<upload_token>">
// <input name="file" type="file" />
// </form>
//表单在http请求中的样子
// POST / HTTP/1.1
// Host: <UpHost>
// Content-Type: multipart/form-data; boundary=<frontier>
// Content-Length: <multipartContentLength>
// Content-Disposition: form-data; name="key"
// Content-Disposition: form-data; name="token"
// Content-Disposition: form-data; name="file"; filename="<fileName>"
// Content-Type: application/octet-stream
// Content-Transfer-Encoding: binary
我解释一下,上面的是表单的样式是什么,下面的是要上传的表单在http协议里面是什么样子的,我们都知道了,http的post请求,数据是在请求体里面的,关于http不熟悉的,可以看一下这篇文章:http://www.cnblogs.com/ranyonsue/p/5984001.html
,看过了文章,那么,我们就要自己在请求体里面拼接表单了,我用postman先输入参数请求成功了,然后点击“code”,看了看大概的成功情况下的请求是什么样,就开始了写如下的方法,封装的很好的,可以直接使用:
public void doFilePost(final String imagePath,String nick,String gender,String sig) throws Exception { this.nick=nick; this.gender=gender; this.sig=sig; new Thread() { @Override public void run() { super.run(); try { String urlstr = uploadUrl; Map<String, String> map = new HashMap(); map.put("key", key); map.put("token", token); Map<String, File> files = new HashMap(); files.put("file", new File(imagePath)); String BOUNDARY = "----WebKitFormBoundaryDwvXSRMl0TBsL6kW"; // 定义数据分隔线 URL url = null; url = new URL(urlstr); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 发送POST请求必须设置如下两行 connection.setDoOutput(true); connection.setDoInput(true); connection.setUseCaches(false); connection.setRequestMethod("POST"); connection.setRequestProperty("Accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Android WYJ"); connection.setRequestProperty("Charsert", "UTF-8"); connection.setRequestProperty("Accept-Encoding", "gzip,deflate"); connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); OutputStream out = new DataOutputStream(connection.getOutputStream()); byte[] end_data = ("--" + BOUNDARY + "--\r\n").getBytes();// 定义最后数据分隔线 // 文件 if (files != null && !files.isEmpty()) { for (Map.Entry<String, File> entry : files.entrySet()) { File file = entry.getValue(); String fileName = entry.getKey(); StringBuilder sb = new StringBuilder(); sb.append("--"); sb.append(BOUNDARY); sb.append("\r\n"); sb.append("Content-Disposition: form-data;name=\"" + fileName + "\";filename=\"" + file.getName() + "\"\r\n"); sb.append("Content-Type: image/jpg\r\n\r\n"); byte[] data = sb.toString().getBytes(); out.write(data); 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); } out.write("\r\n".getBytes()); // 多个文件时,二个文件之间加入这个 in.close(); } } // 数据参数 if (map != null && !map.isEmpty()) { for (Map.Entry<String, String> entry : map.entrySet()) { StringBuilder sb = new StringBuilder(); sb.append("--"); sb.append(BOUNDARY); sb.append("\r\n"); sb.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\""); sb.append("\r\n"); sb.append("\r\n"); sb.append(entry.getValue()); sb.append("\r\n"); byte[] data = sb.toString().getBytes(); out.write(data); } } out.write(end_data); out.flush(); out.close(); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { InputStream inStream = connection.getInputStream(); byte[] number = read(inStream); String json = new String(number); Log.i("zhangjiaren",json); parseQiNiurData(json); } } catch (Exception e) { e.printStackTrace(); } } }.start(); } public byte[] read(InputStream inStream) throws IOException { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } inStream.close(); return outStream.toByteArray(); }先记录下来,等需要的时候再回来拿着用