私はこれをすべて探していますが、何も私のために働いていません。アンドロイドからjavaサーブレットに画像をアップロードして保存します。
私はアンドロイドアプリからJavaサーブレットに画像をアップロードし、サーバーに保存しようとしています。 私が見つけたすべての解決策は私には役に立たなかった。
私のコードは、現在何!私は、ファイルが作成され、それを保存しようとしているときのAndroidアプリは、サーブレット、 に画像を送信しているが、それは空:(あなたの助けのための
おかげだ
アンドロイドクライアント(i_fileデバイス上のファイルの場所である)でマイコード:サーバ側の
public static void uploadPictureToServer(String i_file) throws ClientProtocolException, IOException {
// TODO Auto-generated method stub
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost("http://192.168.1.106:8084/Android_Server/GetPictureFromClient");
File file = new File(i_file);
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file, "image/jpeg");
mpEntity.addPart("userfile", cbFile);
httppost.setEntity(mpEntity);
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
System.out.println(response.getStatusLine());
if (resEntity != null) {
System.out.println(EntityUtils.toString(resEntity));
}
if (resEntity != null) {
resEntity.consumeContent();
}
httpclient.getConnectionManager().shutdown();
}
マイコード:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
InputStream in = request.getInputStream();
OutputStream out = new FileOutputStream("C:\\myfile.jpg");
IOUtils.copy(in, out); //The function is below
out.flush();
out.close();
}
IOUtils.copyコード:
public static long copy(InputStream input, OutputStream output) throws IOException {
byte[] buffer = new byte[4096];
long count = 0L;
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}
これはクライアントに関連するだけでなく、サーバー(サーブレット)にも実装する必要があります。 Btw: 'processRequest'は何をするのですか? – home
ありがとうございます。私はリンクの情報を読んだが、私の問題の解決方法を理解できないようだ。 – Ohadza
サーブレットのコードはどのように見えますか? 'processRequest'ではどうなりますか? – home