测试http接口的类的实现

package com.goldgrid.utils;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class CeshiImg {
	public static void main(String[] args) throws IOException {
		String s = null; 
		FileReader f = null;
		BufferedReader buf = null;
		try{
			f = new FileReader("D:\\ZTestForWork\\createNoSignPdf.xml");
			buf = new BufferedReader(f);
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			int i = -1;
			while((i = buf.read()) != -1) 
			out.write(i);
			s = out.toString();
		}catch(IOException e){
			System.out.println(e);
		}finally{
			f.close(); 
			buf.close();
		}
		System.out.println("返回的数据为" + sendPostMessage(s));
	}
	
	private static String sendPostMessage(String str) { 
	    String strs = "";
		HttpURLConnection conn = null;
		try{
			URL objUrl = new URL("http://localhost:8080/iSolutions-BDE/ServletForTest");
			conn = (HttpURLConnection) objUrl.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Accept-Charset", "utf-8"); 
            conn.setRequestProperty("Content-Type","text/html");
            conn.setRequestProperty("Content-Length", String.valueOf(str.length()));
            DataOutputStream out2 = new DataOutputStream(conn.getOutputStream());
            out2.write(str.getBytes());
            System.out.println("开始POST请求");
            out2.flush();
            System.out.println("结束POST请求");
            out2.close();
            if(conn.getResponseCode() != 200){
            	return strs;
            }else{
             // 读取返回数据    
                StringBuffer strBuf = new StringBuffer();
                BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String line = null;
                while ((line = reader.readLine()) != null) {
                    strBuf.append(line).append("\n");
                }  
                strs = strBuf.toString();  
                reader.close();  
                //reader = null;  
                return strs;
            }
		}catch(Exception ex){
			ex.printStackTrace();
			return strs;
		}finally {
			if(conn != null){
				conn.disconnect();
				conn = null;
			}
		}
    } 
}
package com.goldgrid.post;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.goldgrid.common.DateUtils;
import com.goldgrid.common.MD5Util;
import com.goldgrid.post.bean.UploadImg;
import com.goldgrid.post.xml.BeanToXml;
import com.goldgrid.webservices.importpdf.Base64;

public class TestUploadImg {
	public static void main(String[] args) throws Exception {
		UploadImg uploadImg = new UploadImg();
		Base64 base64 = new Base64();
		//1.授权码
		String uploadDate = DateUtils.getCurrentTime("yyyyMMddHHmmss");
		String authCode = "tcxy" + uploadDate;
		uploadImg.setAuthCode(new String(base64.encode(MD5Util.stringToMD5(authCode, "utf-8").getBytes())));
		//2.工号
		uploadImg.setOptCode(new String(base64.encode("tcxy".getBytes())));
		uploadImg.setExt1("");
		//3.图片
		String imgstr1 = new String(new String(base64.encode(readBytes(new File("d:/image/img03.jpg")))).getBytes(), "utf-8");
		String imgstr2 = new String(new String(base64.encode(readBytes(new File("d:/image/img02.jpg")))).getBytes(), "utf-8");

		//String imgstr11 = new String(new String(base64.encode(readBytes(new File("d:/image/img11.jpg")))).getBytes(), "utf-8");		
//		uploadImg.setPhotos(imgstr1 + ";" + imgstr2 + ";" + imgstr3 + ";" + imgstr4 + ";" + 
//				imgstr5 + ";" + imgstr6 + ";" + imgstr7 + ";" + imgstr8 + ";" + imgstr9 + ";" + imgstr10 + ";" + imgstr11);
//		uploadImg.setPhotos(imgstr1 + ";" + imgstr2);
//		uploadImg.setPhotos(imgstr1);
		uploadImg.setExt2(imgstr1);
		System.out.println(uploadImg.getPhotos());
		uploadImg.setDeviceCode("abcdeeeee");
		uploadImg.setTel("13647093642");
		uploadImg.setTime(uploadDate);
		uploadImg.setIp("192.168.0.80");
		uploadImg.setMac("aaaaaaaaaaaaaa");
		
		//新增参数,地区编码areaCode,操作类型OptCode
		uploadImg.setAreaCode("563");
		uploadImg.setOptType("2");	//操作类型:0,上传后直接保存;1,上传后若订单无附件则保存,有附件不保存;2,上传后补单
		String businessId = DateUtils.getCurrentTime("yyyyMMddHHmmssSSS");
		uploadImg.setBusinessId(new String(base64.encode(" 20170418152159991".getBytes())));
//		uploadImg.setBusinessId("2014072512051533");
		
		//4.将java对象转换成xml
		BeanToXml beanToXml = new BeanToXml();
		String importXml = beanToXml.createImportXML(uploadImg);
		System.out.println(importXml);
		//5.POST入参
		String importXmlPath = "d:\\image\\UploadXML.xml";
		writeBytes(new File(importXmlPath), importXml.getBytes("UTF-8"));
		TestUploadImg testUploadImg = new TestUploadImg();
		String returnXml = testUploadImg.uploadImgFile(importXmlPath,
				"http://192.168.0.80:8080/iSolutions-BDE/uploadIMG?method=upload");
		System.out.println(returnXml);
	}
	private synchronized  String uploadImgFile(String importXmlPath, String strURL) {
		// TODO Auto-generated method stub
		String result = null;

		try {
			URL url = new URL(strURL);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setUseCaches(false);
			conn.setRequestProperty("enctype", "multipart/form-data");
			conn.setRequestProperty("contentType", "charset=UTF-8");
			conn.setRequestMethod("POST");

			// 上传xml文件
			DataInputStream file = new DataInputStream(new FileInputStream(
					new File(importXmlPath)));
			OutputStream out = conn.getOutputStream();
			int bytesOut = 0;
			byte[] bufferOut = new byte[8192];
			while ((bytesOut = file.read(bufferOut, 0, 8192)) != -1) {
				out.write(bufferOut, 0, bytesOut);//
			}
			out.flush();
			out.close();

			// 获得上传的结果
			InputStream isResult = conn.getInputStream();

			result = inputStream2String(isResult);

		} catch (Exception e) {
			result = "ERROR";
			System.out.println(e.getMessage());
		}
		return result;
	}
	public static byte[] readBytes(File file) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            InputStream fis = new FileInputStream(file);
            InputStream is = new BufferedInputStream(fis);
            int count = 0;
            byte[] buf = new byte[16384];
            while ( (count = is.read(buf)) != -1) {
                if (count > 0) {
                    baos.write(buf, 0, count);
                }
            }
            is.close();
        }
        catch (Exception e) { 
        }
        return baos.toByteArray();
    }
	
	public static void writeBytes(File file, byte[] data) {
		try {
			OutputStream fos = new FileOutputStream(file);
			OutputStream os = new BufferedOutputStream(fos);
			os.write(data);
			os.close();
		} catch (Exception e) {
		}
	}
	
	public String inputStream2String(InputStream is) throws IOException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		int i = -1;
		while ((i = is.read()) != -1) {
			baos.write(i);
		}
		return baos.toString();
	}
}

 

发布了22 篇原创文章 · 获赞 13 · 访问量 4万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览