yanzhenjie/AndServer

List<MultipartFile> not supported, while MultipartFile[] works on v2.1.6

Open

#357 aperta il 23 gen 2021

Vedi su GitHub
 (1 commento) (0 reazioni) (0 assegnatari)Java (723 fork)batch import
help wanted

Metriche repository

Star
 (3443 star)
Metriche merge PR
 (Nessuna PR mergiata in 30 g)

Descrizione

My server api:

    /**
     * 接收上传文件并转移到服务器根目录下。
     *
     * @param file 客户端上传的文件
     * @throws IOException
     */
    @PostMapping(path = "/uploadWithInfo")
    UserInfo uploadWithInfo(@FormPart(name = "user") UserInfo userInfo,
                            @FormPart(name = "file") MultipartFile file,
                            @FormPart(name = "fileList", required = false) List<MultipartFile> fileList
    ) throws IOException {
        File localFile = FileUtils.createFile(file);
        file.transferTo(localFile);
        if (fileList != null) {
            userInfo.setUserName(userInfo.getUserId() + " | fileList: " + fileList.size());
        }
        return userInfo;
    }

My client calls:

        UserInfo userInfo = new UserInfo();
        userInfo.setUserId("yanzhenjie");
        userInfo.setUserName("YAN Zhenjie");

        FormBody formBody = FormBody.newBuilder()
                .param("user", JSON.toJSONString(userInfo))
                .build();

        // 传object和文件及多文件
        List<File> files = new ArrayList<>();
        files.add(new File("/sdcard/batteryInfo.json"));
        files.add(new File("/sdcard/userinfo.json"));
        // 第一次上传不带可选参数多个文件
        formBody = FormBody.newBuilder()
                .param("user", JSON.toJSONString(userInfo))
                .file("file", new File("/sdcard/Debug_Log_ST2303/loglist.txt"))
                .build();
        Kalle.post(UrlConfig.UPLOAD_WITH_INFO)
                .body(formBody)
                .perform(new SimpleCallback<UserInfo>(this) {
                    @Override
                    public void onResponse(SimpleResponse<UserInfo, String> response) {
                        Logger.i(UrlConfig.UPLOAD_WITH_INFO + " 第一次上传不带可选参数多个文件 responded form:" + response.succeed());
                    }
                });
        // 第二次上传带可选参数多个文件
        formBody = FormBody.newBuilder()
                .param("user", JSON.toJSONString(userInfo))
                .file("file", new File("/sdcard/Debug_Log_ST2303/loglist.txt"))
                .files("fileList", files)
                .build();
        Kalle.post(UrlConfig.UPLOAD_WITH_INFO)
                .body(formBody)
                .perform(new SimpleCallback<UserInfo>(this) {
                    @Override
                    public void onResponse(SimpleResponse<UserInfo, String> response) {
                        Logger.i(UrlConfig.UPLOAD_WITH_INFO + " 第二次上传带可选参数多个文件 responded form:" + response.succeed());
                    }
                });

The output from the server:

2021-01-23 14:40:04.173 24806-25118/com.yanzhenjie.andserver.sample I/AndServerSample: Path: /uploadWithInfo
2021-01-23 14:40:04.173 24806-25118/com.yanzhenjie.andserver.sample I/AndServerSample: Method: POST
2021-01-23 14:40:04.174 24806-25118/com.yanzhenjie.andserver.sample I/AndServerSample: Param: {"user":["{\"userId\":\"yanzhenjie\",\"userName\":\"YAN Zhenjie\"}"]}
2021-01-23 14:40:04.178 24806-25118/com.yanzhenjie.andserver.sample W/System.err: com.alibaba.fastjson.JSONException: exepct '[', but {, pos 1, json : {"capacityVoltage":0.471,"floatingCharging":false,"lineGatherCharging":0,"mcuAlarmType":0,"powerSourceVoltage":0,"sampleSecondsSinceEpoch":1611384001,"voltage":6.728,"workTemperature":20.0,"name":"BatteryInfo"}
2021-01-23 14:40:04.179 24806-25118/com.yanzhenjie.andserver.sample W/System.err:     at com.alibaba.fastjson.parser.DefaultJSONParser.parseArray(DefaultJSONParser.java:734)
2021-01-23 14:40:04.179 24806-25118/com.yanzhenjie.andserver.sample W/System.err:     at com.alibaba.fastjson.serializer.CollectionCodec.deserialze(CollectionCodec.java:171)
2021-01-23 14:40:04.179 24806-25118/com.yanzhenjie.andserver.sample W/System.err:     at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:704)
2021-01-23 14:40:04.179 24806-25118/com.yanzhenjie.andserver.sample W/System.err:     at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:677)
2021-01-23 14:40:04.190 24806-25118/com.yanzhenjie.andserver.sample W/System.err:     at com.alibaba.fastjson.JSON.parseObject(JSON.java:245)
2021-01-23 14:40:04.191 24806-25118/com.yanzhenjie.andserver.sample W/System.err:     at com.alibaba.fastjson.JSON.parseObject(JSON.java:217)
2021-01-23 14:40:04.191 24806-25118/com.yanzhenjie.andserver.sample W/System.err:     at com.alibaba.fastjson.JSON.parseObject(JSON.java:187)
2021-01-23 14:40:04.191 24806-25118/com.yanzhenjie.andserver.sample W/System.err:     at com.yanzhenjie.andserver.sample.util.JsonUtils.parseJson(JsonUtils.java:80)
2021-01-23 14:40:04.191 24806-25118/com.yanzhenjie.andserver.sample W/System.err:     at com.yanzhenjie.andserver.sample.component.AppMessageConverter.convert(AppMessageConverter.java:50)
2021-01-23 14:40:04.191 24806-25118/com.yanzhenjie.andserver.sample W/System.err:     at com.yanzhenjie.andserver.sample.controller.CentralControllerUploadWithInfoHandler.onHandle(CentralControllerUploadWithInfoHandler.java:106)
2021-01-23 14:40:04.195 24806-25118/com.yanzhenjie.andserver.sample W/System.err:     at com.yanzhenjie.andserver.framework.handler.MappingHandler.handle(MappingHandler.java:174)
2021-01-23 14:40:04.195 24806-25118/com.yanzhenjie.andserver.sample W/System.err:     at com.yanzhenjie.andserver.DispatcherHandler.handle(DispatcherHandler.java:159)
2021-01-23 14:40:04.195 24806-25118/com.yanzhenjie.andserver.sample W/System.err:     at com.yanzhenjie.andserver.DispatcherHandler.handle(DispatcherHandler.java:128)
2021-01-23 14:40:04.195 24806-25118/com.yanzhenjie.andserver.sample W/System.err:     at org.apache.httpcore.protocol.HttpService.doService(HttpService.java:437)
2021-01-23 14:40:04.195 24806-25118/com.yanzhenjie.andserver.sample W/System.err:     at org.apache.httpcore.protocol.HttpService.handleRequest(HttpService.java:342)
2021-01-23 14:40:04.195 24806-25118/com.yanzhenjie.andserver.sample W/System.err:     at org.apache.httpcore.impl.bootstrap.Worker.run(Worker.java:66)
2021-01-23 14:40:04.196 24806-25118/com.yanzhenjie.andserver.sample W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
2021-01-23 14:40:04.200 24806-25118/com.yanzhenjie.andserver.sample W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
2021-01-23 14:40:04.200 24806-25118/com.yanzhenjie.andserver.sample W/System.err:     at java.lang.Thread.run(Thread.java:761)

This is what generated code looks like:

package com.yanzhenjie.andserver.sample.controller;

import android.content.Context;
import android.text.TextUtils;
import com.yanzhenjie.andserver.error.ParamMissingException;
import com.yanzhenjie.andserver.framework.MessageConverter;
import com.yanzhenjie.andserver.framework.cross.CrossOrigin;
import com.yanzhenjie.andserver.framework.handler.MappingHandler;
import com.yanzhenjie.andserver.framework.mapping.Addition;
import com.yanzhenjie.andserver.framework.mapping.Mapping;
import com.yanzhenjie.andserver.framework.view.ObjectView;
import com.yanzhenjie.andserver.framework.view.View;
import com.yanzhenjie.andserver.http.HttpMethod;
import com.yanzhenjie.andserver.http.HttpRequest;
import com.yanzhenjie.andserver.http.HttpResponse;
import com.yanzhenjie.andserver.http.RequestBody;
import com.yanzhenjie.andserver.http.multipart.MultipartFile;
import com.yanzhenjie.andserver.http.multipart.MultipartRequest;
import com.yanzhenjie.andserver.sample.model.UserInfo;
import com.yanzhenjie.andserver.util.MediaType;
import com.yanzhenjie.andserver.util.TypeWrapper;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.Object;
import java.lang.Override;
import java.lang.Throwable;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

/**
 * This file was generated by AndServer automatically and you should NOT edit it.
 */
public final class CentralControllerUploadWithInfoHandler extends MappingHandler {
  private Object mHost;

  public CentralControllerUploadWithInfoHandler(Object host, Mapping mapping, Addition addition,
      CrossOrigin crossOrigin) {
    super(host, mapping, addition, crossOrigin);
    this.mHost = host;
  }

  @Override
  protected View onHandle(HttpRequest request, HttpResponse response) throws Throwable {
    Context context = (Context)request.getAttribute(HttpRequest.ANDROID_CONTEXT);
    String httpPath = request.getPath();
    HttpMethod httpMethod = request.getMethod();

    Object converterObj = request.getAttribute(HttpRequest.HTTP_MESSAGE_CONVERTER);
    MessageConverter converter = null;
    if (converterObj != null && converterObj instanceof MessageConverter) {
      converter = (MessageConverter)converterObj;
    }

    MultipartRequest multiRequest = null;
    if (request instanceof MultipartRequest) {
      multiRequest = (MultipartRequest) request;
    }

    RequestBody requestBody = null;
    if (httpMethod.allowBody()) {
      requestBody = request.getBody();
    }

    Map<String, String> pathMap = getPathVariable(httpPath);

    /** ---------- Building Parameters ---------- **/ 

    UserInfo param0 = null;
    Type param0Type = new TypeWrapper<UserInfo>(){}.getType();
    if (converter != null && multiRequest != null) {
      MultipartFile param0File = multiRequest.getFile("user");
      if (param0File != null) {
        InputStream stream = param0File.getStream();
        MediaType mimeType = param0File.getContentType();
        param0 = converter.convert(stream, mimeType, param0Type);
      }
      if (param0 == null) {
        String param0Str = multiRequest.getParameter("user");
        if (!TextUtils.isEmpty(param0Str)) {
          InputStream stream = new ByteArrayInputStream(param0Str.getBytes());
          MediaType mimeType = MediaType.TEXT_PLAIN;
          param0 = converter.convert(stream, mimeType, param0Type);
        }
      }
    }
    if (param0 == null) {
      throw new ParamMissingException("user");
    }

    MultipartFile param1 = null;
    if (multiRequest != null) {
      param1 = multiRequest.getFile("file");
    }
    if (param1 == null) {
      throw new ParamMissingException("file");
    }

    List<MultipartFile> param2 = null;
    Type param2Type = new TypeWrapper<List<MultipartFile>>(){}.getType();
    if (converter != null && multiRequest != null) {
      MultipartFile param2File = multiRequest.getFile("fileList");
      if (param2File != null) {
        InputStream stream = param2File.getStream();
        MediaType mimeType = param2File.getContentType();
        param2 = converter.convert(stream, mimeType, param2Type);
      }
      if (param2 == null) {
        String param2Str = multiRequest.getParameter("fileList");
        if (!TextUtils.isEmpty(param2Str)) {
          InputStream stream = new ByteArrayInputStream(param2Str.getBytes());
          MediaType mimeType = MediaType.TEXT_PLAIN;
          param2 = converter.convert(stream, mimeType, param2Type);
        }
      }
    }
    Object o = ((CentralController)mHost).uploadWithInfo(param0, param1, param2);
    return new ObjectView(true, o);
  }
}

Guida contributor