V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Yohann97
V2EX  ›  Java

Java 接收并上传前端传来的 form 表单提交的图片文件到另一个接口

  •  
  •   Yohann97 · 2018-10-13 10:07:26 +08:00 · 4302 次点击
    这是一个创建于 2233 天前的主题,其中的信息可能已经有所发展或是发生改变。

    postman 可以跑通 http://cms.pactera.com:9097/upload/public/32/Other/1013100210-575550.png ,但是代码不会写肿么办,postman 生成的代码是这样的

    OkHttpClient client = new OkHttpClient();

    MediaType mediaType = MediaType.parse("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"); RequestBody body = RequestBody.create(mediaType, "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name="Domain"\r\n\r\nxsh\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name="IsPublic"\r\n\r\ntrue\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name="FileType"\r\n\r\nImage\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name="FileCategory"\r\n\r\nOther\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name="files"; filename="C:\Users\z1557\Pictures\aa.png"\r\nContent-Type: image/png\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"); Request request = new Request.Builder() .url("http://cms.pactera.com:9097/api/services/app/Public/UploadFiles") .post(body) .addHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW") .addHeader("Content-Type", "application/x-www-form-urlencoded") .addHeader("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1laWRlbnRpZmllciI6IjMyIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZSI6IjE1NTcyMzg5MDIxIiwiQXNwTmV0LklkZW50aXR5LlNlY3VyaXR5U3RhbXAiOiI4NWQ4MjhkYS03Y2FjLTQ4NTItOWZmYS0zZTQxMjE5YTJjZWIiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL3JvbGUiOiJBZG1pbiIsInN1YiI6IjMyIiwianRpIjoiMmVkMDEyZTctNmM3My00ZDc0LWE4ZWYtOTIyNzk0YjY3OGUyIiwiaWF0IjoxNTM5Mzk2MjAwLCJuYmYiOjE1MzkzOTYyMDAsImV4cCI6MTUzOTQ4MjYwMCwiaXNzIjoiVmFua2UiLCJhdWQiOiJWYW5rZSJ9.FFbs7JH0Fad1yY5On-ivZCDb8UO-yZ0SE-HMqYFgRjk") .addHeader("Cache-Control", "no-cache") .addHeader("Postman-Token", "c3d0193f-0e32-4c4e-9154-bec83f7d6a62") .build();

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

    postman 的参数在这 Domain:xsh IsPublic:true FileType:Image FileCategory:Other

    BearerToken:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1laWRlbnRpZmllciI6IjMyIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZSI6IjE1NTcyMzg5MDIxIiwiQXNwTmV0LklkZW50aXR5LlNlY3VyaXR5U3RhbXAiOiI4NWQ4MjhkYS03Y2FjLTQ4NTItOWZmYS0zZTQxMjE5YTJjZWIiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL3JvbGUiOiJBZG1pbiIsInN1YiI6IjMyIiwianRpIjoiMmVkMDEyZTctNmM3My00ZDc0LWE4ZWYtOTIyNzk0YjY3OGUyIiwiaWF0IjoxNTM5Mzk2MjAwLCJuYmYiOjE1MzkzOTYyMDAsImV4cCI6MTUzOTQ4MjYwMCwiaXNzIjoiVmFua2UiLCJhdWQiOiJWYW5rZSJ9.FFbs7JH0Fad1yY5On-ivZCDb8UO-yZ0SE-HMqYFgRjk

    14 条回复    2018-10-15 17:59:30 +08:00
    Yohann97
        1
    Yohann97  
    OP
       2018-10-13 10:08:59 +08:00
    大佬们能帮帮我不,好几天了,写不出来,难受
    secaas
        2
    secaas  
       2018-10-13 11:13:41 +08:00
    这是个盗版头像= =
    Yohann97
        3
    Yohann97  
    OP
       2018-10-13 11:34:15 +08:00
    @secaas 盗版,快换了吧,颜色都不正
    ghos
        4
    ghos  
       2018-10-13 15:27:02 +08:00
    aLazarus
        5
    aLazarus  
       2018-10-13 16:09:30 +08:00 via Android
    十一节前写了个使用 formdata 传图片和参数给接口的方法,不知道是不是你这意思?
    dbolo123
        6
    dbolo123  
       2018-10-13 20:34:02 +08:00
    postman 好像可以生成 okhttp 的代码吧?参考下?
    lifespy
        7
    lifespy  
       2018-10-14 11:25:57 +08:00
    这个很简单啊,就行楼上说的,你可以用 okhttp,也可以用自带的
    mmdsun
        8
    mmdsun  
       2018-10-14 11:50:17 +08:00 via Android
    搜索 okhttp 上传文件就 OK 了。
    Yohann97
        9
    Yohann97  
    OP
       2018-10-14 19:20:05 +08:00
    @aLazar 嗯嗯,能发我参考下么
    Yohann97
        10
    Yohann97  
    OP
       2018-10-14 19:21:07 +08:00
    @dbolo123 我在上面贴的就是 postman 生成的,但是没有文件上传的具体写法
    Yohann97
        11
    Yohann97  
    OP
       2018-10-14 19:22:26 +08:00
    @ghos 感谢,我试试
    Yohann97
        12
    Yohann97  
    OP
       2018-10-15 00:32:18 +08:00
    还是不行😭😭😭有大佬能在线指导下我么,好崩溃呀
    Alexliu
        13
    Alexliu  
       2018-10-15 09:51:23 +08:00
    不能直接前端直传么,还到后端倒手一次
    Yohann97
        14
    Yohann97  
    OP
       2018-10-15 17:59:30 +08:00
    @Alexliu 别人写好的代码我接手的,客户要求换文件上传地址,我写到前端得全改一遍,所以想写一个名字参数列表和返回参数一样的方法算了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2683 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 11:57 · PVG 19:57 · LAX 03:57 · JFK 06:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.