如何图像数据的Android发送到服务器 [英] How to send image data to server from Android
问题描述
我想写一个Android应用程序,将拍摄一张照片,把数据(字节[])中的对象以及一些元数据,并张贴到AppEngine上的服务器在那里将得到的数据存储坚持为斑点。我真的不希望将图像保存为Android上的文件(除非它是绝对必要的)。我搜索周围的解决方案,但没有明确的或特定的足够的走了过来。我的问题是:
I am trying to write an Android application that will take a picture, put the data (byte[]) in an object along with some metadata, and post that to an AppEngine server where it will get persisted in a datastore as a blob. I don't really want to save the image as a file on Android (unless it's absolutely necessary). I searched around for solutions but nothing clear or specific enough came up. My questions are:
- 如何上传对象,以我的servlet?具体如何正确序列化对象,并得到序列化输出到HttpPost或类似的东西。
- 有一次我坚持在数据存储中的BLOB,我怎么就能检索它作为一个图像显示在网页上?
code的例子是非常有益的。另外,如果我走的方法过于复杂或有问题的,请建议其他方法(例如,保存图像文件,张贴到服务器,然后删除)。
Code examples would be very helpful. Also, if the approach I am taking is too complicated or problematic, please suggest other approaches (e.g. saving image as file, posting to server, then deleting).
推荐答案
您只需要做一个Http-的FileUpload这在POST的一个特例。 没有必要对uuen code中的文件。 无需使用特殊的lib /瓶 没有必要保存对象到磁盘(不管下面的例子是这样做的)
You just need to do a Http-FileUpload which in a special case of a POST. There is no need to uuencode the file. No need to use a special lib/jar No need to save the object to disk (regardless that the following example is doing so)
您找到的Http-指挥为您特别关注文件上传下
You find a very good explanation of Http-Command and as your special focus "file upload" under
<一个href="http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests">How使用java.net.URLConnection中的火灾和处理HTTP请求?
这是有文件上传示例如下(看送二进制文件) 并且可以添加一些同伴数据或者
The file upload sample from there follows (watch "send binary file") and it is possible to add some companion data either
String param = "value";
File textFile = new File("/path/to/file.txt");
File binaryFile = new File("/path/to/file.bin");
String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
String CRLF = "\r\n"; // Line separator required by multipart/form-data.
URLConnection connection = new URL(url).openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
PrintWriter writer = null;
try {
OutputStream output = connection.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(output, charset), true); // true = autoFlush, important!
// Send normal param.
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"param\"").append(CRLF);
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
writer.append(CRLF);
writer.append(param).append(CRLF).flush();
// Send text file.
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"textFile\"; filename=\"" + textFile.getName() + "\"").append(CRLF);
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
writer.append(CRLF).flush();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(textFile), charset));
for (String line; (line = reader.readLine()) != null;) {
writer.append(line).append(CRLF);
}
} finally {
if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {}
}
writer.flush();
// Send binary file.
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + binaryFile.getName() + "\"").append(CRLF);
writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName()).append(CRLF);
writer.append("Content-Transfer-Encoding: binary").append(CRLF);
writer.append(CRLF).flush();
InputStream input = null;
try {
input = new FileInputStream(binaryFile);
byte[] buffer = new byte[1024];
for (int length = 0; (length = input.read(buffer)) > 0;) {
output.write(buffer, 0, length);
}
output.flush(); // Important! Output cannot be closed. Close of writer will close output as well.
} finally {
if (input != null) try { input.close(); } catch (IOException logOrIgnore) {}
}
writer.append(CRLF).flush(); // CRLF is important! It indicates end of binary boundary.
// End of multipart/form-data.
writer.append("--" + boundary + "--").append(CRLF);
} finally {
if (writer != null) writer.close();
}
关于你问题的第二部分。在成功上传文件(我使用Apache共同文件),它不是一个大问题,提供一个blob为图像。
Regarding the second part of your question. When successful uploading the file (I use apache common files), it is not a big deal to deliver a blob as an image.
这是如何接受一个文件中的一个servlet
This is how to accept a file in a servlet
public void doPost(HttpServletRequest pRequest, HttpServletResponse pResponse)
throws ServletException, IOException {
ServletFileUpload upload = new ServletFileUpload();
try {
FileItemIterator iter = upload.getItemIterator (pRequest);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String fieldName = item.getFieldName();
InputStream stream = item.openStream();
....
stream.close();
}
...
这code提供了一个形象
And this code delivers an image
public void doGet (HttpServletRequest pRequest, HttpServletResponse pResponse) throws IOException {
try {
Blob img = (Blob) entity.getProperty(propImg);
pResponse.addHeader ("Content-Disposition", "attachment; filename=abc.png");
pResponse.addHeader ("Cache-Control", "max-age=120");
String enc = "image/png";
pResponse.setContentType (enc);
pResponse.setContentLength (img.getBytes().length);
OutputStream out = pResponse.getOutputStream ();
out.write (img.getBytes());
out.close();
我希望这code片段有助于回答您的问题
I hope this code fragments help to answer your questions
这篇关于如何图像数据的Android发送到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!