0
我需要將jpeg圖像發送(上傳)到Servlet,而不是將其保存在文件上,我想將其轉換爲BufferedImage並對其執行一些處理。將jpeg圖像發送到Servlet並將其轉換爲BufferedImage
這是我的客戶端代碼:
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost("http://localhost:9000/upload");
File file = new File("/tmp/lena.jpg");
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();
我怎麼能接受一個Servlet和處理它的形象呢?
我想這對我的接收端的Servlet,但圖像是空:
InputStream is = request.getInputStream();
BufferedImage bImageFromConvert = ImageIO.read(is);
最後,我不應該保存在磁盤上的任何東西的過程中。
的答案是正確的,但如果你想使用的Servlet 3.0的HttpServletRequest#的getParts()方法,那麼你必須註解與@MultipartConfig你的servlet。 之後,事情奏效 – Hossein