SimpleAnalyzer.java
资源名称:J2ME.rar [点击查看]
上传用户:sygbg6
上传日期:2020-12-29
资源大小:4884k
文件大小:5k
源码类别:
游戏
开发平台:
Java
- package cn.com.imageanalyze.analyzer;
- import java.io.IOException;
- import java.io.OutputStream;
- import javax.microedition.io.Connector;
- import javax.microedition.io.HttpConnection;
- import org.kobjects.base64.*;
- import org.ksoap2.SoapEnvelope;
- import org.ksoap2.serialization.SoapObject;
- import org.ksoap2.serialization.SoapPrimitive;
- import org.ksoap2.serialization.SoapSerializationEnvelope;
- import org.ksoap2.transport.HttpTransport;
- import org.xmlpull.v1.XmlPullParserException;
- public class SimpleAnalyzer {
- private static String BOUNDARY = "---------7d4a6d158c9"; // separate line
- private static String MULTIPART_FORM_DATA = "multipart/form-data";
- /**
- * 图片分析
- *
- * @param buff
- * @return 如果分析失败返回 null
- * @throws IOException
- * @throws IOException
- * @throws XmlPullParserException
- */
- public static String analyze(byte[] buff) throws IOException,
- XmlPullParserException {
- HttpTransport ht = null;
- try {
- SoapObject rpc = new SoapObject("http://tempuri.org/", "UploadFile");
- rpc.addProperty("FileName", "jpedtemp.jpeg");
- rpc.addProperty("fs", new SoapPrimitive
- // (SoapSerializationEnvelope.ENC, "base64Binary", Base64.encode(buff).substring(0,10)));
- (SoapSerializationEnvelope.ENC, "base64Binary", Base64.encode(buff)));
- SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
- SoapSerializationEnvelope.VER10);
- envelope.setOutputSoapObject(rpc);
- envelope.dotNet = true;
- ht = new HttpTransport("http://60.30.88.204:83/upload.asmx");
- ht.debug = true;
- ht.setXmlVersionTag("<?xml version="1.0" encoding="UTF-8"?>");
- ht.call("http://tempuri.org/UploadFile", envelope);
- Object object = envelope.getResponse();
- SoapPrimitive sp = (SoapPrimitive) object;
- return sp.toString();
- } catch (XmlPullParserException e) {
- System.out.println(ht.requestDump);
- System.out.println(ht.responseDump);
- throw e;
- }
- }
- // public static String analyze(byte[] buff) throws IOException {
- // String[] props = {}; // property name
- // String[] values = {}; // property value
- //// return writeHttpRequest("http://127.0.0.1:8888",props,values,"pic","jpedtemp","testcontent".getBytes());
- // return writeHttpRequest("http://127.0.0.1:8888",props,values,"pic","jpedtemp",buff);
- // }
- //
- // private static String writeHttpRequest(String url, String[] props,
- // String[] values, String fileFieldName,String filename, byte[] buff) throws IOException {
- // HttpConnection hc = null;
- // OutputStream output = null;
- // try {
- // StringBuffer sb = new StringBuffer();
- // for (int i = 0; i < props.length; i++) { // send each property
- // sb = sb.append("--");
- // sb = sb.append(BOUNDARY);
- // sb = sb.append("rn");
- // sb = sb.append("Content-Disposition: form-data; name=""
- // + props[i] + ""rnrn");
- // // sb = sb.append(URLEncoder.encode(values[i],"UTF-8"));
- // sb = sb.append(values[i]);
- // sb = sb.append("rn");
- // }
- // sb = sb.append("--");
- // sb = sb.append(BOUNDARY);
- // sb = sb.append("rn");
- // sb = sb
- // .append("Content-Disposition: form-data;name="" + fileFieldName + "";filename=""
- // + filename + ".jpg"rn");
- // sb = sb.append("Content-Type: application/octet-streamrnrn");
- // byte[] data = sb.toString().getBytes();
- // byte[] end_data = ("rn--" + BOUNDARY + "--rn").getBytes();
- // hc = (HttpConnection) Connector.open(url, Connector.READ_WRITE); // set
- // // HTTP
- // // head:
- // hc.setRequestProperty("Accept-Encoding", "gzip, deflate");
- // hc.setRequestProperty("Content-Type", MULTIPART_FORM_DATA
- // + "; boundary=" + BOUNDARY);
- // hc.setRequestProperty("Content-Length", String.valueOf(data.length
- // + buff.length + end_data.length)); //
- // hc.setRequestProperty("Connection", "keep-alive");
- // hc.setRequestMethod(HttpConnection.POST); // out: OutputStream //
- // output = hc.openOutputStream();
- // output.write(data);
- // int i = 0;
- // while(i + 500 < buff.length){
- // output.write(buff, i, i + 500);
- // }
- // i = buff.length % 500;
- // if(i != 0) output.write(buff,buff.length - i ,buff.length);
- // output.write(end_data);
- // int cah = hc.getResponseCode();
- // if (cah == HttpConnection.HTTP_OK) {
- // return hc.getResponseMessage();
- // } else throw new IOException();
- // } finally {
- // if (output != null)
- // output.close();
- // if (hc != null)
- // try {
- // hc.close();
- // } catch (IOException e) {
- // // TODO Auto-generated catch block
- // e.printStackTrace();
- // }
- // }
- // }
- }