我的代码:
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";
}