有关于android上传文件问题
来源:互联网 发布:php求数组中的最大值 编辑:程序博客网 时间:2024/05/01 21:37
O(∩_∩)O 要坚持把遇到的所有问题都总结到博客中
android上传关键是在于将所有的信息都通过form表单的形式传递,首先一定要确定同服务器端的传递接口参数(坑爹啊。。。不知道参数调试了半天),模拟http头信息,将所有的信息可以通过各种方式(HttpURLConnection, socket, ?httpclient?)传递到服务端, 这其中参数我忽略了数据长度的参数,结果下场很惨。。。。。。
话不多说 上代码~~~
public static String post(Task task, Map<String, File> files) throws IOException {final String BOUNDARY = "---------------------------"+ java.util.UUID.randomUUID().toString(); // 数据分隔线final String endline = "--" + BOUNDARY + "--\r\n";// 数据结束标志int fileDataLength = 0;for (Map.Entry<String, File> entry : files.entrySet()) {// 得到文件类型数据的总长度StringBuilder fileExplain = new StringBuilder();File file = entry.getValue();fileExplain.append("--");fileExplain.append(BOUNDARY);fileExplain.append("\r\n");fileExplain.append("Content-Disposition: form-data;name=\""+ entry.getKey() + "\";filename=\"" + file.getName()+ "\"\r\n");fileExplain.append("Content-Type: " + "application/octet-stream"+ "\r\n\r\n");fileExplain.append("\r\n");fileDataLength += fileExplain.length();if (file != null) {fileDataLength += file.length();}}StringBuilder textEntity = new StringBuilder();String data = getJson(getParams(task.params));textEntity.append("--");textEntity.append(BOUNDARY);textEntity.append("\r\n");textEntity.append("Content-Disposition: form-data; name=\"" + "data"+ "\"\r\n\r\n");textEntity.append(data);textEntity.append("\r\n");int dataLength = textEntity.toString().getBytes().length+ fileDataLength + endline.getBytes().length;if (task.url != null) {URL url = new URL(task.url);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setConnectTimeout(5 * 1000);conn.setDoInput(true);// 允许输入conn.setDoOutput(true);// 允许输出conn.setUseCaches(false); // 不允许使用缓存conn.setRequestMethod("POST");conn.setRequestProperty("connection", "keep-alive");conn.setRequestProperty("Charsert", "UTF-8");conn.setRequestProperty("Content-Type", "multipart/form-data"+ ";boundary=" + BOUNDARY);conn.setRequestProperty("Content-Length",String.valueOf(dataLength));DataOutputStream outputStream = new DataOutputStream(conn.getOutputStream());outputStream.writeBytes(textEntity.toString());for (Map.Entry<String, File> entry : files.entrySet()) {StringBuilder fileEntity = new StringBuilder();File file = entry.getValue();if (file != null) {fileEntity.append("--");fileEntity.append(BOUNDARY);fileEntity.append("\r\n");fileEntity.append("Content-Disposition: form-data;name=\""+ entry.getKey() + "\";filename=\""+ entry.getValue().getName() + "\"\r\n");fileEntity.append("Content-Type: "+ "application/octet-stream" + "\r\n\r\n");outputStream.writeBytes(fileEntity.toString());FileInputStream fis = new FileInputStream(file);byte[] buffer = new byte[1024];int len = 0;while ((len = fis.read(buffer)) != -1) {outputStream.write(buffer, 0, len);outputStream.flush();}outputStream.write("\r\n".getBytes());fis.close();}}outputStream.writeBytes(endline);BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));if (conn.getResponseCode() != 200) {// 读取web服务器返回的数据,判断请求码是否为200,如果不是200,代表请求失败return "error";}String result = "";String line = null;while ((line = reader.readLine()) != null) {result = result + line;}outputStream.flush();outputStream.close();reader.close();return result;}else{return "error";}}
Task 中封装了 一些关于上传任务的信息,Map 参数 主要用于存放上传文件。
- 有关于android上传文件问题
- 有关于asp问题?
- 有关 文件上传后乱码问题
- 文件上传有关问题(含代码)
- 有关于sln 类型文件的 unrecognized version 问题
- 一个有关于android设置的小问题
- 有关于android模拟器的问题(不定期更新)
- 在Android学习中有关于广播注册的问题
- 有关于oracle的问题!
- 有关于预处理的问题
- 于Excel文件上传读取数据的问题
- 一些有关browser-server的文件上传问题
- 有关springmvc文件上传,出现400 的错误问题
- 有关asp.net获取android端上传的图片问题
- java--文件上传于下载
- 有关于oracle参数文件的说明
- 有关于虚函数的问题
- 有关于java中的一些容器问题
- 异步套接字编程之select模型
- 数字图像处理和图像识别中常用的OpenCV函数
- Wireshark支持OpenFlow
- MySQL 生成随机密码
- Natural Language Processing On Coursera
- 有关于android上传文件问题
- php tutorial include and requre
- Auto CAD的相关问题·绘图单位
- data service
- php tutorial Constant,Variables and data types
- 两个select多选控件中项的移动的相关操作
- Android中的Intent详细讲解
- php tutorial operator
- php tutorial statement