我正在尝试使用 android 手机将图像上传到外部 MS SQL 数据库。我正在使用 Java HttpClient 将字节数组发送到 Web 服务器或网页。我不知道我应该如何处理这个问题。该网页应位于 ASP.net 中。我对 ASP.Net 还很陌生。我对如何使用 ASP.Net 读取字节数组进行了深入研究,但仍然没有答案。我希望我的网页或服务器读取字节并将它们存储到数据库中。
下面是我想用来发送字节的 Java 函数(尚未测试,因为我还没有读取字节的方法)。但我不知道如何在网站上阅读它们。任何建议,将不胜感激。如果你们发现我做错了什么,如果你们让我知道并告诉我应该如何解决它,我将不胜感激。请具体说明,因为我对此很陌生,并且对网页不太了解。谢谢。
private void sendImagesToServer() throws Exception
{
ImageItem image;
HttpURLConnection conn = null;
ImageIterator iterator;
DataOutputStream dos;
byte[] byteArray;
iterator = new ImageIterator(imageAdapter);
String uploadUrl;
while(iterator.hasNext())
{
image = iterator.getNext();
Uri uri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(image.id));
Bitmap bmp=BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byteArray = stream.toByteArray();
uploadUrl = "http://localhost:63776/SQLScript.aspx";
// Send request
try {
// Configure connection
URL url = new URL(uploadUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("PUT");
conn.setRequestProperty("Connection", "Keep-Alive");
dos = new DataOutputStream(conn.getOutputStream());
dos.write(byteArray, 0, byteArray.length);
dos.flush();
dos.close();
// Read response
try {
if (conn.getResponseCode() == 200) {
Toast.makeText(this,"Yay, We Got 200 as Response Code",Toast.LENGTH_SHORT).show();
}
} catch (IOException ioex) {
ioex.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} finally{
}
}
}