有关于android上传文件问题


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 参数 主要用于存放上传文件。





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章