上传图片携带参数至服务器工具类

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Ae_fring/article/details/51993705


本篇采用的上传方式是HttpUrlConnection方法,下篇将写出xutils上传的方法(包括多图片上传显示进度条等)。敬请期待。

//UpLoadFileVideoUtils这个类就是工具类,只需要将这个类拷贝至项目中即可使用。下面是代码部分:

解释下方法里面的参数含义:

Map<String, String> params---用来装你上传服务器需要的参数,注意的是:参数的Key值必须填入的是你服务器定好的参数字段,

Map<String, File> files--将你选择好的图片转成File文件用Map装起来 图片的Key值可以自己命名。

/**
 * @author: ZQF_DemoStyle
 * @类 说 明:
 * @version 1.0
 * @创建时间:2016-4-18 下午1:18:24
 */
public class UpLoadFileVideoUtils {

	private StringBuilder sb2;

	public boolean defaultUploadMethod(String actionUrl,
			Map<String, String> params, Map<String, File> files)
			throws IOException {
		String PREFIX = "--";
		String BOUNDARY = java.util.UUID.randomUUID().toString();
		String LINEND = "\r\n";
		String MULTIPART_FROM_DATA = "multipart/form-data";
		String CHARSET = "UTF-8";
		URL uri = new URL(actionUrl);
		HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
		conn.setReadTimeout(5 * 10000); // 缓存的最长时间
		// conn.setFixedLengthStreamingMode(contentLength);
		conn.setAllowUserInteraction(false);
		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_FROM_DATA
				+ ";boundary=" + BOUNDARY);
		// 首先组拼文本类型的参数
		StringBuilder sub = new StringBuilder();
		for (Map.Entry<String, String> entry : params.entrySet()) {
			sb.append(PREFIX);
			sb.append(BOUNDARY);
			sb.append(LINEND);
			sb.append("Content-Disposition: form-data; name=\""
					+ entry.getKey() + "\"" + LINEND);
			sb.append("Content-Type: text/plain; charset=" + CHARSET + LINEND);
			sb.append("Content-Transfer-Encoding: 8bit" + LINEND);
			sb.append(LINEND);
			sb.append(entry.getValue());
			sb.append(LINEND);
		}

		DataOutputStream outStream = new DataOutputStream(
				conn.getOutputStream());
		outStream.write(sb.toString().getBytes());
		InputStream in = null;
		// 发送流文件数据
		if (files != null) {
			for (Map.Entry<String, File> file : files.entrySet()) {
				StringBuilder sb1 = new StringBuilder();
				sb1.append(PREFIX);
				sb1.append(BOUNDARY);
				sb1.append(LINEND);
				sb1.append("Content-Disposition: form-data; name=\"file\"; filename=\""
						+ file.getKey() + ".jpg" + "\"" + LINEND);
				sb1.append("Content-Type: video/mpeg4; charset=" + CHARSET
						+ LINEND);
				sb1.append(LINEND);
				outStream.write(sb1.toString().getBytes());
				InputStream is = new FileInputStream(file.getValue());
				byte[] buffer = new byte[1024 * 8];
				int len = 0;
				while ((len = is.read(buffer)) != -1) {
					outStream.write(buffer, 0, len);
				}
				is.close();
				outStream.write(LINEND.getBytes());
			}

			// 请求结束标志
			byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();
			outStream.write(end_data);
			outStream.flush();
			// 得到响应码
			int res = conn.getResponseCode();
			if (res == 200) {
				in = conn.getInputStream();
				int ch;
			if (res == 200) {
<span style="white-space:pre">				</span>in = conn.getInputStream();
<span style="white-space:pre">				</span>int ch;
<span style="white-space:pre">				</span>su2 = new StringBuilder();
<span style="white-space:pre">				</span>while ((ch = in.read()) != -1) {
<span style="white-space:pre">					</span>su2.append((char) ch);
<span style="white-space:pre">				</span>}
			}
			outStream.close();
			conn.disconnect();
			in.close();
			LogUtil.logD("返回的数据---->" + su2 + "");
			return true;
		}
		return false;
	}
最后根据返回的boolean值判断上传成功与否,成功返回的数据在sb2中

猜你喜欢

转载自blog.csdn.net/Ae_fring/article/details/51993705