图片二进制流显示的图片,图片上面汉字标注会乱码

wy_chen 发布于 2013/04/17 10:13
阅读 1K+
收藏 0

图片二进制流显示的图片,图片上面汉字标注会出现乱码

正常的显示如下:

输出语句:

ServletOutputStream bos = response.getOutputStream();
	String servicePath = request.getParameter("url");
	String appkey = request.getParameter("appkey");
	String appid = request.getParameter("appid");

	byte[] respMsgByte = null;

	servicePath = new String(servicePath.getBytes("ISO-8859-1"), "UTF-8");
	respMsgByte = sendMessage(appkey, appid, servicePath);

	respMsgByte = (respMsgByte == null) ? new byte[0] : respMsgByte;
	bos.write(respMsgByte);
	bos.flush();
	bos.close();
获取二进制流:

public static Object[] httpSend4Byte(HttpURLConnection httpConn, MethodName methodName, CharsetName requestCharset,Map<String, String> headerMap, String message, Integer timeout, Integer connectTimeout,Set<String> getRespHeaderKeys, Integer reConnect) throws IOException {
        Object[] result = new Object[3];
        OutputStream os = null;
        BufferedWriter bw = null;
        DataInputStream dis = null;
        BufferedInputStream bis = null;
        ByteArrayOutputStream baoStream = null;
        try {
            // 设置请求方式,默认GET
            String method = "GET";
            if (methodName != null) {
                method = methodName.getMethod();
            }
            httpConn.setRequestMethod(method);
            httpConn.setDoInput(true);
            httpConn.setDoOutput(true);
            String reqCharset = "UTF-8";
            if (requestCharset != null) {
                reqCharset = requestCharset.getCharsetName();
            }
            // 设置响应超时时间,毫秒级
            if (timeout != null) {
                httpConn.setReadTimeout(timeout);
            }
            // 设置连接超时时间,毫秒级
            if (connectTimeout != null) {
                httpConn.setConnectTimeout(connectTimeout);
            }
            // 设置HTTP Headers
            if (headerMap != null) {
                Set<String> keys = headerMap.keySet();
                if (keys != null && keys.size() > 0) {
                    for (String key : keys) {
                        httpConn.setRequestProperty(key, headerMap.get(key));
                    }
                }
            }
            // 建立连接,并重连
            ClientUtil.createConnect(httpConn, reConnect);
            // 非GET请求且发送消息体不为空时建立输出流
            if (!"GET".equals(method) && StringUtils.isNotEmpty(message)) {
                os = httpConn.getOutputStream();
                bw = new BufferedWriter(new OutputStreamWriter(os, reqCharset));
                bw.write(message == null ? "" : message);
                bw.flush();
            }

            // 接收并解析响应报文
            byte[] bRecByte = new byte[1025];
            // 读取HTTP Status-Code
            result[0] = httpConn.getResponseCode();
            // 读取相应的响应 HTTP Header
            if (getRespHeaderKeys != null && getRespHeaderKeys.size() > 0) {
                Map<String, String> respHeaders = new HashMap<String, String>();
                for (String key : getRespHeaderKeys) {
                    respHeaders.put(key, httpConn.getHeaderField(key));
                }
                result[1] = respHeaders;
            }
            // 读取消息体
            dis = new DataInputStream(httpConn.getInputStream());
            bis = new BufferedInputStream(dis);
            int nByte = -1;
            baoStream = new ByteArrayOutputStream();
            nByte = bis.read(bRecByte, 0, 1024);
            while (nByte != -1) {
                baoStream.write(bRecByte, 0, nByte);
                nByte = bis.read(bRecByte, 0, 1024);
            }
            result[2] = baoStream.toByteArray();
        } catch (IOException e) {
            throw e;
        } finally {
            if (bw != null) {
                bw.flush();
                bw.close();
            }
            if (os != null) {
                os.close();
            }
            if (baoStream != null) {
                baoStream.flush();
                baoStream.close();
            }
            if (bis != null) {
                bis.close();
            }
            if (dis != null) {
                dis.close();
            }
        }
        return result;
    }

加载中
返回顶部
顶部