sinat_25213573 2015-03-07 13:35 采纳率: 16.7%
浏览 3090
已采纳

C#新手想问为什么我运行后会显示连接已重置~~~~~

我想将文件上传至阿里云的OSS中,点击button就会显示连接已重置,不知道是端口的问题还是其他的?请各位大神指教~

设置method="post" enctype="multipart/form-data",里面有一个 和上传按钮button。

protected void button_Click(object sender, EventArgs e)
{
HttpRequest request = System.Web.HttpContext.Current.Request;
HttpFileCollection FileCollect = request.Files; //获取成HttpFileCollection对象集合,集合中包含了上传所有内容,所有的上传的内容都在集合里了
if (FileCollect.Count > 0) //如果集合的数量大于0
{
foreach (string str in FileCollect)
{
HttpPostedFile file = FileCollect[str]; //用key获取单个文件对象HttpPostedFile
string Path = "/" + FileCollect.Get(str).FileName;
string AbsolutePath = Server.MapPath(Path);

MultipartUploadSample.UploadMultipart("cpu-maitian", file_value.Text.ToString(), AbsolutePath, 1024 * 1024);
}
}
}

}

namespace Aliyun.OpenServices.OpenStorageService.Samples
{
public class MultipartUploadSample
{
private const string _accessId = "";
private const string _accessKey = "";
private const string _endpoint = "http://oss-cn-beijing.aliyuncs.com";

    private static readonly OssClient _ossClient = new OssClient(_endpoint, _accessId, _accessKey);

    /// <summary>
    /// 分片上传。
    /// </summary>
    /// <param name="bucketName"><see cref="Bucket"/></param>
    /// <param name="objectName"><see cref="OssObject"/></param>
    /// <param name="fileToUpload">指定分片上传文件路径</param>
    /// <param name="partSize">分片大小(单位:字节)</param>
    public static void UploadMultipart(String bucketName, String objectName, String fileToUpload, int partSize)
    {
        var uploadId = InitiateMultipartUpload(bucketName, objectName);
        var partETags = BeginUploadPart(bucketName, objectName, fileToUpload, uploadId, partSize);
        var completeResult = CompleteUploadPart(bucketName, objectName, uploadId, partETags);
        Console.WriteLine("Upload multipart result : " + completeResult.Location);
    }

    /// <summary>
    /// 分片拷贝。
    /// </summary>
    /// <param name="targetBucket">目标<see cref="Bucket"/></param>
    /// <param name="targetKey">目标<see cref="OssObject"/></param>
    /// <param name="sourceBucket">源<see cref="Bucket"/></param>
    /// <param name="sourceKey">源<see cref="OssObject"/></param>
    /// <param name="partSize">分片大小(单位:字节)</param>
    public static void UploadMultipartCopy(String targetBucket, String targetKey, String sourceBucket, String sourceKey, int partSize)
    {
        var uploadId = InitiateMultipartUpload(targetBucket, targetKey);
        var partETags = BeginUploadPartCopy(targetBucket, targetKey, sourceBucket, sourceKey, uploadId, partSize);
        var completeResult = CompleteUploadPart(targetBucket, targetKey, uploadId, partETags);

        Console.WriteLine("Upload multipart copy result : ");
        Console.WriteLine(completeResult.Location);
    }

    private static string InitiateMultipartUpload(String bucketName, String objectName)
    {
        var request = new InitiateMultipartUploadRequest(bucketName, objectName);
        var result = _ossClient.InitiateMultipartUpload(request);
        return result.UploadId;
    }

    private static List<PartETag> BeginUploadPart(String bucketName, String objectName, String fileToUpload,
        String uploadId, int partSize)
    {
        var fileSize = -1;
        using (var fs = File.Open(fileToUpload, FileMode.Open))
        {
            fileSize = (int)fs.Length;
        }

        var partCount = fileSize / partSize;
        if (fileSize % partSize != 0)
        {
            partCount++;
        }

        var partETags = new List<PartETag>();
        for (var i = 0; i < partCount; i++)
        {
            using (FileStream fs = File.Open(fileToUpload, FileMode.Open))
            {
                var skipBytes = (long)partSize * i;
                fs.Seek(skipBytes, 0);
                var size = (partSize < fileSize - skipBytes) ? partSize : (fileSize - skipBytes);
                var request = new UploadPartRequest(bucketName, objectName, uploadId);
                request.InputStream = fs;
                request.PartSize = size;
                request.PartNumber = i + 1;
                var result = _ossClient.UploadPart(request);
                partETags.Add(result.PartETag);
            }
        }
        return partETags;
    }

    private static List<PartETag> BeginUploadPartCopy(String targetBucket, String targetKey, String sourceBucket, String sourceKey,
        String uploadId, int partSize)
    {
        var metadata = _ossClient.GetObjectMetadata(sourceBucket, sourceKey);
        var fileSize = metadata.ContentLength;

        var partCount = (int)fileSize / partSize;
        if (fileSize % partSize != 0)
        {
            partCount++;
        }

        var partETags = new List<PartETag>();
        for (var i = 0; i < partCount; i++)
        {
            var skipBytes = (long)partSize * i;
            var size = (partSize < fileSize - skipBytes) ? partSize : (fileSize - skipBytes);
            var request =
                new UploadPartCopyRequest(targetBucket, targetKey, sourceBucket, sourceKey, uploadId);
            request.PartSize = size;
            request.PartNumber = i + 1;
            request.BeginIndex = skipBytes;
            var result = _ossClient.UploadPartCopy(request);
            partETags.Add(result.PartETag);
        }
        return partETags;
    }

    private static CompleteMultipartUploadResult CompleteUploadPart(String bucketName, String objectName,
        String uploadId, List<PartETag> partETags)
    {
        var completeMultipartUploadRequest =
            new CompleteMultipartUploadRequest(bucketName, objectName, uploadId);
        foreach (var partETag in partETags)
        {
            completeMultipartUploadRequest.PartETags.Add(partETag);
        }

        return _ossClient.CompleteMultipartUpload(completeMultipartUploadRequest);
    }
}
  • 写回答

5条回答 默认 最新

  • 微wx笑 Java领域优质创作者 2015-03-08 00:19
    关注

    将文件上传至阿里云的OSS中,你的程序中有登录吗?没通过身份验证连接当然会被重置了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 ios 新安装app收不到fcm推送
  • ¥15 有没有实力的写手?有过成品的优先
  • ¥15 图像信息库的建立与识别
  • ¥15 韩国网站购物,KG支付的支付回调如何解决
  • ¥15 workstation导入ovf文件,报错,怎么解决呢?
  • ¥15 关于#c语言#的问题:构成555单稳态触发器,采用LED指示灯延时时间,对延时时间进行测量并显示(如楼道声控延时灯)需要Proteus仿真图和C语言代码
  • ¥15 workstation加载centos进入emergency模式,查看日志报警如图,怎样解决呢?
  • ¥50 如何用单纯形法寻优不能精准找不到给定的参数,并联机构误差识别,给定误差有7个?matlab
  • ¥15 workstation加载centos进入emergency模式,查看日志报警如图,没有XFS,怎样解决呢?
  • ¥15 应用商店如何检测在架应用内容是否违规?