0

AndroidフォンからコンピューターのローカルWebサーバーに写真を送信します。画像をローカル サーバー上のフォルダーに保存したいと考えています。私の計画は、受信した画像を処理して保存するある種のコントローラーを作成することです。したがって、基本的には、パラメーター (画像) を受け取り、それをサーバーのフォルダーに保存するコントローラーを作成する必要があると思います。私はあちこち探し回っていますが、探しているものはまだ見つかりません。

したがって、私が知りたいのは、次のとおり
です。そのようなコントローラーはどのように作成されますか。



現在、STS を介して Apache Tomcat/7.0.39 Web サーバー、Spring MVC Framework を使用しており、OS は Windows 7 です。

私が得ることができるどんな助けにも感謝します!
コード例は大歓迎です。

ありがとう、
マット

4

3 に答える 3

0

Apache Commons FileUploadは、マルチパート フォームの投稿を処理するのに非常に使いやすいです。Spring MVC で使用したことはないと思いますが、はいくつかあります。

于 2013-05-17T12:46:25.073 に答える
0

同様の機能 (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);

それが役に立てば幸い。

于 2013-05-18T03:36:07.503 に答える