问题描述
各位大神,最近用阿里云的OSS,我把字符串用gzip算法压缩后上传到服务器,再取回来,解压,值没变,但为什么解压不了?报“Base-64字符串中的无效字符”的错误,求各位大神们帮忙看看?代码如下:classProgram{staticStringaccessKeyId="******";staticStringaccessKeySecret="******";staticStringbucketName="***";publicstaticboolputObject(StringbucketName,Stringkey,StringstrData){try{//初始化OSSClientbyte[]dataBytes;//Compress(Convert.FromBase64String(Data));OssClientclient=newOssClient(accessKeyId,accessKeySecret);//创建上传Object的MetadataObjectMetadatameta=newObjectMetadata();using(MemoryStreamms=newMemoryStream()){using(StreamWritersw=newStreamWriter(ms,Encoding.UTF8)){sw.Write(strData);sw.Flush();ms.Position=0;dataBytes=ms.ToArray();meta.ContentLength=dataBytes.Length;PutObjectResultresult=client.PutObject(bucketName,key,ms,meta);returntrue;}}//打印ETag//System.out.println(result.getETag());}catch(Exceptionex){returnfalse;}}publicstaticstringgetObject(StringbucketName,Stringkey){//初始化OSSClientOssClientclient=newOssClient(accessKeyId,accessKeySecret);//获取Object,返回结果为OSSObject对象OssObjectossobj=client.GetObject(bucketName,key);//获取Object的输入流byte[]buffer=newbyte[4096];byte[]dataBytes;using(MemoryStreamms=newMemoryStream()){//上传Object.intlength=0;while((length=ossobj.Content.Read(buffer,0,4096))>0){ms.Write(buffer,0,length);}ms.Position=0;dataBytes=ms.ToArray();}returnSystem.Text.Encoding.UTF8.GetString(dataBytes);}staticvoidMain(string[]args){stringss="123456";ss=GZipCompressString(ss);strings=GZipDecompressString(ss);stringkey="SYGLDB_Demo/ss.txt";boolb=putObject(bucketName,key,ss);Console.WriteLine(b);ss=getObject(bucketName,key);ss=GZipDecompressString(ss);Console.WriteLine(ss);Console.Read();}#regionGZip///<summary>///将传入字符串以GZip算法压缩后,返回Base64编码字符///</summary>///<paramname="rawString">需要压缩的字符串</param>///<returns>压缩后的Base64编码的字符串</returns>publicstaticstringGZipCompressString(stringrawString){if(string.IsNullOrEmpty(rawString)||rawString.Length==0){return"";}else{byte[]rawData=System.Text.Encoding.UTF8.GetBytes(rawString.ToString());byte[]zippedData=Compress(rawData);return(string)(Convert.ToBase64String(zippedData));}}///<summary>///GZip压缩///</summary>///<paramname="rawData"></param>///<returns></returns>privatestaticbyte[]Compress(byte[]rawData){MemoryStreamms=newMemoryStream();GZipStreamcompressedzipStream=newGZipStream(ms,CompressionMode.Compress,true);compressedzipStream.Write(rawData,0,rawData.Length);compressedzipStream.Close();returnms.ToArray();}///<summary>///将传入的二进制字符串资料以GZip算法解压缩///</summary>///<paramname="zippedString">经GZip压缩后的二进制字符串</param>///<returns>原始未压缩字符串</returns>publicstaticstringGZipDecompressString(stringzippedString){if(string.IsNullOrEmpty(zippedString)||zippedString.Length==0){return"";}else{byte[]zippedData=Convert.FromBase64String(zippedString.ToString());return(string)(System.Text.Encoding.UTF8.GetString(Decompress(zippedData)));}}///<summary>///ZIP解压///</summary>///<paramname="zippedData"></param>///<returns></returns>privatestaticbyte[]Decompress(byte[]zippedData){MemoryStreamms=newMemoryStream(zippedData);GZipStreamcompressedzipStream=newGZipStream(ms,CompressionMode.Decompress);MemoryStreamoutBuffer=newMemoryStream();byte[]block=newbyte[1024];while(true){intbytesRead=compressedzipStream.Read(block,0,block.Length);if(bytesRead<=0)break;elseoutBuffer.Write(block,0,bytesRead);}compressedzipStream.Close();returnoutBuffer.ToArray();}#endregion}
解决方案
解决方案二:
自己仔细检查吧,应该是小问题.
解决方案三:
这么长的代码,而且你也没标出来是哪行抛错撸主单步调试一下,应该可以自己解决掉的实在不行,把报错行的代码定位出来,这样人家才方便帮你分析。