我制作了一个应该让我上传到服务器的应用程序,但它只适用于localhost。我使用的在线服务器是ueuo.com(免费)。什么可能解决这个问题?有趣的是,我得到的服务器响应代码是200。
这是应用程序:
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private static final String JPEG_FILE_PREFIX = "img";
private static final String JPEG_FILE_SUFFIX = ".jpg";
private static final String FOLDER_NAME = "/idms/";
private String imageFileName = "";
Button btnUpload;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
makeDirectory();
btnUpload = (Button) findViewById(R.id.button1);
btnUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss")
.format(new Date());
imageFileName = JPEG_FILE_PREFIX + timeStamp + JPEG_FILE_SUFFIX;
File file = new File(Environment.getExternalStorageDirectory()
.getPath() + FOLDER_NAME + imageFileName);
Uri imageUri = Uri.fromFile(file);
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, 0);
}
});
}
public void makeDirectory() {
File dir = new File(Environment.getExternalStorageDirectory()
+ FOLDER_NAME);
if (!dir.exists()) {
dir.mkdir();
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//String pathToOurFile = Environment.getExternalStorageDirectory()
.getPath() + FOLDER_NAME + imageFileName;
// Upload the image to the server
new Thread(new Runnable() {
public void run() {
uploadFile();
}
}).start();
}
protected void uploadFile() {
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
// DataInputStream inputStream = null;
String pathToOurFile = Environment.getExternalStorageDirectory()
.getPath() + FOLDER_NAME + imageFileName;
String urlServer = "http://idmstest.ueuo.com/upload_api/uploadtoserver.php";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
Log.d("tester", pathToOurFile);
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)
@SuppressWarnings("unused")
int serverResponseCode = connection.getResponseCode();
@SuppressWarnings("unused")
String serverResponseMessage = connection.getResponseMessage();
Log.i("uploadFile", "HTTP Response is : "
+ serverResponseMessage + ": " + serverResponseCode);
fileInputStream.close();
outputStream.flush();
outputStream.close();
Log.d("tester", pathToOurFile);
} catch (Exception ex) {
// Exception handling
ex.printStackTrace();
Log.e("Upload file to server Exception", "Exception : "
+ ex.getMessage(), ex);
}
}
这是服务器端的php:
<?php
$file_path = "uploads/";
$file_path = $file_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $file_path)) {
echo "success";
} else{
echo "fail";
}
?>