文件上传(v1.8.7)

  • SDK

    1. /**
    2. * 上传文件,读取本地文件
    3. *
    4. * @throws IOException
    5. */
    6. @Test
    7. public void testUpload() throws IOException {
    8. GoodsParam param = new GoodsParam();
    9. param.setGoods_name("iphone6");
    10. GoodsReq req = new GoodsReq("file.upload", param);
    11. String path = this.getClass().getResource("").getPath();
    12. List<UploadFile> files = new ArrayList<>();
    13. // 这里的headImg,idcardImg要跟服务端参数名对应
    14. files.add(new UploadFile("headImg", new File(path + "1.txt")));
    15. files.add(new UploadFile("idcardImg", new File(path + "2.txt")));
    16. GoodsResp result = client.requestFile(req, files);
    17. System.out.println("--------------------");
    18. if (result.isSuccess()) {
    19. System.out.println(result.getData());
    20. } else {
    21. System.out.println("errorMsg:" + result.getMsg());
    22. }
    23. System.out.println("--------------------");
    24. }
  • 服务端处理

    1. @Api(name = "file.upload")
    2. @ApiDocMethod(description = "文件上传")
    3. Object upload(UploadParam param) throws IllegalStateException, IOException {
    4. // 获取上传文件
    5. MultipartFile headImgFile = param.getHeadImg();
    6. MultipartFile idcardImgFile = param.getIdcardImg();
    7. StringBuilder sb = new StringBuilder();
    8. sb.append("表单名:").append(headImgFile.getName()).append(",")
    9. .append("文件大小:").append(headImgFile.getSize()).append(";");
    10. sb.append("表单名:").append(idcardImgFile.getName()).append(",")
    11. .append("文件大小:").append(idcardImgFile.getSize()).append(";");
    12. // headImgFile.getInputStream(); // 返回文件流
    13. // headImgFile.getBytes(); // 返回文件数据流
    14. headImgFile.transferTo(new File("D:/new_" + headImgFile.getOriginalFilename()));
    15. idcardImgFile.transferTo(new File("D:/new_" + idcardImgFile.getOriginalFilename()));
    16. return new ApiResult(sb.toString());
    17. }
  • UploadParam.java

    1. public class UploadParam {

    2. @ApiDocField(description = "商品名称", required = true, example = "iphoneX")
      @NotEmpty(message = "商品名称不能为空")
      @Length(min = 3, max = 20, message = "{goods.name.length}=3,20")
      private String goods_name;

    3. // 这里定义上传的文件,属性名称对应客户端上传的name
      @ApiDocField(description = "头像图片", required = true, dataType = DataType.FILE)
      @NotNull(message = "请上传头像图片")
      private MultipartFile headImg;

    4. @ApiDocField(description = "身份证图片", required = true, dataType = DataType.FILE)
      @NotNull(message = "请上传身份证图片")
      private MultipartFile idcardImg;

    5. //getter,setter
      }


    headImg,idcardImg就是上传的表单名,客户端需要于此对应。

上传内存文件

有些文件不是从本地读取的,而是从其它地方下载到内存中,比如从阿里云下载文件到内存中,不落地。

  1. List<UploadFile> files = new ArrayList<>();
  2. files.add(new UploadFile("headImg","headImg.txt", this.getClass().getResourceAsStream("1.txt")));
  3. files.add(new UploadFile("idcardImg", "idcardImg.txt", this.getClass().getResourceAsStream("2.txt")));
  4. GoodsResp result = client.requestFile(req, files);

或者

  1. List<UploadFile> files = new ArrayList<>();
  2. files.add(new UploadFile("headImg","headImg.txt", byte[]));
  3. files.add(new UploadFile("idcardImg", "idcardImg.txt", byte[]));
  4. GoodsResp result = client.requestFile(req, files);

主要通过UploadFile类的构造方法来区分

  1. /**
  2. * @param name 表单名称,不能重复
  3. * @param file 文件
  4. * @throws IOException
  5. */
  6. public UploadFile(String name, File file)
  7. /**
  8. * @param name 表单名称,不能重复
  9. * @param fileName 文件名
  10. * @param input 文件流
  11. * @throws IOException
  12. */
  13. public UploadFile(String name, String fileName, InputStream input)
  14. /**
  15. * @param name 表单名称,不能重复
  16. * @param fileName 文件名
  17. * @param fileData 文件数据
  18. */
  19. public UploadFile(String name, String fileName, byte[] fileData)