【发布时间】:2011-09-12 17:06:48
【问题描述】:
我正在尝试通过 http 将照片发送到我的服务器。因此,我将图像转换为字节,然后将其作为名称值对发送。这是我这样做的代码。现在我的问题是服务器端,我如何从收到的字节字符串中重新创建和存储图像
我也在使用 java servlet
安卓上的代码
ByteArrayOutputStream baos = new ByteArrayOutputStream();
pinnedV.getPhoto().compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("photo",new String(b)));
HttpClient httpclient = new DefaultHttpClient();
HttpPost request = new HttpPost(URL);
try {
request.setEntity(new UrlEncodedFormEntity(params));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ResponseHandler<String> handler = new BasicResponseHandler();
try {
result = httpclient.execute(request, handler);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
服务器上的代码
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String fromClientphoto= request.getParameter("photo");
byte[] b = fromClientphoto.getBytes();
FileOutputStream fos = new FileOutputStream("D:\\img.png");
fos.write(b);
fos.close();
}
上面的代码写入了一个文件,但它不会作为图像打开。这也是byte[] b = fromClientphoto.getBytes(); 转换回与安卓手机相同字节的正确方法吗?有什么想法吗?
【问题讨论】:
-
有什么问题?你有代码来做这两个操作。
-
@Haphazard 基本上错误是我的 bim 对象为空 -
-
我对@987654324@一无所知,所以我怀疑我能帮上什么忙。
-
@Haphazard 但我是否以正确的方式发送图像,如果有不同的方式你能帮忙吗?
-
您能否确认是否有任何数据进入服务器?调试或打印出
b数组的大小。
标签: java android image servlets