我有以下问题。
我有一个用C ++ / CLI编写的ActiveX控件,我在客户端使用它。其中一个控制方法按固定块大小返回二进制文件(例如1024)。此文件已压缩。
我需要使用C ++ / CLI编写方法,它将每个文件块发送到服务器。我有Java Spring MVC方法,它接收这个byte []块并将其附加到文件。换句话说,我只是通过块上传zip文件。
我的问题是,虽然两个文件的文件大小(原始文件及其在服务器上的副本),但该文件的MD5校验和不一样,我无法使用zip打开此文件。该文件已损坏。
我只是将每个块字节[]转换为BASE64并使用我的常规POST请求将其发送到服务器:
我的代码如下:
int SceneUploader::AddChunk(int id, array ^buffer,int size){
WebRequest^ request = WebRequest::Create(AddChunkURI);
request->Method = "POST";
request->ContentType = "application/x-www-form-urlencoded";
System::String ^base64Image = Convert::ToBase64String(buffer);
String ^param = "id="+id+"&chunk="+base64Image+"&len="+size;
//Just make another copy of the file to verify that sent byted are ok!!!
String ^path = "C:\Users\dannyl\AppData\Local\Temp\test.zip";
FileStream ^MyFileStream = gcnew FileStream(path, FileMode::Append, FileAccess::Write);
MyFileStream->Write(buffer,0,size);
MyFileStream->Close();
//Adding the byteArray to the stream.
System::IO::Stream ^stream = request->GetRequestStream();
System::IO::StreamWriter ^streamWriter = gcnew System::IO::StreamWriter(stream);
streamWriter->Write(param);
streamWriter->Close();
HttpWebResponse^ response = dynamic_cast(request->GetResponse());
Stream^ dataStream = response->GetResponseStream();
StreamReader^ reader = gcnew StreamReader( dataStream );
String^ responseFromServer = reader->ReadToEnd();
return System::Int32::Parse(responseFromServer);
}
我的MVC控制器看起来像这样:
@RequestMapping(value="/addChunk.dlp",method = RequestMethod.POST)
@ResponseBody
public String addChunk(@RequestParam("id") String id,
@RequestParam("chunk") String chunk,
@RequestParam("len") String len){
try{
BASE64Decoder decoder = new BASE64Decoder();
Integer length = Integer.decode(len);
byte[] decodedBytes = new byte[length];
decodedBytes = decoder.decodeBuffer(chunk.trim());
File sceneFile = new File(VAULT_DIR+id+".zip");
if (!sceneFile.exists()){
sceneFile.createNewFile();
}
long fileLength = sceneFile.length();
RandomAccessFile raf = new RandomAccessFile(sceneFile, "rw");
raf.seek(fileLength);
raf.write(decodedBytes);
raf.close();
}
catch(FileNotFoundException ex){
ex.getStackTrace();
System.out.println(ex.getMessage());
return "-1";
}
catch(IOException ex){
ex.getStackTrace();
System.out.println(ex.getMessage());
return "-1";
}
return "0";
}
我究竟做错了什么?
更新:问题解决了。原始Base64字符串有'+'字符,在将请求提交给服务器后,chunk参数由Spring进行URL解码,所有'+'字符都被空格替换,因此zip文件已损坏。请给我回复我的+50声望:)
谢谢,
丹尼。