Android文件上传

上传的方式

本文将介绍2中文件上传的方式:
1.multipart/from-data方式上传。
2.binary方式上传。

multipart上传方式

html代码

这中上传方式是我们最常用的上传方式。比如我们使用网页上传文件,其中html代码大致为这样:

<form method="post" enctype="multipart/form-data" action="/upload/single">
    文件:<input name="file" type="file"> <br/>
    <input name="submit" type="submit" value="提交">
</form>

其中 enctype设置为multipart/form-data方式。如果是多文件的话,html代码应该是这样:

<form method="post" enctype="multipart/form-data" action="/upload/multi_file">
    <input name="file" type="file"><br>
    <input name="file" type="file"><br/>
    ...
    <input name="submit" type="submit" value="提交">
</form>

input标签中的name就是对应的字段,后台程序将根据这字段取出文件。

具体的报文

这种方式对应的HTTP报文如下:

POST /upload/binary HTTP/1.1
Host: 192.168.10.63:8080
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
Cache-Control: no-cache


------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename=""
Content-Type: image/jpeg
二进制文件信息

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename=""
Content-Type: 
二进制文件信息

------WebKitFormBoundary7MA4YWxkTrZu0gW--

通过上面的报文我们看以看出:multipart/form-data方式的一个重要组成部分请求头Content-Type必须为:
Content-Type:multipart/form-data;boundarty=一个32字节的随机数,用来分割每个part
然后每个Part之间用“双横杠”加bundary来分割,最后一个Part分割符末尾也要加“双横杠”

每个Part中必须包含Content-Disposition字段来注明字段文件名等信息,也可以包含Content-Type来说明文件的MeidaType。

Java服务端接受代码

/**
 * 单个文件上传。
 * <p>
 * 字段名为 file。
 *
 * @param file 文件
 * @return json
 */
@RequestMapping(method = RequestMethod.POST, value = "/single")
public UpLoadResponse singleReceive(@RequestParam("file") MultipartFile file) {
    List<PartInfo> partInfos = new ArrayList<PartInfo>();
    PartInfo partInfo = new PartInfo();
    partInfo.setMediaType(file.getContentType());
    partInfo.setSize(file.getSize());
    partInfos.add(partInfo);

    FileUtil.saveFile(file);

    System.out.println("接受到文件====" + file.getOriginalFilename());
    UpLoadResponse upLoadResponse = new UpLoadResponse();
    upLoadResponse.setPartInfos(partInfos);
    upLoadResponse.setMsg("上传成功");
    return upLoadResponse;
}

/**
 * 多文件上传,公用一个字段 "file"
 *
 * @param files 文件
 * @return json
 */
@RequestMapping(method = RequestMethod.POST, value = "/multi_file")
public UpLoadResponse multiFileReceive(@RequestParam("file") MultipartFile[] files) {
    List<PartInfo> partInfos = new ArrayList<PartInfo>();

    for (MultipartFile file : files) {
        PartInfo partInfo = new PartInfo();
        partInfo.setSize(file.getSize());
        partInfo.setMediaType(file.getContentType());
        partInfos.add(partInfo);

        FileUtil.saveFile(file);

        System.out.println("接受到文件====" + file.getOriginalFilename());
    }
    UpLoadResponse upLoadResponse = new UpLoadResponse();
    upLoadResponse.setPartInfos(partInfos);
    upLoadResponse.setMsg("上传成功");
    return upLoadResponse;
}

/**
 * 文件+文本一起上传,其实和多文件上传一样的。
 * <p>
 * 字段名为 file、text
 *
 * @param file 文件
 * @param text 文本
 * @return json
 */
@RequestMapping(method = RequestMethod.POST, value = "/multi")
public UpLoadResponse multiReceive(@RequestParam("file") MultipartFile file,
                                   @RequestParam("text") String text) {


    List<PartInfo> partInfos = new ArrayList<PartInfo>();

    PartInfo partInfo = new PartInfo();
    partInfo.setMediaType(file.getContentType());
    partInfos.add(partInfo);
    partInfo.setSize(file.getSize());
    // 保存文件
    FileUtil.saveFile(file);

    System.out.println("接受到文件====" + file.getOriginalFilename());

    PartInfo partInfo1 = new PartInfo();
    partInfo.setText(text);
    partInfo.setSize(text.length());
    partInfos.add(partInfo1);

    System.out.println("接受到文本====" + text);

    UpLoadResponse upLoadResponse = new UpLoadResponse();
    upLoadResponse.setPartInfos(partInfos);
    upLoadResponse.setMsg("提交成功");

    return upLoadResponse;
}

上面的代码演示了,Java服务端通过@RequestParam("file") MultipartFile file就可以获得文件信息了,如果客户端传的Part类型为String,也可以直接用String类型获取文本信息。

客户端使用Retrofit上传

客户端这边使用Retrofit上传文件可以有2中方式,一种是使用Multipart.Part上传,另一种直接使用RequestBody上传。需要注意的是如果使用RequestBody上传的时候,我们需要在@Part注解中将 字段名和文件名(filename)拼接出来,如果不拼filename的话,服务端则会报错
如果是上传文本信息的话,可以不用拼接“filename" 也可以不用Multipart.PartRequestBody直接使用String类型就行。

例如:

package com.blueberry.multipart.api;

import com.blueberry.multipart.entity.UpLoadResponse;

import java.util.HashMap;
import java.util.List;

import io.reactivex.Observable;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.Part;
import retrofit2.http.PartMap;

/**
 * Created by blueberry on 7/6/2017.
 * <p>
 * 如果使用Multipart.Part需要注意 @Part直接中不要有参数。
 * 如果使用RequestBody需要注意:如果上传文件name应该包含有filename(文件名)这个字段,负责后台可能会出错,
 * 比如我们要上传一个文件我们的name值应该为:file";filename="image.jpg;注意前后没有双引号,中间有2个双引号,
 * 这是因为Retrofit会自动帮我们拼接Content-Disposition;它拼接的方式为 form-data; name="我们设置的值",如
 * 果用MultipartBody.Part我们则不需要这么费事的拼接,因为Multipart.Part.createFormData()放我们完成了该操
 * 作;
 * {@link okhttp3.MultipartBody.Part#createFormData(String, String, RequestBody)}
 */

public interface MultipartApi {

    /**
     * 单个文件上传
     */
    String SINGLE = "upload/single";

    /**
     * 多个文件上传(使用同一个字段名)
     */
    String MULTI_FILE = "upload/multi_file";

    /**
     * 文件+文本一起上传
     */
    String MULTI = "upload/multi";


    /**
     * 单个文件上传,使用MultipartBody.Part。
     *
     * @param part
     * @return
     */
    @Multipart
    @POST(SINGLE)
    Observable<UpLoadResponse> singlePart(@Part MultipartBody.Part part);

    /**
     * 单个文件上传,使用RequestBody。
     *
     * @param body
     * @return
     */
    @Multipart
    @POST(SINGLE)
    Observable<UpLoadResponse> singleRequestBody(@Part("file\";filename=\"image.jpg") RequestBody body);


    /**
     * 多文件上传使用 List<MultipartBody.Part>。
     *
     * @param parts
     * @return
     */
    @Multipart
    @POST(MULTI_FILE)
    Observable<UpLoadResponse> multiFilePart(@Part List<MultipartBody.Part> parts);

    /**
     * 多文件上传使用 HashMap<String, RequestBody> map。
     *
     * @param map
     * @return
     */
    @Multipart
    @POST(MULTI_FILE)
    Observable<UpLoadResponse> multiFileRequestBody(@PartMap HashMap<String, RequestBody> map);


    /**
     * 文件+文本上传。文件使用 RequestBody
     *
     * @param body
     * @param string
     * @return
     */
    @Multipart
    @POST(MULTI)
    Observable<UpLoadResponse> multiRequestBody(@Part("file\";filename=\"image.jpg") RequestBody body,
                                                @Part("text") String string);

    /**
     * 文件+文本上传。文件使用 MultipartBody.Part上传
     *
     * @param part
     * @param string
     * @return
     */
    @Multipart
    @POST(MULTI)
    Observable<UpLoadResponse> multiPart(@Part MultipartBody.Part part,
                                         @Part("text") String string);

}

上面演示了好几种方式提交文件,包含:
1.单个文件上传,使用RequestBody,和使用MultiBody.Part方式
2.多个文件上传(part都使用同一个字段),RequestBody和MulipartBody.Part的写法。
3.文件+文本上传,RequestBody和MulipartBody.Part方式的写法。

使用MulipartBody.Part具体的实现:


private MultipartApi mService;

private MultipartRepositoryPartImpl() {
    mService = RetrofitHelper
            .getInstance()
            .getRetrofit()
            .create(MultipartApi.class);
}

@Override
public void singleFileUpload(File file, Observer<UpLoadResponse> observer) {
    mService.singlePart(MultipartBody.Part
            .createFormData("file", "temp.jpg",
                    RequestBody.create(MediaType.parse("image/jpg"), file)))
            .compose(TransformUtil.<UpLoadResponse>applySchedulerTransformer())
            .subscribe(observer);
}

使用RequestBody的具体实现:

@Override
public void singleFileUpload(File file, Observer<UpLoadResponse> observer) {
    mService.singleRequestBody(RequestBody.create(MediaType.parse("image/jpg"), file))
            .compose(TransformUtil.<UpLoadResponse>applySchedulerTransformer())
            .subscribe(observer);
}

多文件上传,RequestBodyt方式的实现:

@Override
public void multiFileUpload(final File[] files, Observer<UpLoadResponse> observer) {
    mService.multiFileRequestBody(new HashMap<String, RequestBody>() {
        {
            for (File file : files) {

                put("file\";filename=\"" + file.getName(),
                        RequestBody.create(MediaType.parse("image/jpg"), file));
            }
        }
    }).compose(TransformUtil.<UpLoadResponse>applySchedulerTransformer())
            .subscribe(observer);
}

多文件上传,MultipartBody.Part方式上传

@Override
public void multiFileUpload(final File[] files, Observer<UpLoadResponse> observer) {
    mService.multiFilePart(new ArrayList<MultipartBody.Part>() {
        {
            for (File file : files) {
                add(MultipartBody.Part.createFormData("file", file.getName(),
                        RequestBody.create(MediaType.parse("image/jpg"), file)));
            }
        }
    }).compose(TransformUtil.<UpLoadResponse>applySchedulerTransformer())
            .subscribe(observer);
}

文件+文本RequestBody实现


@Override
public void multiUpload(File file, String text, Observer<UpLoadResponse> observer) {
    mService.multiRequestBody(RequestBody.create(MediaType.parse("image/jpg"), file), text)
            .compose(TransformUtil.<UpLoadResponse>applySchedulerTransformer())
            .subscribe(observer);
}

文件+文本MultiaprtBody.Part实现

@Override
public void multiUpload(File file, String text, Observer<UpLoadResponse> observer) {
    mService.multiPart(MultipartBody.Part.createFormData("file", file.getName(), RequestBody
            .create(MediaType.parse("image/jpg"), file)), text)
            .compose(TransformUtil.<UpLoadResponse>applySchedulerTransformer())
            .subscribe(observer);
}

Binary方式上传

Binary上传方式,就是整个请求体就是二进制数据!! 就是这么粗暴!

报文形式

POST /upload/binary HTTP/1.1
Host: 192.168.10.63:8080
Content-Type: xxx
Cache-Control: no-cache


二进制数据

Java服务端接收

后台接受的话,直接拿到HttpServeletRequest#inputStream读数据就好了!

当然,如果用户想传入文件信息,也可以通过请求头来传递。

/**
 * 二进制上传。
 *
 * @param request
 * @return
 */
@RequestMapping(method = RequestMethod.POST, value = "/binary")
public UpLoadResponse binaryReceive(HttpServletRequest request) {

    String contentType = request.getHeader("Content-Type");

    String size = request.getHeader("Content-Length");
    try {
        InputStream in = request.getInputStream();
        FileUtil.saveInputStream(in);
    } catch (IOException e) {
        e.printStackTrace();
    }


    List<PartInfo> partInfos = new ArrayList<PartInfo>();

    PartInfo partInfo = new PartInfo();
    partInfo.setMediaType(contentType + "");
    partInfo.setSize(Integer.parseInt(size));
    partInfos.add(partInfo);
    UpLoadResponse upLoadResponse = new UpLoadResponse();
    upLoadResponse.setPartInfos(partInfos);
    upLoadResponse.setMsg("二进制上传成功");
    return upLoadResponse;
}
``

### 客户端代码

```java

public interface BinaryApi {

    /**
     * binary方式上传,这种方式整个请求体直接就是二进制数据。服务端只需要拿到request#iputsteam就可以获得。
     *
     * @param body
     * @return
     */
    @POST("upload/binary")
    Observable<UpLoadResponse> binary(@Body RequestBody body);
}

实现代码:

public void uploadBinary(File file, Observer<UpLoadResponse> observer){
    RetrofitHelper.getInstance()
            .getRetrofit()
            .create(BinaryApi.class)
            .binary(RequestBody.create(MediaType.parse("image/jpg"),file))
            .compose(TransformUtil.<UpLoadResponse>applySchedulerTransformer())
            .subscribe(observer);
}

我们也可以直接传流,例如:

public void uploadBinary(InputStream input, Observer<UpLoadResponse> observer){
    RetrofitHelper.getInstance()
            .getRetrofit()
            .create(BinaryApi.class)
            .binary(RequestBodyUtil.create(MediaType.parse("image/jpg"),input))
            .compose(TransformUtil.<UpLoadResponse>applySchedulerTransformer())
            .subscribe(observer);
}

RequestBodyUtil.java

public class RequestBodyUtil {

    public static RequestBody create(final MediaType mediaType, final InputStream inputStream) {
        return new RequestBody() {
            @Override
            public MediaType contentType() {
                return mediaType;
            }

            @Override
            public long contentLength() throws IOException {
                return inputStream.available();
            }

            @Override
            public void writeTo(BufferedSink sink) throws IOException {
                Source source=null;
                try {
                    source = Okio.source(inputStream);
                    sink.writeAll(source);
                }finally {
                    if(source!=null){
                        source.close();
                    }
                }
            }
        };
    }
}

完整代码

https://github.com/blueberryCoder/MultipartExample

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,496评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,407评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,632评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,180评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,198评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,165评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,052评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,910评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,324评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,542评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,711评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,424评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,017评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,668评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,823评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,722评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,611评论 2 353

推荐阅读更多精彩内容

  • 主题 记录安卓端上传模块优化的经历。通过本次分享,咱们可以知道 一个文件经历了几个步骤才能从手机上传到服务端 能知...
    紫阚阅读 4,428评论 1 10
  • 文件上传与下载 文件上传 -- 服务端 以Tomcat为服务器,Android客服端访问Servlet,经Serv...
    sunhaiyu阅读 14,153评论 2 20
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,077评论 25 707
  • 文件上传在B/S应用中是一种十分常见的功能,那么在Android平台下是否可以实现像B/S那样的文件上传功能呢?答...
    0dce86ba3565阅读 513评论 0 1
  • 真實,是人生的最高境界。什麼是真實?就是不撒謊、不做作、不違背良心,純乎心性而行。我一個90後,大學畢業後...
    美梅阅读 202评论 0 1