Android开发-网络编程单(多)文件上传
拉大锯
发表于
2019-12-10 20:48
4058
网络编程
文件上传
多文件上传
安卓开发
android
本文为android开发基础课程网络编程系列文章,关于接口,权限的声明与配置,请看前面的文章。
课程地址:
接口地址
方法:post
/file/upload
参数file
多文件上传
/files/upload
方法post 参数 files
抓包
我们前面已经了解了http的请求格式,那么如果我们不知道如何上传文件,那怎么整呢?
用postMan上传一个文件抓个包不就知道了嘛!
尾部:
我们按照这个套路,去拼接内容,不就可以啦!对吧!
代码实现
private void uploadFile(File file,
String fileKey,
String fileName,
String fileType,
String BOUNDARY,
OutputStream outputStream,
boolean isLast) throws IOException {
//准备数据
StringBuilder headerSbInfo = new StringBuilder();
headerSbInfo.append("--");
headerSbInfo.append(BOUNDARY);
headerSbInfo.append("\r\n");
headerSbInfo.append("Content-Disposition: form-data; name=\"" + fileKey + "\"; filename=\"" + fileName + "\"");
headerSbInfo.append("\r\n");
headerSbInfo.append("Content-Type: " + fileType);
headerSbInfo.append("\r\n");
headerSbInfo.append("\r\n");
byte[] headerInfoBytes = headerSbInfo.toString().getBytes("UTF-8");
outputStream.write(headerInfoBytes);
//文件内容
FileInputStream fos = new FileInputStream(file);
BufferedInputStream bfi = new BufferedInputStream(fos);
byte[] buffer = new byte[1024];
int len;
while((len = bfi.read(buffer,0,buffer.length)) != -1) {
outputStream.write(buffer,0,len);
}
//写尾部信息
StringBuilder footerSbInfo = new StringBuilder();
footerSbInfo.append("\r\n");
footerSbInfo.append("--");
footerSbInfo.append(BOUNDARY);
if(isLast) {
footerSbInfo.append("--");
footerSbInfo.append("\r\n");
}
footerSbInfo.append("\r\n");
outputStream.write(footerSbInfo.toString().getBytes("UTF-8"));
}
如果这个不方便理解,就看视频吧。一次就能看懂了。
这里面跟格式有关系,单文件和多文件的都有了。
如果是多文件,其实我们是向body里多添加几个文件而已,格式一样的。
所以就有了这样的调用。
多文件上传调用
OutputStream outputStream = null;
try {
File fileOne = new File("/storage/emulated/0/Download/1.jpg");
File fileTwo = new File("/storage/emulated/0/Download/rBsADV2rEz-AIzSoAABi-6nfiqs456.png");
File fileThree = new File("/storage/emulated/0/Download/2.jpg");
File fileFour = new File("/storage/emulated/0/Download/rBPLFV2A8POASi1aAAE-PgvGzOo723.jpg");
String fileKey = "files";
String fileType = "image/jpeg";
String BOUNDARY = "--------------------------954555323792164398227139";
//String BOUNDARY = "----------------------------954555323792164398227139--";
//String BOUNDARY = "----------------------------954555323792164398227139";
URL url = new URL(BASE_URL + "/files/upload");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setConnectTimeout(10000);
httpURLConnection.setRequestProperty("User-Agent","Android/" + Build.VERSION.SDK_INT);
httpURLConnection.setRequestProperty("Accept","*/*");
httpURLConnection.setRequestProperty("Cache-Control","no-cache");
httpURLConnection.setRequestProperty("Content-Type","multipart/form-data; boundary=" + BOUNDARY);
httpURLConnection.setRequestProperty("Connection","keep-alive");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
//连接
httpURLConnection.connect();
outputStream = httpURLConnection.getOutputStream();
uploadFile(fileOne,fileKey,fileOne.getName(),fileType,BOUNDARY,outputStream,false);
uploadFile(fileTwo,fileKey,fileTwo.getName(),fileType,BOUNDARY,outputStream,false);
uploadFile(fileFour,fileKey,fileFour.getName(),fileType,BOUNDARY,outputStream,false);
uploadFile(fileThree,fileKey,fileThree.getName(),fileType,BOUNDARY,outputStream,true);
outputStream.flush();
//获取返回的结果
int responseCode = httpURLConnection.getResponseCode();
Log.d(TAG,"responseCode -- > " + responseCode);
if(responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
String result = bf.readLine();
Log.d(TAG,"result -- > " + result);
}
} catch(Exception e) {
e.printStackTrace();
} finally {
if(outputStream != null) {
try {
outputStream.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
}
okay啦,详细内容请看视频吧,感谢同学们的支持与关注