求一个java项目参考:webservice股票接口,输入查询某支股票以及GIF走势图。

本人写了一部分但是返回图老是接收不到,郁闷!求做过的发一个完整的参考下!

gif返回的也是一个路径,先看浏览器能否支持打开,这样定位是服务器路径有问题还是你客户端接收处理有问题

我用的火狐,在做一个官网,天气啊 股票 车票之类的API接口,我可以把我代码发下能帮忙看下吗?

package com.flw.web.stock;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage; //这个是根据输入股票代码从webservice公共接口
import java.io.*;
import java.lang.Integer; 传递一个股票走势图
import java.net.URL;
import java.net.URLConnection;

public class StockReport {

private String theStockCode;//  前台JSP输入的股票代码
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://web.juhe.cn:8080/finance/stock/szall");   //这个是公共接口可以写别的接口

        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.bmp"); //这个是返回一个bmp或者一个JPG图片存储到这个位置!
    if (saveFile.exists()) { 
        System.out.println(saveFile.exists());        
        saveFile.delete();
    }

    srcImage = ImageIO.read(in);

    ImageIO.write(srcImage, "bmp", saveFile);
    in.close();

}

public String execute() throws Exception {
    InputStream is = getSoapInputStream(theStockCode, theType);
    saveImageAsGif(is);//这里调用图片传给图片存储
    return "yes";
}

}

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" // struts配置
"http://struts.apache.org/dtds/struts-2.0.dtd">






/index.jsp

    <action name="stock" class="com.flw.web.stock.StockReport">
        <result name="yes">/stockInfo.jsp</result>
    </action>
</package>

<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%> //输入代码JSP

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


股票查询






<!--

-->



股票代号:如:sh000001

预览类型:如:D:日[默认]、W:周、M:月




<%@ page language="java" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> //返回图片JSP

<title>中国股票GIF日/周/月 K 线图</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->





浏览器打开是无法显示 在execute() 方法里边打印是图片的首地址! 唉 醉了