C#代码转JAVA C#是关于WEBSERVICE的,如何将C#代码转换成JAVA的代码,望高手指教. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 转换的文件在附件中using System;using System.Web;using System.Data;using System.Xml;using System.Net;using System.IO;using System.Text;using System.Text.RegularExpressions;using System.Drawing;using System.Drawing.Imaging;using System.Collections.Specialized;namespace CDJJ.Service.WebDAL{ /// <summary> /// QueryWeb 的摘要说明。 /// </summary> public abstract class QueryBase { private static CDJJ.Service.Common.Configuration WebQuery = CDJJ.Service.Common.Configuration.Create("Config\\WebQuery.xml"); private static Regex BeRemoved = new Regex(@"\s+(?=<)|\s+$|(?<=>)\s+", RegexOptions.Compiled); private static RegexOptions ContentRegexOptions = RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.ExplicitCapture | RegexOptions.Compiled; private readonly static int TIMEOUT = 15000; private CookieContainer _cookieCon = new CookieContainer(); private CredentialCache _credentials; private XmlNode _currentCommand; /// <summary> /// 当前Command节点 /// </summary> protected XmlNode CurrentCommand { get { return this._currentCommand; } } protected QueryBase(string commandType) { this._currentCommand = WebQuery.GetNode("Command[@type='" + commandType + "']"); } #region 处理内容 protected DataTable GetDataTable(string patternPath, string content) { string fileName = this._currentCommand.SelectSingleNode(patternPath).InnerText; string pattern = PatternReader.Instance.GetPattern(fileName); //解析 DataTable dt = this.ParseDataTable(pattern, content); return dt; } protected DataSet GetDataSet(string patternPath, string content) { DataTable dt = this.GetDataTable(patternPath, content); DataSet ds = new DataSet(); ds.Tables.Add(dt); return ds; } #region 解析内容 private DataTable ParseDataTable(string pattern, string content) { //解析列 string temp = BeRemoved.Replace(pattern, ""); Regex regex = new Regex(temp, ContentRegexOptions); string[] columnNames = regex.GetGroupNames(); DataTable dt = new DataTable(); for (int i = 1; i < columnNames.Length; i++) { dt.Columns.Add(columnNames[i], typeof(String)); } //解析列表内容 temp = BeRemoved.Replace(content, ""); foreach (Match m in regex.Matches(temp)) { DataRow row = dt.NewRow(); for (int i = 1; i < columnNames.Length; i++) { temp = Convert.ToString(m.Groups[columnNames[i]]); temp = temp.Replace(" ", ""); row[columnNames[i]] = temp; } dt.Rows.Add(row); } return dt; } #endregion #endregion #region 处理图片 protected byte[] ProcessPicture(bool png, int destWidth, int destHeight, byte[] srcBytes) { System.Drawing.Image img = null; System.Drawing.Bitmap bmp = null; try { //设置缩略图大小 img = System.Drawing.Image.FromStream(new System.IO.MemoryStream(srcBytes)); int srcWidth = img.Width; int srcHeight = img.Height; ImageFormat destFormat = img.RawFormat; if (img.RawFormat.Equals(ImageFormat.Png) && srcWidth <= destWidth && srcHeight <= destHeight) { return srcBytes; } //如果原始图片是Gif,且宽和高都小于指定的宽和高,则保持图像不变。 else if (img.RawFormat.Equals(ImageFormat.Gif) && !png && srcWidth <= destWidth && srcHeight <= destHeight) { return srcBytes; } else { if (srcWidth <= destWidth) { destWidth = srcWidth; } if (srcHeight <= destHeight) { destHeight = srcHeight; } //计算目标图片和原始图片的宽和高的比率 double destRate = destWidth * 1.0 / destHeight; double srcRate = srcWidth * 1.0 / srcHeight; if (srcRate > destRate) { destHeight = (int)(destWidth / srcRate); } else if (srcRate < destRate) { destWidth = (int)(destHeight * srcRate); } if (png) { destFormat = ImageFormat.Png; } else if (!img.RawFormat.Equals(ImageFormat.Gif) && !img.RawFormat.Equals(ImageFormat.Png)) { //原始图片是gif或png,则保持格式不变,其它情况目标格式定为jpeg。 destFormat = ImageFormat.Jpeg; } //生成缩略图 bmp = new Bitmap(destWidth, destHeight); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp); g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; g.DrawImage(img, new Rectangle(0, 0, destWidth, destHeight), new Rectangle(0, 0, srcWidth, srcHeight), System.Drawing.GraphicsUnit.Pixel); //使用八叉树进行颜色量化 if (!destFormat.Equals(ImageFormat.Jpeg)) { OctreeQuantizer quantizer = new OctreeQuantizer(255, 5); img = quantizer.Quantize(bmp); } else { img = bmp; } //保存图片返回字节流 System.IO.MemoryStream ms = new System.IO.MemoryStream(); img.Save(ms, destFormat); byte[] bytes = ms.GetBuffer(); return bytes; } } finally { if (img != null) { img.Dispose(); } if (bmp != null) { bmp.Dispose(); } } } #endregion #region 获取Web内容 protected string GetPageContent(string url) { return this.GetPageContent(url, null); } protected string GetPageContent(string url, string param) { StringBuilder ret = new StringBuilder(500); HttpWebResponse response = null; try { Uri uri = new Uri(url); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); request.Headers.Add("Accept-Language: zh-cn"); request.Timeout = TIMEOUT; request.AllowAutoRedirect = true; request.CookieContainer = _cookieCon; if (_credentials != null) { request.PreAuthenticate = true; request.Credentials = _credentials; } //传入POST参数的分析 if (!string.IsNullOrEmpty(param)) { request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; string temp = this.EncodeParams(param, System.Text.Encoding.Default); byte[] bytes = Encoding.UTF8.GetBytes(temp); request.ContentLength = bytes.Length; Stream rspStream = request.GetRequestStream(); rspStream.Write(bytes, 0, bytes.Length); rspStream.Close(); } else { request.ContentLength = 0; } //取得请求后返回的的数据 response = (HttpWebResponse)(request.GetResponse()); Stream stream = response.GetResponseStream(); using (StreamReader sr = new StreamReader(stream, System.Text.Encoding.Default)) { Char[] read = new Char[256]; int count = sr.Read(read, 0, 256); while (count > 0) { ret.Append(read, 0, count); count = sr.Read(read, 0, 256); } } return ret.ToString(); } catch (Exception er) { string err = er.ToString(); throw; } finally { if (response != null) { response.Close(); } } } protected byte[] GetFileContent(string url) { WebResponse response = null; byte[] bytes = null; try { Uri uri = new Uri(url); WebRequest request = WebRequest.Create(uri); response = request.GetResponse(); Stream stream = response.GetResponseStream(); using (MemoryStream ms = new MemoryStream()) { int b; while ((b = stream.ReadByte()) != -1) { ms.WriteByte((byte)b); } bytes = ms.ToArray(); } return bytes; } finally { if (response != null) { response.Close(); } } } private string EncodeParams(string param, Encoding enc) { StringBuilder ret = new StringBuilder(); char[] reserved = { '?', '=', '&', '%', '+' }; if (param != null) { int i = 0, j; while (i < param.Length) { j = param.IndexOfAny(reserved, i); if (j == -1) { ret.Append(HttpUtility.UrlEncode(param.Substring(i, param.Length - i), enc)); break; } ret.Append(HttpUtility.UrlEncode(param.Substring(i, j - i), enc)); ret.Append(param.Substring(j, 1)); i = j + 1; } } return ret.ToString(); } #endregion }} 半夜抽风、请教ajax能干什么啊? 看一看??? hibernate查询 关于httpurlconnection 问一个非技术的问题,现在国内的培训市场还有很大的发展空间吗 怎样通过cookie记录浏览者访问过的页面 怎样将redoffice的文件.sxw转化成.DOC格式的或者pdf格式的 在APPLET中使用JDBC访问ACCESS数据库(在线等待) ■■请问高手,在哪里可以根据表的字段名自动生成get set 方法???jbuilder? 在JAVA应用程序中可以连接访问数据库在JAVA小程序中则不能原因出在何处?(急!!) 自学于教学的困惑!!!! 那位大哥麻烦帮小弟解决一下问题
using System;
using System.Web;
using System.Data;
using System.Xml;
using System.Net;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections.Specialized;namespace CDJJ.Service.WebDAL
{
/// <summary>
/// QueryWeb 的摘要说明。
/// </summary>
public abstract class QueryBase
{
private static CDJJ.Service.Common.Configuration WebQuery = CDJJ.Service.Common.Configuration.Create("Config\\WebQuery.xml"); private static Regex BeRemoved = new Regex(@"\s+(?=<)|\s+$|(?<=>)\s+", RegexOptions.Compiled);
private static RegexOptions ContentRegexOptions = RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.ExplicitCapture | RegexOptions.Compiled; private readonly static int TIMEOUT = 15000;
private CookieContainer _cookieCon = new CookieContainer();
private CredentialCache _credentials; private XmlNode _currentCommand; /// <summary>
/// 当前Command节点
/// </summary>
protected XmlNode CurrentCommand
{
get { return this._currentCommand; }
} protected QueryBase(string commandType)
{
this._currentCommand = WebQuery.GetNode("Command[@type='" + commandType + "']");
} #region 处理内容
protected DataTable GetDataTable(string patternPath, string content)
{
string fileName = this._currentCommand.SelectSingleNode(patternPath).InnerText;
string pattern = PatternReader.Instance.GetPattern(fileName); //解析
DataTable dt = this.ParseDataTable(pattern, content);
return dt;
} protected DataSet GetDataSet(string patternPath, string content)
{
DataTable dt = this.GetDataTable(patternPath, content);
DataSet ds = new DataSet();
ds.Tables.Add(dt);
return ds;
}
private DataTable ParseDataTable(string pattern, string content)
{
//解析列
string temp = BeRemoved.Replace(pattern, "");
Regex regex = new Regex(temp, ContentRegexOptions);
string[] columnNames = regex.GetGroupNames(); DataTable dt = new DataTable();
for (int i = 1; i < columnNames.Length; i++)
{
dt.Columns.Add(columnNames[i], typeof(String));
} //解析列表内容
temp = BeRemoved.Replace(content, "");
foreach (Match m in regex.Matches(temp))
{
DataRow row = dt.NewRow();
for (int i = 1; i < columnNames.Length; i++)
{
temp = Convert.ToString(m.Groups[columnNames[i]]);
temp = temp.Replace(" ", "");
row[columnNames[i]] = temp;
}
dt.Rows.Add(row);
}
return dt;
}
#endregion
#endregion #region 处理图片
protected byte[] ProcessPicture(bool png, int destWidth, int destHeight, byte[] srcBytes)
{
System.Drawing.Image img = null;
System.Drawing.Bitmap bmp = null; try
{
//设置缩略图大小
img = System.Drawing.Image.FromStream(new System.IO.MemoryStream(srcBytes));
int srcWidth = img.Width;
int srcHeight = img.Height; ImageFormat destFormat = img.RawFormat; if (img.RawFormat.Equals(ImageFormat.Png)
&& srcWidth <= destWidth && srcHeight <= destHeight)
{
return srcBytes;
}
//如果原始图片是Gif,且宽和高都小于指定的宽和高,则保持图像不变。
else if (img.RawFormat.Equals(ImageFormat.Gif)
&& !png
&& srcWidth <= destWidth && srcHeight <= destHeight)
{
return srcBytes;
}
else
{
if (srcWidth <= destWidth)
{
destWidth = srcWidth;
}
if (srcHeight <= destHeight)
{
destHeight = srcHeight;
} //计算目标图片和原始图片的宽和高的比率
double destRate = destWidth * 1.0 / destHeight;
double srcRate = srcWidth * 1.0 / srcHeight; if (srcRate > destRate)
{
destHeight = (int)(destWidth / srcRate);
}
else if (srcRate < destRate)
{
destWidth = (int)(destHeight * srcRate);
} if (png)
{
destFormat = ImageFormat.Png;
}
else if (!img.RawFormat.Equals(ImageFormat.Gif) && !img.RawFormat.Equals(ImageFormat.Png))
{
//原始图片是gif或png,则保持格式不变,其它情况目标格式定为jpeg。
destFormat = ImageFormat.Jpeg;
}
//生成缩略图
bmp = new Bitmap(destWidth, destHeight);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.DrawImage(img, new Rectangle(0, 0, destWidth, destHeight), new Rectangle(0, 0, srcWidth, srcHeight), System.Drawing.GraphicsUnit.Pixel);
//使用八叉树进行颜色量化
if (!destFormat.Equals(ImageFormat.Jpeg))
{
OctreeQuantizer quantizer = new OctreeQuantizer(255, 5);
img = quantizer.Quantize(bmp);
}
else
{
img = bmp;
} //保存图片返回字节流
System.IO.MemoryStream ms = new System.IO.MemoryStream();
img.Save(ms, destFormat); byte[] bytes = ms.GetBuffer();
return bytes;
}
}
{
if (img != null)
{
img.Dispose();
} if (bmp != null)
{
bmp.Dispose();
}
}
}
#endregion #region 获取Web内容
protected string GetPageContent(string url)
{
return this.GetPageContent(url, null);
} protected string GetPageContent(string url, string param)
{
StringBuilder ret = new StringBuilder(500);
HttpWebResponse response = null; try
{
Uri uri = new Uri(url);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); request.Headers.Add("Accept-Language: zh-cn");
request.Timeout = TIMEOUT;
request.AllowAutoRedirect = true;
request.CookieContainer = _cookieCon; if (_credentials != null)
{
request.PreAuthenticate = true;
request.Credentials = _credentials;
} //传入POST参数的分析
if (!string.IsNullOrEmpty(param))
{
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded"; string temp = this.EncodeParams(param, System.Text.Encoding.Default);
byte[] bytes = Encoding.UTF8.GetBytes(temp);
request.ContentLength = bytes.Length;
Stream rspStream = request.GetRequestStream();
rspStream.Write(bytes, 0, bytes.Length);
rspStream.Close();
}
else
{
request.ContentLength = 0;
} //取得请求后返回的的数据
response = (HttpWebResponse)(request.GetResponse()); Stream stream = response.GetResponseStream(); using (StreamReader sr = new StreamReader(stream, System.Text.Encoding.Default))
{
Char[] read = new Char[256];
int count = sr.Read(read, 0, 256);
while (count > 0)
{
ret.Append(read, 0, count);
count = sr.Read(read, 0, 256);
}
} return ret.ToString();
}
catch (Exception er)
{
string err = er.ToString();
throw;
}
finally
{
if (response != null)
{
response.Close();
}
}
} protected byte[] GetFileContent(string url)
{
WebResponse response = null;
byte[] bytes = null; try
{
Uri uri = new Uri(url);
WebRequest request = WebRequest.Create(uri); response = request.GetResponse();
Stream stream = response.GetResponseStream(); using (MemoryStream ms = new MemoryStream())
{
int b;
while ((b = stream.ReadByte()) != -1)
{
ms.WriteByte((byte)b);
}
bytes = ms.ToArray();
}
return bytes;
}
finally
{
if (response != null)
{
response.Close();
}
}
} private string EncodeParams(string param, Encoding enc)
{
StringBuilder ret = new StringBuilder();
char[] reserved = { '?', '=', '&', '%', '+' }; if (param != null)
{
int i = 0, j;
while (i < param.Length)
{
j = param.IndexOfAny(reserved, i);
if (j == -1)
{
ret.Append(HttpUtility.UrlEncode(param.Substring(i, param.Length - i), enc));
break;
}
ret.Append(HttpUtility.UrlEncode(param.Substring(i, j - i), enc));
ret.Append(param.Substring(j, 1));
i = j + 1;
}
} return ret.ToString();
}
#endregion
}
}