Android的图片上传问题 [英] Android image Upload problem
问题描述
您好,我想从Android模拟器到asp.net服务器上传图片。在code以下可以通讯的服务器。当我试图创建一个文本文件,看看从机器人发送的数据是成功还是失败。但是,没有文件数据没有发送跨越到服务器。我试图发送纯文本到服务器,但我在服务器上创建没有打印的文本文件。
Hello i am wanting to upload image from android emulator to asp.net server. The code below can communicate the server. When I tried to create a text file to see the data sent from android was successful or not . But no the file data didn't send across to the server. I tried sending the plain text to the server but the file I created on the server didn't print the text.
在code在这里: HttpURLConnection的康恩= NULL;
The code here: HttpURLConnection conn = null;
String boundary = "==============";
try
{
String disposition = "Content-Disposition: form-data; name=\"userfile\"; filename=\"" + filename + ".jpg\"";
String contentType = "Content-Type: application/octet-stream";
String t1 = "Content-Disposition: form-data; name=\"test\";";
String t2 = "Content-Type: text/plain";
// This is the standard format for a multipart request
StringBuffer requestBody = new StringBuffer();
/*
requestBody.append("--"+boundary);
requestBody.append('\n');
requestBody.append(disposition);
requestBody.append('\n');
requestBody.append(contentType);
requestBody.append('\n');
requestBody.append('\n');
requestBody.append(new String(getByteFromStream(stream)));
*/
requestBody.append('\n');
requestBody.append('\n');
requestBody.append("--"+boundary);
requestBody.append('\n');
requestBody.append(t1);
requestBody.append('\n');
requestBody.append(t2);
requestBody.append('\n');
requestBody.append('\n');
requestBody.append("basdfsdafsadfsad");
requestBody.append("--"+boundary+"--");
// Make a connect to the server
URL url = new URL(targetURL);
conn = (HttpURLConnection) url.openConnection();
// Put the authentication details in the request
/*
if (username != null) {
String usernamePassword = username + ":" + password;
String encodedUsernamePassword = Base64.encodeBytes(usernamePassword.getBytes());
conn.setRequestProperty ("Authorization", "Basic " + encodedUsernamePassword);
}
*/
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("MIME-Version:", "1.0");
conn.setRequestProperty("Content-Type", "multipart/mixed; boundary=" + boundary);
// Send the body
DataOutputStream dataOS = new DataOutputStream(conn.getOutputStream());
dataOS.writeBytes(requestBody.toString());
dataOS.flush();
dataOS.close();
// Ensure we got the HTTP 200 response code
int responseCode = conn.getResponseCode();
if (responseCode != 200) {
throw new Exception(String.format("Received the response code %d from the URL %s", responseCode, url));
}
是我的请求主体布局不正确?
Is my request body layout not correctly ?
推荐答案
我使用asp.net的作为文件处理程序。下面是一个简单的Android $ C $下,你会用它来上传文件时
I am using asp.net for as file handler. Below is the simple Android code for the event you will use to upload the file
String pathToOurFile = "/mnt/sdcard/sysdroid.png";//this will be the file path String urlServer = "http://yourdomain/fileupload.aspx";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
try
{
FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile) );
URL url = new URL(urlServer);
connection = (HttpURLConnection) url.openConnection();
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// Enable POST method
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data");
connection.setRequestProperty("SD-FileName", "sysdroid.png");//This will be the file name
outputStream = new DataOutputStream( connection.getOutputStream() );
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
Log.d("ServerCode",""+serverResponseCode);
Log.d("serverResponseMessage",""+serverResponseMessage);
fileInputStream.close();
outputStream.flush();
outputStream.close();
}
catch (Exception ex)
{
//ex.printStackTrace();
Log.e("Error: ", ex.getMessage());
}
到目前为止好。让我们看看到asp.net code。我用简单的Web窗体这一点。在code的背后,是
So far so good. Lets look into the asp.net code. I used simple 'Web Form' for this. The code behind is
protected void Page_Load(object sender, EventArgs e)
{
string uploadDir = Server.MapPath("~/images");
string imgPath = Path.Combine(uploadDir, Request.Headers["SD-FileName"]);
try{
byte[]bytes = new byte[Request.InputStream.Length];
Request.InputStream.Read(bytes, 0, bytes.Length);
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes);
Bitmap btMap = (Bitmap)System.Drawing.Image.FromStream(ms);
btMap.Save(imgPath, ImageFormat.Jpeg);
ms.Close();
}
catch (Exception exp)
{
Response.Write(exp.Message);
}
}
希望这会工作,你有读/写权限在两个Android手机的SD卡和asp.net文件夹的知识。
Hope this will work and you have the knowledge of read/write permissions on both Android's SD card and asp.net folders.
欢呼 Fahar
这篇关于Android的图片上传问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!