problem in uploading image to php server from android
hi all i am trying to upload image file from sdcard to php server in android. i use the following code for uploading , it does not show exception but the image is not uploaded in server. i do not know what is the problem. please assist me. i attached my code and Logcat information. code:
public class UploadImage extends Activity {
InputStream is;
private int serverResponseCode;
private String serverResponseMessage;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
String pathToOurFile = "//sdcard//chsevtoneta.png";
String urlServer = "http://xxxxxxxxxxxxxxxxxxxxxxxx/upload.php";
String lineEnd = "\r\n";
String开发者_如何学Go twoHyphens = "--";
String boundary = "*****";
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;boundary="+boundary);
outputStream = new DataOutputStream( connection.getOutputStream() );
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
outputStream.writeBytes(lineEnd);
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);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
serverResponseCode = connection.getResponseCode();
serverResponseMessage = connection.getResponseMessage();
Log.e("response",""+serverResponseCode);
Log.e("serverResponseMessage",""+serverResponseMessage);
fileInputStream.close();
outputStream.flush();
outputStream.close();
}
catch (Exception ex)
{
//Exception handling
}
}
}
and Logcat information:
05-10 11:04:59.762: ERROR/response(2203): 200
05-10 11:04:59.762: ERROR/serverResponseMessage(2203): OK
The remote servers answers with a success (200) message. I think the error may be in the server side, but you don't provide any code for this part.
Looking at your source it appears that you are building a multipart/form upload by hand: this can easily be error-prone, adding a line break or an extra space in the wrong place would be very difficult to spot by eye but could easily confuse the server (depending on the strictness of the HTTP parser in use).
I would recommend using Android's built-in HTTP components pieces to do it: an example is at http://indiwiz.com/2009/02/11/multi-part-content-upload-in-apache-http-components-http-client/.
精彩评论