使用OkHttp + Spring上传多部分文件

时间:2015-06-08 11:13:56

标签: android spring spring-mvc okhttp

我最近切换到OkHttp。切换后,下面的代码执行上传。

RequestBody requestBody = new MultipartBuilder()
                        .type(MultipartBuilder.FORM)
                        .addPart(
                                Headers.of("Content-Disposition", "form-data; name=\"qqfile\""),
                                RequestBody.create(
                                        MediaType.parse(filename),
                                        new File(filename)))
                        .build();

如果您比较图片,则第二张图片有multipartFiles size = 0。它应该是size = 1。如何使用multipartHttpRequest正确填充OkHttp以使服务器接受成功上传?

Correct In-Correct

控制器代码

import org.apache.commons.fileupload.servlet.ServletFileUpload;

import org.springframework.http.MediaType;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.util.WebUtils;

@RequestMapping (
        method = RequestMethod.POST,
        value = "/upload",
        produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8"
)
public String upload(
        HttpServletRequest request,
        HttpServletResponse response
) throws IOException {
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if (isMultipart) {
        MultipartHttpServletRequest multipartHttpRequest = 
        WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class);

        final List<MultipartFile> files = multipartHttpRequest.getFiles("qqfile");

        if (files.isEmpty()) {
            LOG.error("qqfile name missing in request or no file uploaded");
            return some error code here
        }

        MultipartFile multipartFile = files.iterator().next();
        //process file code below
    }
    return failure;
}

2 个答案:

答案 0 :(得分:3)

您可以更轻松地使用MultipartFile:

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String upload(@RequestParam("qqfile") MultipartFile file) throws IOException {
    if (!file.isEmpty()) {
        // ...
    }
    return "failure";
}

然后,用OkHttp:

RequestBody body = new MultipartBuilder()
    .addFormDataPart("qqfile", filename, RequestBody.create(MediaType.parse("media/type"), new File(filename)))
    .type(MultipartBuilder.FORM)
    .build();

Request request = new Request.Builder()
    .url("/path/to/your/upload")
    .post(body)
    .build();

OkHttpClient client = new OkHttpClient();
Response response = client.newCall(request).execute();

这对我很好。

小心使用MediaType.parse(filename),你必须传递一个有效的类型,如text / plain,application / json,application / xml ......

答案 1 :(得分:2)

Builder requestBodyBuilder = new MultipartBody.Builder()
                        .setType(MultipartBody.FORM);
File file= new File(FILE_PATH + FILE_NAME);
requestBodyBuilder.addFormDataPart("file", FILE_NAME, RequestBody.create(MultipartBody.FORM, file));
<块引用>

fileVO.getOriginalFlnm()

您可以省略此字段。

并且您还必须在标头中设置“MultipartHttpServletRequest”参数和消费、生产

@PostMapping(path = "/save", consumes = "multipart/*", produces = "application/json;charset=utf-8")
public boolean CONTROLLER(MultipartHttpServletRequest request, @RequestParam Map<String, Object> param) {
    boolean result = SERVICE.save(request, param);
    return result;
}