Send it as a multipart / form-data request using MultipartEntity
the Android builtin class HttpClient API .
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://example.com/uploadservlet");
MultipartEntity entity = new MultipartEntity();
entity.addPart("fieldname", new InputStreamBody(fileContent, fileContentType, fileName));
httpPost.setEntity(entity);
HttpResponse servletResponse = httpClient.execute(httpPost);
And then in the servlet method doPost()
use Apache Commons FileUpload to extract the part.
try {
List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : items) {
if (item.getFieldName().equals("fieldname")) {
String fileName = FilenameUtils.getName(item.getName());
String fileContentType = item.getContentType();
InputStream fileContent = item.getInputStream();
}
}
} catch (FileUploadException e) {
throw new ServletException("Cannot parse multipart request.", e);
}
I do not want to use apache commons
Servlet 3.0, multipart/form-data
HttpServletRequest#getParts()
, multipart/form-data parser RFC2388. . . , . ? . commons-fileupload.jar
commons-io.jar
/WEB-INF/lib
. . .