上传的方式
本文将介绍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.Part
或RequestBody
直接使用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();
}
}
}
};
}
}