package com.jqyd.app; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.SocketTimeoutException; import java.net.URL; import java.nio.charset.Charset; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import org.apache.commons.httpclient.params.HttpMethodParams; import org.apache.http.HttpResponse; import org.apache.http.HttpVersion; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.FileEntity; import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.entity.mime.content.StringBody; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.CoreConnectionPNames; import org.apache.http.params.CoreProtocolPNames; import com.jqyd.pub.CustomMultipartEntity; import com.jqyd.pub.CustomMultipartEntity.ProgressListener; import com.jqyd.pub.MyProgressDialog; import com.jqyd.shareInterface.ConnectStateService; import com.jqyd.shareInterface.Optdb_interfce; import com.jqyd.shareInterface.Optsharepre_interface; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; public class UpMyFile { private Map<String, String> params = new HashMap<String, String>(); private Map<String, File> files = new HashMap<String,File>();; // private String actionUrl = "http://10.29.0.60:8080/jqgj_2.0_serv/"; private String actionUrl = "http://www.jqgj.com.cn:9090/jqgj_server_client/"; private String picPath; private Context context; private Optsharepre_interface share; public UpMyFile(Context context){ this.context = context; share = new Optsharepre_interface(context); } public String upToServer(Bundle bundle,String pic_sort){ //返回值0、成功1、网络原因2、调用相机并上传图片出现问题3、上传失败,服务器端问题 //System.out.println("bundle ****"+bundle.get("xxnr")); picPath = bundle.getString("imagePath"); String arrs[] = null; //异常处理,部分机型可能存在无法读取存储卡中照片路径 try { arrs = picPath.split("/"); } catch (Exception e) { e.printStackTrace(); return "2"; } String fileName = arrs[arrs.length - 1]; files.put(fileName, new File(picPath)); if(pic_sort.equals("xxfk")){ System.out.println("bundle.getString "+bundle.getString("gguid")); params.put("gguid", bundle.getString("gguid")); params.put("zguid", bundle.getString("zguid")); params.put("cosim", bundle.getString("cosim")); params.put("regsim", bundle.getString("regsim")); params.put("xxbt", bundle.getString("xxbt")); params.put("xxnr", bundle.getString("xxnr")); params.put("xxcjlb", bundle.getString("xxcjlb")); params.put("tpxx", bundle.getString("pic_detail")); params.put("time", bundle.getString("time")); params.put("length", String.valueOf(new File(picPath).length())); params.put("guid", bundle.getString("guid")); params.put("zdmc", bundle.getString("zdmc")); params.put("cell_id", bundle.getString("cell_id")); params.put("lac_code", bundle.getString("lac_code")); params.put("country_code", bundle.getString("country_code")); params.put("ncode", bundle.getString("ncode")); params.put("signal_strength", bundle.getString("signal_strength")); params.put("yys", bundle.getString("yys")); params.put("lon", bundle.getString("lon")); params.put("lat", bundle.getString("lat")); params.put("radius", bundle.getString("radius")); params.put("loc_method", bundle.getString("loc_method")); params.put("relationid", bundle.getString("relationid")); actionUrl += "xxfk_position.action"; }else if(pic_sort.equals("khcj")){ params.put("gguid", bundle.getString("gguid")); params.put("guid", bundle.getString("guid")); params.put("cosim", bundle.getString("cosim")); params.put("regsim", bundle.getString("regsim")); params.put("cname", bundle.getString("cname")); params.put("sjhm", bundle.getString("sjhm")); params.put("linkman", bundle.getString("linkman")); params.put("link_sim", bundle.getString("link_sim")); params.put("address", bundle.getString("address")); params.put("cid", bundle.getString("cid")); params.put("time", bundle.getString("time")); params.put("lon", bundle.getString("lon")); params.put("lat", bundle.getString("lat")); params.put("cell_id", bundle.getString("cell_id")); params.put("lac_code", bundle.getString("lac_code")); params.put("country_code", bundle.getString("country_code")); params.put("ncode", bundle.getString("ncode")); params.put("signal_strength", bundle.getString("signal_strength")); params.put("radius", bundle.getString("radius")); params.put("bz", bundle.getString("bz")); params.put("kh_belong", bundle.getString("kh_belong")); params.put("tpxx", bundle.getString("pic_detail")); params.put("yys", bundle.getString("yys")); actionUrl += "khcj.action"; }else if(pic_sort.equals("msbf")){ params.put("gguid", bundle.getString("gguid")); params.put("guid", bundle.getString("guid")); params.put("cosim", bundle.getString("cosim")); params.put("regsim", bundle.getString("regsim")); params.put("cname", bundle.getString("cname")); params.put("sjhm", bundle.getString("sjhm")); params.put("linkman", bundle.getString("linkman")); params.put("link_sim", bundle.getString("link_sim")); params.put("address", bundle.getString("address")); params.put("cid", bundle.getString("cid")); params.put("time", bundle.getString("time")); params.put("lon", bundle.getString("lon")); params.put("lat", bundle.getString("lat")); params.put("cell_id", bundle.getString("cell_id")); params.put("lac_code", bundle.getString("lac_code")); params.put("country_code", bundle.getString("country_code")); params.put("ncode", bundle.getString("ncode")); params.put("signal_strength", bundle.getString("signal_strength")); params.put("radius", bundle.getString("radius")); params.put("content", bundle.getString("content")); params.put("kh_belong", bundle.getString("kh_belong")); params.put("tpxx", bundle.getString("pic_detail")); params.put("jhid", bundle.getString("jhid")); params.put("yys", bundle.getString("yys")); actionUrl += "khcj.action"; }else if(pic_sort.equals("khbf")){ params.put("gguid", bundle.getString("gguid")); params.put("guid", bundle.getString("guid")); params.put("cosim", bundle.getString("cosim")); params.put("regsim", bundle.getString("regsim")); params.put("cid", bundle.getString("cid")); params.put("time", bundle.getString("time")); params.put("lon", bundle.getString("lon")); params.put("lat", bundle.getString("lat")); params.put("cell_id", bundle.getString("cell_id")); params.put("lac_code", bundle.getString("lac_code")); params.put("country_code", bundle.getString("country_code")); params.put("ncode", bundle.getString("ncode")); params.put("signal_strength", bundle.getString("signal_strength")); params.put("loc_method", bundle.getString("loc_method")); params.put("radius", bundle.getString("radius")); params.put("isfirst", bundle.getString("isfirst")); params.put("type", bundle.getString("type")); params.put("content", bundle.getString("content")); params.put("tpxx", bundle.getString("pic_detail")); params.put("zdmc", bundle.getString("zdmc")); params.put("cname", bundle.getString("cname")); params.put("jhid", bundle.getString("jhid")); params.put("yys", bundle.getString("yys")); actionUrl += "khbf.action"; //********************************************** System.out.println("客户拜访"+actionUrl); }else if(pic_sort.equals("scjc")){ params.put("gguid", bundle.getString("gguid")); params.put("guid", bundle.getString("guid")); params.put("cosim", bundle.getString("cosim")); params.put("regsim", bundle.getString("regsim")); params.put("cid", bundle.getString("cid")); params.put("time", bundle.getString("time")); params.put("lon", bundle.getString("lon")); params.put("lat", bundle.getString("lat")); params.put("cell_id", bundle.getString("cell_id")); params.put("lac_code", bundle.getString("lac_code")); params.put("country_code", bundle.getString("country_code")); params.put("ncode", bundle.getString("ncode")); params.put("signal_strength", bundle.getString("signal_strength")); params.put("loc_method", bundle.getString("loc_method")); params.put("radius", bundle.getString("radius")); params.put("content", bundle.getString("content")); params.put("zhpf", bundle.getString("zhpf")); params.put("tpxx", bundle.getString("pic_detail")); params.put("zdmc", bundle.getString("zdmc")); params.put("cname", bundle.getString("cname")); params.put("jclb", bundle.getString("jclb")); params.put("jhid", bundle.getString("jhid")); params.put("yys", bundle.getString("yys")); params.put("imagecode", bundle.getString("imagecode")); actionUrl += "scjc.action"; }else if(pic_sort.equals("rzsb")){ params.put("gguid", bundle.getString("gguid")); params.put("zguid", bundle.getString("zguid")); params.put("cosim", bundle.getString("cosim")); params.put("regsim", bundle.getString("regsim")); params.put("gznr", bundle.getString("gznr")); params.put("ywrq", bundle.getString("ywrq")); params.put("gzsc", bundle.getString("gzsc")); params.put("gzyj", bundle.getString("gzyj")); params.put("gzcg", bundle.getString("gzcg")); params.put("imagecode", bundle.getString("imagecode")); params.put("pic_detail", bundle.getString("pic_detail")); params.put("souceid", bundle.getString("souceid")); actionUrl += "rzsb.action"; }else if(pic_sort.equals("qdxq")){ // String head = "Content-Length="+ uploadFile.length() + ";filename="+ // uploadFile.getName() + ";sourceid="+ // (souceid==null? "" : souceid)+";6"+";"+cosim+";"+type+";" // +share_obj.getDataFromPres("GUID")+";"+relationid+";"+"\r\n"; Optsharepre_interface share_obj = new Optsharepre_interface(context); String cosim = share_obj.getDataFromPres("COSIM"); cosim = cosim+".jpg"; Log.e("cosim", cosim); Log.e("filename", bundle.getString("filename")); Log.e("type", bundle.getString("type")); params.put("cosim", cosim); params.put("filename",bundle.getString("filename")); params.put("type",bundle.getString("type")); params.put("guid",share_obj.getDataFromPres("GUID")); Log.e("guid", share_obj.getDataFromPres("GUID")); Log.e("relationid", share_obj.getDataFromPres("relationid")); params.put("relationid",bundle.getString("relationid")); actionUrl += "qdqt.action"; }else if(pic_sort.equals("errorLogs")){ params.put("guid", bundle.getString("guid")); params.put("regsim", bundle.getString("regsim")); params.put("xxbt", bundle.getString("xxbt")); params.put("xxnr", ""); params.put("rzlb", ""); params.put("rzxx", ""); // params.put("time", new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())); actionUrl += "errorlog.action"; } //执行文件流上传 String result = uploadFile2(); //上传成功删除文件 return result; } public String uploadFile2(){ String retMsg ="1"; CustomMultipartEntity mpEntity = new CustomMultipartEntity( new ProgressListener() { @Override public void transferred(long num) { Intent intent2 = new Intent(); intent2.putExtra("length",(int) num); intent2.putExtra("max", Integer.parseInt(share.getDataFromPres("fileMax"))); intent2.setAction("upfile"); context.sendBroadcast(intent2); } }); if (params != null && !params.isEmpty()) { for (Map.Entry<String, String> entry : params.entrySet()) { StringBody par = null; try { if(entry.getValue() == null){ continue; } par = new StringBody(java.net.URLEncoder.encode(entry.getValue(),"UTF-8")); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } mpEntity.addPart(entry.getKey(), par); } } FileBody file = new FileBody(new File(picPath)); mpEntity.addPart("image", file); long max = mpEntity.getContentLength(); share.editPres("fileMax", max+""); Intent intent = new Intent(); intent.putExtra("max",(int) max); intent.setAction("upfile"); context.sendBroadcast(intent); HttpPost post = new HttpPost(actionUrl); // 发送请求体 post.setEntity(mpEntity); DefaultHttpClient dhc = new DefaultHttpClient(); try { dhc.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000); HttpResponse response = dhc.execute(post); int res = response.getStatusLine().getStatusCode(); Log.e("图片上传返回相应码", res+","); switch(res){ case 200: retMsg = "0"; break; case 404: retMsg = "1"; break; default: retMsg = "3"; } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return retMsg ; } public String uploadFile(){ String retMsg ="1"; String arrs[] = picPath.split("/"); String fileName = arrs[arrs.length - 1]; //System.out.println("图片路径:"+picPath); File file=new File(picPath); Intent intent = new Intent(); intent.putExtra("max",(int) file.length()); intent.setAction("upfile"); context.sendBroadcast(intent); String BOUNDARY = java.util.UUID.randomUUID().toString(); String PREFIX = "--", LINEND = "\r\n"; String MULTIPART_FROM_DATA = "multipart/form-data"; String CHARSET = "UTF-8"; try { URL url = new URL(actionUrl); HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); urlConn.setRequestMethod("POST"); urlConn.setDoInput(true);// 允许输入 urlConn.setDoOutput(true);// 允许输出 urlConn.setConnectTimeout(10000); //设置超时 urlConn.setReadTimeout(10000); urlConn.setUseCaches(false); //设置http协议头 urlConn.setRequestProperty("connection", "keep-alive"); urlConn.setRequestProperty("Charset", "UTF-8"); urlConn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA + ";boundary=" + BOUNDARY); // urlConn.setRequestProperty("Accept-Language", "zh-cn"); // urlConn.setRequestProperty("Accept-Charset", "GB18030"); //System.out.println("拍照数据--------------"+params.toString()); // 首先组拼文本类型的参数 StringBuilder sb = 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()); //System.out.println("拍照数据--------------"+entry.getValue()); if(entry.getValue() != null){ sb.append(java.net.URLEncoder.encode(entry.getValue(),"UTF-8")); } sb.append(LINEND); } urlConn.connect(); System.out.println("-------------------------------"); System.out.println(sb.toString()); System.out.println("-------------------------------"); DataOutputStream outStream = new DataOutputStream(urlConn.getOutputStream()); outStream.write(sb.toString().getBytes()); // 发送文件数据 if (file != null){ StringBuilder sb1 = new StringBuilder(); sb1.append(PREFIX); sb1.append(BOUNDARY); sb1.append(LINEND); //sb1.append("Content-Length:"+file.length()); sb1.append("Content-Disposition: form-data; name=\"image\"; filename=\""+ picPath + "\"" + LINEND); sb1.append("Content-Type: image/jpeg" + LINEND); sb1.append(LINEND); System.out.println("********------------------********"); System.out.println(sb1.toString()); System.out.println("********------------------********"); outStream.write(sb1.toString().getBytes()); InputStream is = new FileInputStream(file); byte[] buffer = new byte[1024]; int len = 0; int length=0; while ((len = is.read(buffer)) != -1) { outStream.write(buffer, 0, len); Intent intent2 = new Intent(); length+=len; intent2.putExtra("length", length); intent2.setAction("upfile"); context.sendBroadcast(intent2); } is.close(); outStream.write(LINEND.getBytes()); } // 请求结束标志 byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes(); outStream.write(end_data); outStream.flush(); // 得到响应码 int res = urlConn.getResponseCode(); System.out.println("---------------------------图片上传返回相应码-------------------------:"+res); InputStream in = urlConn.getInputStream(); InputStreamReader isReader = new InputStreamReader(in); BufferedReader bufReader = new BufferedReader(isReader,8192); String line = null; String data = "OK"; while((line = bufReader.readLine())==null) data += line; switch(res){ case 200: int ch; StringBuilder sb2 = new StringBuilder(); while ((ch = in.read()) != -1) { sb2.append((char) ch); } Log.e("sb2", sb2.toString()); retMsg = "0"; break; case 404: retMsg = "1"; break; default: retMsg = "3"; } outStream.close(); urlConn.disconnect(); //retMsg = "OK"; } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch(SocketTimeoutException e){ }catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Log.e("返回结果", retMsg); //返回结果 return retMsg; } }