Android program development uses HttpURLConnection to upload files to the server

  • 2021-01-19 22:24:32
  • OfStack

1: Principle of implementation

Recently doing Android client application development, involve the images uploaded to the backend server, you chose to do Spring3 MVC HTTP API as a background to upload interface, android HttpURLConnection I chose to use to client by form data file upload function, wanted to copy online search under 1 change code, it found that there is no ready-made examples, most of the examples are based on HttpClient or based on Base64 after encoded as a string to transmit image data, So I have to begin, refer to the online 1 some information, finally realizes HttpURLConnection upload file based android client code, cut the crap, is actually based on HttpURLConnection implementation file upload the key is to be familiar with the Http agreement relevant knowledge, know Http MIME file blocks is in the format of the agreement, said the basic data transmission format is as follows:

boundary represents the boundary of form. As long as the number of bytes of content is written to the object output stream of HttpURLConnection in accordance with the format, Spring Controller on the server will automatically respond to accept, just like uploading a file on a browser page.

Server side HTTP API, I am based on Spring3 MVC implementation of Controller, the code is as follows:


@RequestMapping(value = "/uploadMyImage/{token}", method = RequestMethod.POST) 
public @ResponseBody String getUploadFile(HttpServletRequest request, HttpServletResponse response, 
@PathVariable String token) { 
logger.info("spring3 MVC upload file with Multipart form"); 
logger.info("servlet context path : " + request.getSession().getServletContext().getRealPath("/")); 
UserDto profileDto = userService.getUserByToken(token); 
String imgUUID = ""; 
try { 
if (request instanceof MultipartHttpServletRequest && profileDto.getToken() != null) { 
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; 
logger.info("spring3 MVC upload file with Multipart form"); 
// does not work, oh my god!! 
MultipartFile file = multipartRequest.getFiles("myfile").get(0); 
InputStream input = file.getInputStream(); 
long fileSize = file.getSize(); 
BufferedImage image = ImageIO.read(input); 
// create data transfer object 
ImageDto dto = new ImageDto(); 
dto.setCreateDate(new Date()); 
dto.setFileName(file.getOriginalFilename()); 
dto.setImage(image); 
dto.setCreator(profileDto.getUserName()); 
dto.setFileSize(fileSize); 
dto.setType(ImageAttachmentType.CLIENT_TYPE.getTitle()); 
dto.setUuid(UUID.randomUUID().toString()); 
/// save to DB 
imgUUID = imageService.createImage(dto); 
input.close(); 
} 
} catch (Exception e) { 
e.printStackTrace(); 
logger.error("upload image error", e); 
} 
return imgUUID; 
}

Android client based on HttpURLConnection upload code, I put it encapsulated into a single class file, so we can directly use, as long as the upload parameters such as URL can be passed. The code is as follows:


package com.demo.http; 
import java.io.BufferedInputStream; 
import java.io.DataOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.util.Random; 
import android.os.Handler; 
import android.util.Base64; 
import android.util.Log; 
public class UploadImageTask implements APIURLConstants { 
private String requestURL = DOMAIN_ADDRESS + UPLOAD_DESIGN_IMAGE_URL; // default 
private final String CRLF = "\r\n"; 
private Handler handler; 
private String token; 
public UploadImageTask(String token, Handler handler) { 
this.handler = handler; 
this.token = token; 
} 
public String execute(File...files) { 
InputStream inputStream = null; 
HttpURLConnection urlConnection = null; 
FileInputStream fileInput = null; 
DataOutputStream requestStream = null; 
handler.sendEmptyMessage(50); 
try { 
// open connection 
URL url = new URL(requestURL.replace("{token}", this.token)); 
urlConnection = (HttpURLConnection) url.openConnection(); 
// create random boundary 
Random random = new Random(); 
byte[] randomBytes = new byte[16]; 
random.nextBytes(randomBytes); 
String boundary = Base64.encodeToString(randomBytes, Base64.NO_WRAP); 
/* for POST request */ 
urlConnection.setDoOutput(true); 
urlConnection.setDoInput(true); 
urlConnection.setUseCaches(false); 
urlConnection.setRequestMethod("POST"); 
long size = (files[0].length() / 1024); 
if(size >= 1000) { 
handler.sendEmptyMessage(-150); 
return "error"; 
} 
//  build Entity form 
urlConnection.setRequestProperty("Connection", "Keep-Alive"); 
urlConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); 
urlConnection.setRequestProperty("Cache-Control", "no-cache"); 
// never try to chunked mode, you need to set a lot of things 
// if(size > 400) { 
// urlConnection.setChunkedStreamingMode(0); 
// } 
// else { 
// urlConnection.setFixedLengthStreamingMode((int)files[0].length()); 
// } 
// end comment by zhigang on 2016-01-19 
/* upload file stream */ 
fileInput = new FileInputStream(files[0]); 
requestStream = new DataOutputStream(urlConnection.getOutputStream()); 
String nikeName = "myfile"; 
requestStream = new DataOutputStream(urlConnection.getOutputStream()); 
requestStream.writeBytes("--" + boundary + CRLF); 
requestStream.writeBytes("Content-Disposition: form-data; name=\"" + nikeName + "\"; filename=\"" + files[0].getName() + "\""+ CRLF); 
requestStream.writeBytes("Content-Type: " + getMIMEType(files[0]) + CRLF); 
requestStream.writeBytes(CRLF); 
//  Write the image byte content  
int bytesRead; 
byte[] buffer = new byte[8192]; 
handler.sendEmptyMessage(50); 
while((bytesRead = fileInput.read(buffer)) != -1) { 
requestStream.write(buffer, 0, bytesRead); 
} 
requestStream.flush(); 
requestStream.writeBytes(CRLF); 
requestStream.flush(); 
requestStream.writeBytes("--" + boundary + "--" + CRLF); 
requestStream.flush(); 
fileInput.close(); 
// try to get response 
int statusCode = urlConnection.getResponseCode(); 
if (statusCode == 200) { 
inputStream = new BufferedInputStream(urlConnection.getInputStream()); 
String imageuuId = HttpUtil.convertInputStreamToString(inputStream); 
Log.i("image-uuid", "uploaded image uuid : " + imageuuId); 
handler.sendEmptyMessage(50); 
return imageuuId; 
} 
} catch (Exception e) { 
e.printStackTrace(); 
} finally { 
if (inputStream != null) { 
try { 
inputStream.close(); 
} catch (IOException e) { 
e.printStackTrace(); 
} 
} 
if(requestStream != null) { 
try { 
requestStream.close(); 
} catch (IOException e) { 
e.printStackTrace(); 
} 
} 
if(fileInput != null) { 
try { 
fileInput.close(); 
} catch (IOException e) { 
e.printStackTrace(); 
} 
} 
if (urlConnection != null) { 
urlConnection.disconnect(); 
} 
} 
handler.sendEmptyMessage(50); 
return null; 
} 
private String getMIMEType(File file) { 
String fileName = file.getName(); 
if(fileName.endsWith("png") || fileName.endsWith("PNG")) { 
return "image/png"; 
} 
else { 
return "image/jpg"; 
} 
} 
}

After my own test, the effect of the bar!! So forget about HttpClient, which is no longer needed for android development.


Related articles: