步骤
1.开启一个子线程
2.OutputStream outputStream=null;
3.写一个try
4.
try {
//手机上的文件
File file1=new File("/storage/emulated/0/Download/plus_logo_web.png");
File file2=new File("/storage/emulated/0/Download/plus_logo_web.png");
File file3=new File("/storage/emulated/0/Download/plus_logo_web.png");
//这里是文件特性
String fileKey="files";
String fileType="image/jpeg";
String BOUNDARY="--------------------------010040537206838198688029";
//这里是我们的服务器,根据服务器的来决定我们怎么使用
URL url=new URL("http://10.0.2.2:9102"+"/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);
outputStream = httpURLConnection.getOutputStream();
//连接
httpURLConnection.connect();
//这里我们写一个方法,用于上传文件
private void uploadFile(File file, String fileName, String fileKey, String fileType, String BOUNDARY, OutputStream outputStream,
boolean isLast) throws IOException {
/*
准备数据
*/
// 头部数据(描述性文件)
StringBuilder headerSbInfo=new StringBuilder();
headerSbInfo.append("--");
headerSbInfo.append(BOUNDARY);
headerSbInfo.append("\r\n");//\r\n可以在window 和其他
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");
byte[] footerSBInfoBytes = footerSBInfo.toString().getBytes("UTF-8");
outputStream.write(footerSBInfoBytes);
}
5.这里是调用编写的方法,并获取返回结果
uploadFile(file1, file1.getName(), fileKey, fileType, BOUNDARY, outputStream,false);
uploadFile(file2, file2.getName(), fileKey, fileType, BOUNDARY, outputStream,false);
uploadFile(file3, file3.getName(), fileKey, 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();
}
}
}