Rop 文件上传解决思路

由于服务请求报文是一个文本,无法直接传送二进制的文件内容,因此必须采用某种
转换机制将二进制的文件内容转换为字符串。Rop 采用如下的方式对上传文件进行编码:
<fileType>@<BASE64( 文件内容 )>
<fileType>代表文件类型,文件内容采用 BASE64 算法进行编码,这样二进制的文件
内容就可以转换为一个字符串,两者“@”字符分隔。服务端接收到上传的文件后,即可
解析出文件的类型和文件的内容。
Rop 定义了一个 UploadFile,代表一个上传的文件,来看一下 UploadFile 的定义:

package com.rop.request;
import com.rop.annotation.IgnoreSign;
import org.springframework.util.FileCopyUtils;
import java.io.File;
import java.io.IOException;
@IgnoreSign ①
public class UploadFile {
  private String fileType;
  private byte[] content;
  public UploadFile(String fileType, byte[] content) {
    this.content = content;
    this.fileType = fileType;
  }
  public UploadFile(File file) {
    try {
      this.content = FileCopyUtils.copyToByteArray(file);
      this.fileType = file.getName().substring(file.getName().lastIndexOf('.')+1);
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
  }
  public String getFileType() {
    return fileType;
  }
  public byte[] getContent() {
    return content;
  }
}

 

posted @ 2015-04-17 15:59  欢歌911  阅读(341)  评论(0编辑  收藏  举报