nyligeng 2015-12-18 11:20 采纳率: 0%
浏览 2344
已结题

求一个java项目参考:webservice股票接口,输入查询股票以及GIF走势图。下面我写的帮看下

我的代码:

private String theStockCode;  //股票编号
private String theType;           //股票走势图:年月日格式

public String getTheStockCode() {
    return theStockCode;
}

public void setTheStockCode(String theStockCode) {
    this.theStockCode = theStockCode;
}

public String getTheType() {
    return theType;
}

public void setTheType(String theType) {
    this.theType = theType;
}

private static String getSoapRequest(String theStockCode, String theType) {
    StringBuilder sb = new StringBuilder();
    sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
            + "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
            + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
            + "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
            + "<soap:Body>    <getStockImage_kByCode xmlns=\"http://WebXml.com.cn/\">"
            + "<theStockCode>" + theStockCode + "</theStockCode>"
            + "<theType>" + theType
            + "</theType>    </getStockImage_kByCode>"
            + "</soap:Body></soap:Envelope>");
    return sb.toString();
}


private static InputStream getSoapInputStream(String theStockCode,
        String theType) throws Exception {
    try {
        String soap = getSoapRequest(theStockCode, theType);
        if (soap == null) {
            return null;
        }
        URL url = new URL(
                "http://www.webxml.com.cn/WebServices/ChinaStockWebService.asmx");
        URLConnection conn = url.openConnection();
        conn.setUseCaches(false);
        conn.setDoInput(true);
        conn.setDoOutput(true);

        conn.setRequestProperty("Content-Length",
                Integer.toString(soap.length()));
        conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
        conn.setRequestProperty("SOAPAction",
                "http://WebXml.com.cn/getStockImage_kByCode");

        OutputStream os = conn.getOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
        osw.write(soap);
        osw.flush();
        osw.close();

        InputStream is = conn.getInputStream();
        return is;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

}

private static void saveImageAsGif(InputStream in) throws Exception {
    BufferedImage srcImage ;

    File saveFile = new File(
            "D:\\下载\\i18n\\WebRoot\\image\\stock.jpg");
    if (saveFile.exists()) { 
        saveFile.delete();
    }

    srcImage= ImageIO.read(in);
    ImageIO.write(srcImage, "jpg", saveFile);
    in.close();

}

public String execute() throws Exception {
    InputStream is = getSoapInputStream(theStockCode, theType);
    System.out.println(is);     //这里打印的是图片首地址 ,老是获取不到图片码好为难   ,这里要是能解决了  我的问题就没了!
    saveImageAsGif(is);
    return "st";
}
  • 写回答

1条回答 默认 最新

  • devmiao 2015-12-18 15:53
    关注
    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作