可以将图像作为 ByteBuffer 发送到我的 Servlet 吗?
我正在尝试让我的 Android 手机连接到我的 servlet 并向其发送某个图像。我认为这样做的方法是使用 copyPixelsToBuffer() 函数,然后尝试通过某个输出流将其发送到 servlet(类似于我在正常独立的情况下执行此操作的方式) java 应用程序)。这种方式行得通吗?如果是这样,我到底使用哪种流?我是否应该只使用 DataOutputStream 并执行如下操作:
ByteBuffer imgbuff;
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
bm.copyPixelsToBuffer(bm);
...code...
URLConnection sc = server.openConnection();
sc.setDoOutput(true);
DataOutputStream out = new DataOutputStream( sc.getOutputStream() );
out.write(imgbuff.array());
out.flush();
out.close();
注意:我知道这可能不是使用 Android 操作系统连接到服务器的正确方法,但目前我正在研究如何发送图像,不是连接(除非这与图像的发送方式相关)。
如果这不是您建议将图像发送到 servlet 的方法(我认为字节缓冲区是最好的,但我可能是错的),您会建议如何完成此操作?
I am attempting to have my android phone connect to my servlet and send it a certain image. The way I figured I would do this, is to use the copyPixelsToBuffer()
function and then attempt to send this to the servlet through some output stream(similar to how I would do it in a normal stand alone java application). Will this way work? If so, what kind of stream do I use exactly? Should I just use DataOutputStream and just do something like the following:
ByteBuffer imgbuff;
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
bm.copyPixelsToBuffer(bm);
...code...
URLConnection sc = server.openConnection();
sc.setDoOutput(true);
DataOutputStream out = new DataOutputStream( sc.getOutputStream() );
out.write(imgbuff.array());
out.flush();
out.close();
Note: I understand that this may not be the proper way of connecting to a server using the Android OS but at the moment I'm working on just how to send the image, not the connection (unless this is relevant on how the image is sent).
If this is not a way you'd recommend sending the image to the servlet (I figured a byte buffer would be best but I could be wrong), how would you recommend this to be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于
HttpServlet
通常侦听 HTTP 请求,因此您希望使用multipart/form-data
编码通过 HTTP 发送二进制数据,而不是像这样的原始(未格式化)数据。从客户端开始,您可以使用
URLConnection
来实现此目的,如 迷你教程,但它会非常冗长。您还可以使用 Apache HttpComponents Client 来实现此目的。但这会增加额外的依赖项,我不确定您是否希望在 Android 上使用它。然后,在服务器端,您可以使用 Apache Commons FileUpload 解析 < code>multipart/form-data 编码的请求正文。您可以在此答案中找到代码示例,
servlet 的 doPost()
应该是这样的。至于您的代码示例:包装在
DataOutputStream
是不必要的。您没有利用DataOutputStream
的功能。您只需使用write(byte[])
方法,该方法已由URLConnection#getOutputStream()
返回的基本OutputStream
提供。此外,Bitmap
有一个 < a href="http://developer.android.com/reference/android/graphics/Bitmap.html#compress%28android.graphics.Bitmap.compressFormat,%20int,%20java.io.OutputStream%29" rel="nofollow noreferrer">compress()
方法,您可以使用该方法使用更标准且更易于理解的格式(PNG、JPG 等)将其压缩为任意OutputStream
。例如,执行此操作,而不是像代码中那样使用
output.write(bytes)
。Since a
HttpServlet
normally listens on HTTP requests, you'd like to usemultipart/form-data
encoding to send binary data over HTTP, instead of raw (unformatted) like that.From the client side on, you can use
URLConnection
for this as outlined in this mini tutorial, but it's going to be pretty verbose. You can also use Apache HttpComponents Client for this. This adds however extra dependencies, I am not sure if you'd like to have that on Android.Then, on the server side, you can use Apache Commons FileUpload to parse the items out of a
multipart/form-data
encoded request body. You can find a code example in this answer how thedoPost()
of the servlet should look like.As to your code example: wrapping in the
DataOutputStream
is unnecessary. You aren't taking benefit of theDataOutputStream
's facilities. You are just usingwrite(byte[])
method which is already provided by the basicOutputStream
as returned byURLConnection#getOutputStream()
. Further, theBitmap
has acompress()
method which you can use to compress it using a more standard and understandable format (PNG, JPG, etc) into an arbitraryOutputStream
. E.g.Do this instead of
output.write(bytes)
as in your code.