关于multipart/form-data 的的使用

//demo
class HTTP {
public static def upload(String url,String filename,byte[] con){
String boundary = "----WebKitFormBoundary7MA4YWxxkT" + Fx.random.nextInt(100000)
String name = 'media'
// String filename = filename
String contentType = 'application/octet-stream'

Map header = [
'Content-Type': 'multipart/form-data; boundary=' + boundary
]
String param = '--' + boundary + '\r\n' +
'Content-Disposition: form-data; name=' + name + '; filename=' + filename +'\r\n' +
'Content-Type: ' + contentType + '\r\n' +
'\r\n' ;

    String hh = '\r\n' +
          '--' + boundary + '--\r\n'

List w1 = []
w1.addAll(param.bytes);
w1.addAll(con);
w1.addAll(hh.bytes);
log.info(param)
header["Content-Length"] =w1.size() as String
def (Boolean error, HttpResult ret, String errorMessage) = Fx.http.post(url, header, w1 as byte[] )
if (error) {
log.info(errorMessage)
} else {
log.info(ret)
}

return ret
}

//debug 时候的入口方法
public static void main(String[] args){
def file = [1,2,3,4,5,6] as byte[];
String url ="http://ip:端口号/seeyon/rest/attachment?token=12312312"
// log.info upload(url,"hello.txt","我的世界".bytes);
log.info upload(url,"hello.png",file)

}

}

2023-02-10
0 0