using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.IO;
using System.Text;namespace test2
{
    public class BaseFileManager
    {
        public string BaseFileAPIUrl = " "; // api地址        public string Api { get; set; }  // 调用的api接口        public string AccessToken { get; set; } //调用api时需要检查的accessToken        public System.Web.HttpPostedFile file { get; set; }        /// <summary>
        /// 拼接请求链接
        /// </summary>
        /// <returns></returns>
        public string TargetUrl()
        {
            string url = BaseFileAPIUrl + Api + "?method=put";
            if (!string.IsNullOrEmpty(AccessToken))
                url += "&access_token=" + AccessToken;
            return url;
        }
     
        /// <summary>
        /// 文件上传
        /// </summary>
        /// <returns></returns>
        public string UploadRequest()
        {
            string returnValue = "";            //参数的分割符
            string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
            byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(TargetUrl()));
            webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
            webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)";
            webRequest.Method = "POST";
            webRequest.KeepAlive = true;
           
            //对发送的数据不使用缓存
            //webRequest.AllowWriteStreamBuffering = false;            //设置获得响应的超时时间毫秒
            webRequest.Timeout = 150000;
           // webRequest.Date = DateTime.Now;            Stream rs = webRequest.GetRequestStream();
            rs.Write(boundarybytes, 0, boundarybytes.Length);
            //写http头
            string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
            string header = string.Format(headerTemplate, "file", file.FileName, "image/gif");
            byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
            rs.Write(headerbytes, 0, headerbytes.Length);
            //写文件内容
            byte[] fileBuffer = new byte[file.ContentLength];
            file.InputStream.Read(fileBuffer, 0, file.ContentLength);
            rs.Write(fileBuffer, 0, file.ContentLength);
            //写
            byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
            rs.Write(trailer, 0, trailer.Length);
            rs.Close();            WebResponse webResponse = null; m
            try
            {
                webResponse = webRequest.GetResponse();
                Stream stream2 = webResponse.GetResponseStream();
                StreamReader reader2 = new StreamReader(stream2);
                returnValue = reader2.ReadToEnd();
            }
            catch (Exception ex)
            {
                if (webResponse != null)
                {
                    webResponse.Close();
                    webResponse = null;
                }
            }
            finally
            {
                webRequest = null;
            }            return returnValue;
        }
    }    
}
根据上面的C#代码,改为用java编写,请教如何实现。