1. 程式人生 > >上傳圖片攜帶引數至伺服器工具類

上傳圖片攜帶引數至伺服器工具類


本篇採用的上傳方式是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中