Android使用XUtils框架上傳照片(一張或多張)和文本,server接收照片和文字(無亂碼)


Android上傳圖片,這里我使用了如今比較流行的XUtils框架。該框架能夠實現文件上傳、文件下載、圖片緩存等等,有待研究。

以下是Android端上傳的代碼:

xUtils.jar下載

String uploadHost="http://192.168.1.100:8080/ReceiveImgFromAndroid/ReceiveImgServlet";  //server接收地址
RequestParams params=new RequestParams();
params.addBodyParameter("msg","上傳圖片"); 
params.addBodyParameter("img1", new File(filePath));  //filePath是手機獲取的圖片地址
sendImgToServer(params,uploadPath);

這是Xutils框架中上傳文件的方法:

public  void uploadMethod(final RequestParams params,final String uploadHost) {
	http.send(HttpRequest.HttpMethod.POST, uploadHost, params,new RequestCallBack<String>() {
		@Override
		public void onStart() {
			//上傳開始
		}
		@Override
		public void onLoading(long total, long current,boolean isUploading) {
			//上傳中
		}
		@Override
		public void onSuccess(ResponseInfo<String> responseInfo) {
			//上傳成功,這里面的返回值,就是server返回的數據
			//使用 String result = responseInfo.result 獲取返回值
		}
		@Override
		public void onFailure(HttpException error, String msg) {
			//上傳失敗
		}
	});
}

上面寫完了手機端提交照片。接下來要寫一個server端。

server端接收手機端上傳照片的方法與接收jsp界面上傳照片的方法同樣。是用了jspsmartupload_zh.jar包文件。最簡單的方式自己實現一個servlet,在里面調用SmartUpload類接收即可。這個還須要處理好接收文字的亂碼問題。

以下是詳細的代碼:

SmartUpload.jar下載    server測試源代碼下載

package com.example.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

/**
 * 接收圖片
 * 
 * @author Administrator
 * @time 2015年8月10日09:27:17
 */

public class ReceiveImgServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");

		SmartUpload smartUpload = new SmartUpload();
		try {
			smartUpload.initialize(this.getServletConfig(), request, response);
			smartUpload.upload();
			String msg = smartUpload.getRequest().getParameter("msg");
			if (msg != null)
				msg = new String(msg.getBytes("GBK"), "utf-8");
			com.jspsmart.upload.Files files = smartUpload.getFiles();
			for (int i = 0; i < files.getCount(); i++) {
				com.jspsmart.upload.File file = files.getFile(i);
				if (!file.isMissing()) {
					SimpleDateFormat sdf = new SimpleDateFormat(
							"yyyyMMddHHmmssSSS");
					String name = sdf.format(new java.util.Date());
					name = name + "." + file.getFileExt();// 得到文件的擴展名
					String filename = this.getServletContext().getRealPath("/")
							+ "images\\" + name;
					file.saveAs(filename);
				}
			}
		} catch (SmartUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

	}

}



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM