Java handles multipart/mixed requests

Keywords: Java Spring

I. Multipartition/mixed requests

_Multpart/mixed and multipart/form-date are both multi-file upload formats. The difference is that multipart/form-data is a special form upload, in which the contents of common fields are constructed according to the general request body, and the contents of file fields are constructed according to the multipart request body. When dealing with multipart/form-data requests, the backend will set up temporary folders on the server to store the contents of files. This article . The multipart/mixed request will upload the content of each field, whether it is a normal field or a file field, into a Stream stream, so the back end must process the multipart/mixed content from the Stream stream.

Servlet handles multipart/mixed requests

            Part signPart = request.getPart(Constants.SIGN_KEY);
            Part appidPart = request.getPart(Constants.APPID_KEY);
            Part noncestrPart = request.getPart(Constants.NONCESTR_KEY);
            Map<String, String[]> paramMap = new HashMap<>(8);
            paramMap.put(signPart.getName(), new String[]{stream2Str(signPart.getInputStream())});
            paramMap.put(appidPart.getName(), new String[]{stream2Str(appidPart.getInputStream())});
            paramMap.put(noncestrPart.getName(), new String[]{stream2Str(noncestrPart.getInputStream())});
    private String stream2Str(InputStream inputStream) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            String line;
            StringBuffer buffer = new StringBuffer();
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }
            return buffer.toString();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "";
    }

3. Spring MVC handles multipart/mixed requests

    @ResponseBody
    @RequestMapping(value = {"/token/user/uploadImage.yueyue", "/token/user/uploadImage"}, method = {RequestMethod.POST, RequestMethod.GET})
    public AjaxList uploadImage(
             @RequestPart (required = false) String token,
             @RequestPart (required = false) String sign,
             @RequestPart (required = false) String appid,
             @RequestPart (required = false) String noncestr,
             @RequestPart MultipartFile avatar, HttpServletRequest request) {

             }

Posted by TheBentinel.com on Sun, 21 Apr 2019 18:51:33 -0700