chengkun
2025-06-05 4080b5997b38ca84b3b203c7101dcadb97b76925
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2020 All Rights Reserved.
 */
package com.alipay.easysdk.kernel.util;
 
import com.alipay.easysdk.kernel.AlipayConstants;
import com.google.common.base.Preconditions;
 
import java.io.File;
 
/**
 * HTTP multipart/form-data格式相关工具类
 *
 * @author zhongyu
 * @version : MulitpartUtil.java, v 0.1 2020年02月08日 11:26 上午 zhongyu Exp $
 */
public class MultipartUtil {
    /**
     * 获取Multipart分界符
     *
     * @param boundary 用作分界的随机字符串
     * @return Multipart分界符
     */
    public static byte[] getEntryBoundary(String boundary) {
        return ("\r\n--" + boundary + "\r\n").getBytes();
    }
 
    /**
     * 获取Multipart结束标记
     *
     * @param boundary 用作分界的随机字符串
     * @return Multipart结束标记
     */
    public static byte[] getEndBoundary(String boundary) {
        return ("\r\n--" + boundary + "--\r\n").getBytes();
    }
 
    /**
     * 获取Multipart中的文本参数结构
     *
     * @param fieldName  字段名称
     * @param fieldValue 字段值
     * @return 文本参数结构
     */
    public static byte[] getTextEntry(String fieldName, String fieldValue) {
        String entry = "Content-Disposition:form-data;name=\""
                + fieldName
                + "\"\r\nContent-Type:text/plain\r\n\r\n"
                + fieldValue;
        return entry.getBytes(AlipayConstants.DEFAULT_CHARSET);
    }
 
    /**
     * 获取Multipart中的文件参数结构(不含文件内容,只有文件元数据)
     *
     * @param fieldName 字段名称
     * @param filePath  文件路径
     * @return 文件参数结构(不含文件内容)
     */
    public static byte[] getFileEntry(String fieldName, String filePath) {
        String entry = "Content-Disposition:form-data;name=\""
                + fieldName
                + "\";filename=\""
                + getFile(filePath).getName()
                + "\"\r\nContent-Type:application/octet-stream"
                + "\r\n\r\n";
        return entry.getBytes(AlipayConstants.DEFAULT_CHARSET);
    }
 
    private static File getFile(String filePath) {
        File file = new File(filePath);
        Preconditions.checkArgument(file.exists(), file.getAbsolutePath() + "文件不存在");
        Preconditions.checkArgument(file.getName().contains("."), "文件名必须带上正确的扩展名");
        return file;
    }
}