同様の機能 (Android からサーブレットへの写真の読み込み) については、私が使用する Android クライアント コードを次に示します (ここに投稿するために少し編集しています)。
URI uri = URI.create(// path to file);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.STRICT);
// several key-value pairs to describe the data, one should be filename
entity.addPart("key", new StringBody("value"));
File inputFile = new File(photoUri.getPath());
// optionally reduces the size of the photo (you can replace with FileInputStream)
InputStream photoInput = getSizedPhotoInputStream(photoUri);
entity.addPart("CONTENT", new InputStreamBody(photoInput, inputFile.getName()));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(uri);
HttpContext localContext = new BasicHttpContext();
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost, localContext);
これを受け取るコードは次のとおりです。まず、サーブレット クラスにマルチパート メッセージをサポートするタグを付けてください。
@MultipartConfig
public class PhotosServlet extends HttpServlet
次に、本文の関連部分:
HttpEntity entity = new InputStreamEntity(request.getPart("CONTENT").getInputStream(), contentLength);
InputStream inputFile = entity.getContent();
// string extension comes from one of the key-value pairs
String extension = request.getParameter(//filename key);
// first write file to a file
File images = new File(getServletContext().getRealPath("images"));
File filePath = File.createTempFile("user", extension, images);
writeInputDataToOutputFile(inputFile, filePath); // just copy input stream to output stream
String path = filePath.getPath();
logger.debug("Wrote new file, filename: " + path);
それが役に立てば幸い。